开发者

symfony: custom Validator does not work with embedRelation?

开发者 https://www.devze.com 2023-01-24 06:18 出处:网络
I am using the sym开发者_如何学Gofony embedRelation method to embed forms.The code is like this:

I am using the sym开发者_如何学Gofony embedRelation method to embed forms.The code is like this:

public function configure(){ //......

$this->embedRelation('Foos as foos');

$this->getEmbeddedForm('foos')->mergePostValidator(new MenuValidatorSchema()); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ this does not work. }


When embeding forms in Symfony, the top-level form keeps track of everything. The widget schema, validator schema, defaults, etc. of the embedded form are no longer directly used. You can see what's happening in sfForm::embedForm.

Note that in this case, since it's a post validator, it's perfectly acceptable to add it to the top-level form, i.e.:

$this->mergePostValidator(new MenuValidatorSchema());

If you want the validator schema on the embedded form and it has no current post validator, you can simply do:

$this->validatorSchema['foos']->setPostValidator(new MenuValidatorSchema());

If it has an existing one, you'll have to turn them into an sfValidatorAnd, doing something like:

$this->validatorSchema['foos']->setPostValidator(new sfValidatorAnd(array(
    'validators' => array(
         $this->validatorSchema['foos']->getPostValidator(),
         new MenuValidatorSchema()
    )
)));

The syntax of that last option is just one reason why setting post validators on the top-level form is the preferred option when available.

0

精彩评论

暂无评论...
验证码 换一张
取 消