0

I have this bit of code implemented with symfony:

$p1 = '/^[0-9]{8}[A-Z]$/i';
$p2 = '/^[XYZ][0-9]{7}[A-Z]$/i';

$this->setValidators(array(
 ...
 'field_1' => new sfValidatorRegex(
          array('pattern' => p1.'|'.p2)),

...

The field_1 must match either pattern p1 or p2, but it doesn't work. Any help please.

1 Answer 1

2

You concatenate the delimiters and flags, that doesn't work.

Try:

$p1 = '(?i:^[0-9]{8}[A-Z]$)';
$p2 = '(?i:^[XYZ][0-9]{7}[A-Z]$)';

$this->setValidators(array(
 ...
 'field_1' => new sfValidatorRegex(
          array('pattern' => "/$p1|$p2/")),

Also in your case you can just use a pattern like:

$re = '/^[XYZ0-9][0-9]{7}[A-Z]$/i';
Sign up to request clarification or add additional context in comments.

2 Comments

or use 2 seperate sfValidatorRegex in combination with sfValidatorOr
It works. Thanks a lot. I didn't want to use two sfValidatorRegex combined with sfValidatorOr cause it throws two invalid messages if an error has occured.

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.