views:

713

answers:

1

Hi,

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.

+5  A: 

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.

Darmen
Darmen, thanks a lot! Got it work with your solution after having spent way too much time trying to figure it out. Your code above gave me an error, so I replaced 'NULL' with 'array()' which clears the error.... which also means the first 'required' clause in the sfValidatorString can be removed. Thanks again.
Tom
@Tom , you're welcome. I fixed answer for users who'll same problem in the future.
Darmen