I am trying to only allow alphanumeric fields for a first name using:
patientFirstName:
- NotBlank: ~
- Regex:
pattern: "/[^a-z_\-0-9]/i"
htmlPattern: "^[a-zA-Z\-0-9]+$"
message: Name must be alphanumeric
However it is still allowing characters like "&&&". Is my regex wrong?
Symfony 3
If you do not want to use regex, you can use @Assert\Type for validator alphanumeric
You can see it in the symfony doc here
@Assert\Type(type="alnum")
You are missing ^
, $
and +
(just like your html pattern): /^[a-z\-0-9]+$/i
If you do not add them, the regex will match any string if it contains at least one alphanumeric character.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With