Symfony表单验证问题

时间:2010-01-23 07:58:25

标签: forms symfony1 validation symfony-1.4

我无法看到以下Symfony 1.4表单验证可能出错的地方。基本上,我只想要所有四个条件都被正确考虑(必需,最小长度,最大长度,正则表达式)。它实际上是可行的,但对于“必需”条件,它无法显示我的自定义错误消息,而只是说“必需”。有没有办法让我的错误信息显示出来?

'username' => new sfValidatorAnd(array(
    new sfValidatorString(
        array('required' => true, 'min_length' => 4, 'max_length' => 20), 
        array('required' => 'Please enter a username.', 'min_length' => 'Your username must have at least 4 characters.', 'max_length' => 'Your username cannot be longer than 20 characters.')
        ),
    new sfValidatorRegex(
        array('pattern' => '/^[A-z0-9]*$/i'),
        array('invalid' => 'Your username can only have letters (A-Z) or numbers (0-9).')
        ),
)),

另外一件事,如果我删除正则表达式验证器并将其转换为普通的单行字符串验证器,我的自定义错误消息会显示!?

任何?

提前致谢。

1 个答案:

答案 0 :(得分:6)

我在两周前发现了同样的问题并找到了一个解决方案 - 只需将您的消息移至sfValidatorAnd声明:

'username' => new sfValidatorAnd(array(
    new sfValidatorString(
        array('required' => true, 'min_length' => 4, 'max_length' => 20), 
        array( 'min_length' => 'Your username must have at least 4 characters.', 'max_length' => 'Your username cannot be longer than 20 characters.')
        ),
    new sfValidatorRegex(
        array('pattern' => '/^[A-z0-9]*$/i'),
        array('invalid' => 'Your username can only have letters (A-Z) or numbers (0-9).')
        ),
), array(), array('required' => 'Please enter a username.')),

这对我很有帮助,我希望它对你有帮助。