0

I have an entity for which I use Regex and NotBlank to validate. I need this field to contain only English letters, without numbers. Everything works fine, but when I post Cyrillic I don't get an error. Why is that? I need only English

/**
 * @var string
 *
 * @Assert\NotBlank()
 * @Assert\Regex(
 *           pattern= "/^\w+/",
 *           match=   false,
 *           message= "This text cannot contain numbers"
 * )
 *

1 Answer 1

2

You might have problem with Unicode. I could not find if the RegEx function does an Unicode or Ascii matching (\u option of preg_match), which might make a difference in your case (As you are certainly providing utf-8 string).

Reference:

http://www.regular-expressions.info/php.html

The second problem might be with:

\w+

Which basically means printable characters, include letters and numbers, in any encoding (If matching utf-8). In which case you might try:

[a-zA-Z]+
Sign up to request clarification or add additional context in comments.

1 Comment

By default in php the regex isn't using unicode. I'm not so sure with Symfony, though. Your last suggestion should be working fine.

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.