Incorrect pattern matching in Yii 2

I have a bunch of validation rules in my method rules

and all the errors seem to work, but the problem with this:

['username', 'match', 'pattern' => '/[a-zA-Z0-9_-]+/', 'message' => 'Your username can only contain alphanumeric characters, underscores and dashes.'],

      

He checks what is the wrong behavior.

Am I doing something wrong here?

+3


source to share


3 answers


This pattern only checks the first character. You need to fix it like below:

['username', 'match', 'pattern' => '/^[a-zA-Z0-9_-]+$/', 'message' => 'Your username can only contain alphanumeric characters, underscores and dashes.'],

      



To make sure the Yii compliance is working, you can test it by writing @

(for example) as the first character. Then you can see that the validation is working. So the problem is with your template.

+9


source


The above answer also doesn't work in my project. I am asking you to follow the below solution:



[['userame'],'match', 'pattern' => '/^[a-z]\w*$/i','message' => 'Enter Characters Only'],

      

0


source


All of the above solutions didn't work for me. This is the best thing, so try:

['first_name', 'match', 'pattern' => '/^[a-z][A-Za-z,;\"\\s]+[!?.]$/i'], 

      

0


source







All Articles