3

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.

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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 , you're welcome. I fixed answer for users who'll same problem in the future.
Not sure if you did this intentionally, but [A-z0-9] will match characters you probably don't want (between 'Z' and 'a' are '[', '\', ']', '^', '_'). Randomly tried one of them and got surprised.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.