使用Symfony 1.4的表单,如何在嵌入表单的post验证器中抛出sfValidatorError?
我的父表单调用以下内容:
public function configure(){
$this->embedForm('page', $pageLinkForm);
}
我的嵌入式表单:
public function configure(){
$this->validatorSchema->setPostValidator(new sfValidatorCallback(array(
'callback' => array($this, 'validateLink')
)));
}
public function validateLink($validator, $values) {
if (!empty($values['link']) && !empty($values['outside_link']))
throw new sfValidatorError($validator, 'Only specify either an internal link or an external link, but not both.');
}
post验证器运行validateLink,它会抛出sfValidatorError,但它不会显示为全局错误,而且形式为isValid(),但它不应该是。
为什么忽略错误?怎么能让它不被忽视?
答案 0 :(得分:2)
恕我直言,最好抛出一个sfValidatorSchemaError,如下所示:
$error = new sfValidatorError($validator, 'invalid', array('value' => $field_name));
throw new sfValidatorErrorSchema($validator, array($field_name => $error));
如果你想在嵌入表单中抛出错误,只需嵌入sfValidatorSchemaError:
//define container
$errorSchema = new sfValidatorErrorSchema($validator);
//embedded field error
$error = new sfValidatorError($validator, 'invalid', array('value' => $field_name));
$errorSchema->addError($error, $field_name);
//associate $errorSchema to your embedded field
throw new sfValidatorErrorSchema($validator, array('page' => $errorSchema));
答案 1 :(得分:0)
public function bind(array $taintedValues = null, array $taintedFiles = null)
{
sfLoader::loadHelpers(array('I18N'));
parent::bind($taintedValues, $taintedFiles);
if($taintedValues["password"])
{
if(!$taintedValues["pwd_verify"])
{
$this->getErrorSchema()->addError(new sfValidatorError(new sfValidatorSchema(), __('Please reenter the new password.')), 'password');
}
}
}
我希望它可以帮到你。