Pass custom parameters to custom ValidationConstraint in Symfony2

后端 未结 5 1092
梦谈多话
梦谈多话 2021-02-04 02:25

I\'m creating a form in Symfony2. The form only contains one book field which allows the user to choose between a list of Books entities. I need to c

5条回答
  •  青春惊慌失措
    2021-02-04 03:14

    Well, I'm not that familier with the Form/Validation component, but you can use a Hidden field with the name/id of the author and check if it's the same:

    class MyFormType extends AbstractType
    {
        protected $author;
    
        public function __construct(Author $author) {
            $this->author = $author;
        }
    
        public function buildForm(FormBuilderInterface $builder, array $options) {
            $builder
                ->add('book', 'entity', array('class' => 'AcmeDemoBundle:Book', 'field' => 'title');
                ->add('author_name', 'hidden', array(
                    'data' => $this->author->getId(),
                ))
            ;
        }
    
        // ...
    }
    

提交回复
热议问题