如何改变教义的“无效”信息?

时间:2010-04-02 18:19:13

标签: php symfony1 doctrine

我正在使用symfony和学说,我做了一个注册表。电子邮件字段被声明为唯一,如果我将其设置为已存在,我会收到消息An object with the same "email" already exist.。我使用以下验证器:

$this->validatorSchema['email'] = new sfValidatorEmail(array('required' => true), array('invalid' => 'Does not seem to be a proper email'));

如何更改“已存在”消息?感谢

1 个答案:

答案 0 :(得分:4)

您看到的唯一字段上的错误发生在验证后,因此您需要覆盖验证器,您可以在表单中执行此操作:

$this->validatorSchema->setPostValidator(
  new sfValidatorDoctrineUnique(
    array(
      'model' => 'Profile',
      'column' => array('email'),
      'throw_global_error' => false
    ),
    array(
      'invalid' => 'A user with that %column% already exists'
    )
  )
);

如果您需要验证多个字段,例如存储在username中的sfGuardUser您可以将sfValidatorAnd(接受多个验证程序的单个数组)传递给setPostValidator

此外:如果您需要保留其他现有验证器,则值得研究mergePostValidator方法。