Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having issue with pattern match incorrectly validating in Yii 2

Tags:

regex

php

yii

yii2

I have a bunch of validation rules within my rules method and all the errors seem to work, but having an issue with this one:

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

It validates, which is the incorrect behavior.

Am I doing something wrong here?

like image 865
Brett Avatar asked Nov 26 '25 20:11

Brett


1 Answers

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

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

To make sure that Yii's matching is working, you can test it by writing @ (for example) as first character. Then, you can see that, validation works. So the problem is with your pattern.

like image 54
Ali MasudianPour Avatar answered Nov 28 '25 08:11

Ali MasudianPour



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!