开发者

display a symfony post-validation error in an embeded Form

开发者 https://www.devze.com 2023-03-13 10:17 出处:网络
I have a form witch contains embeded开发者_如何学运维Forms. the Post-Validator: $this->validatorSchema->setPostValidator(

I have a form witch contains embeded开发者_如何学运维Forms.

the Post-Validator:

$this->validatorSchema->setPostValidator(
  new sfValidatorCallback(
    array('callback' => array($this, 'myPostValidator'))
  )
);

I throw an error in the myPostValidator method:

$error = new sfValidatorError($validator, 'invalid ' . $values['embededform1']['field']);
throw new sfValidatorErrorSchema($validator, array('field' => $error));

now I get an global error for the root-form.

I want to display the error next to the correct field.

$values['embededform1']['field']


My first suggestion would be to add the post validator in the embedded form. While embedding it will copy the validator to the container form, and your code for throwing the exception should work as expected.

Alternative you can try the following in the callback on your container form:

$error = new sfValidatorError($validator, 'invalid');
$errorschema = new sfValidatorErrorSchema($validator, array('field' => $error));
throw new sfValidatorErrorSchema($validator, array('embeddedformname' => $errorschema));


$error = new sfValidatorError($validator, 'invalid');
$errorschema = new sfValidatorErrorSchema($validator, array('field' => $error));
throw new sfValidatorErrorSchema($validator, array('embeddedformname' => $errorschema));

this worked for me.

0

精彩评论

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

关注公众号