开发者

Problem with Symfony form validation

开发者 https://www.devze.com 2022-12-18 01:47 出处:网络
I\'m unable to see what I may be doing wrong with the following Symfony 1.4 form validation. Basically, all I just want is for all four conditions to be taken correctly into account (required, min-len

I'm unable to see what I may be doing wrong with the following Symfony 1.4 form validation. Basically, all I just want is for all four conditions to be taken correctly into account (required, min-length, max-length, regular expression). It actually WORKS, but for the 'required' condition it fails to display my custom error message and just says "Required" instead. Is there a way to get MY error message to show?

'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).')
        ),
)),

One additional thing, if I remove the Regex validator and just turn it into a normal 开发者_如何学编程single-line String validator, my custom error message does show!?

Anyone?

Thanks in advance.


I've noticed same issue about two weeks ago and found a solution - just move your message to sfValidatorAnd declaration:

'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.')),

That helped me, I hope it helps you too.

0

精彩评论

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