This is the login form model {code ...} appcomponentsNameValidator is a self-defined validator: {code ...} call the LoginForm validate method in PassportController for verification: {code ...} the result is my custom appcomponentsNa... this is the login form model.
class LoginForm extends Model{ public $account; public $password; /** * @return array the validation rules. */ public function rules() { return [ ['account','app\components\NameValidator','maxLen'=>12] ]; }}
App \ components \ NameValidator is a self-defined validators:
Namespace app \ components; use yii \ validators \ Validator; class NameValidator extends Validator {public $ minLen = 2; public $ maxLen = 14; public $ charset = 'utf-8 '; public $ message; public function validateValue ($ value) {file_put_contents('hello.txt ', '000000'); // test whether the method is executed. $ EnPattern = '/[a-z]/I'; $ zhPattern = '/^ [\ x {4e00}-\ x {9fa5}] * $/U '; $ valid = false; $ zhStr = preg_replace ($ enPattern, '', $ value,-1, $ count); $ enLen = $ count; $ zhLen = mb_strlen ($ zhStr, $ this-> charset); $ len = $ enLen + $ zhLen * 2; if ($ len> $ this-> maxLen | $ len <$ this-> minLen) {if (! $ This-> message) $ this-> message = 'incorrect Character Count ';} else if (! Preg_match ($ zhPattern, $ zhStr) {if (! $ This-> message) $ this-> message = 'invalid characters';} else {$ valid = true;} return $ valid? Null: [$ this-> message, [] ;}}
Call the LoginForm validate method in PassportController for verification:
Namespace app \ controllers; use Yii; use yii \ web \ Controller; use app \ models \ LoginForm; class PassportController extends Controller {public $ enableCsrfValidation = false; public $ layout = '@ app/views/passport/main. php'; public function actionLogin () {if (\ yii: $ app-> request-> isPost) {$ login = new LoginForm (); $ login-> load (\ yii: $ app-> request-> post (); if ($ login-> validate () {echo 'valid pass ';} else {echo $ login-> errors; }}$ this-> view-> title = 'login'; return $ this-> render ('login ');}}
The result is that the validateValue method in the Custom app \ components \ NameValidator class is not executed. In the actionLogin methodRun $ login-> validate ()
Always output the valid pass string. I don't know what this is about?
I know this can be achieved through the in-row validators, but I want to ask what is going on here? Why is the verification method not executed here?
Reply content:
This is the login form model.
class LoginForm extends Model{ public $account; public $password; /** * @return array the validation rules. */ public function rules() { return [ ['account','app\components\NameValidator','maxLen'=>12] ]; }}
App \ components \ NameValidator is a self-defined validators:
Namespace app \ components; use yii \ validators \ Validator; class NameValidator extends Validator {public $ minLen = 2; public $ maxLen = 14; public $ charset = 'utf-8 '; public $ message; public function validateValue ($ value) {file_put_contents('hello.txt ', '000000'); // test whether the method is executed. $ EnPattern = '/[a-z]/I'; $ zhPattern = '/^ [\ x {4e00}-\ x {9fa5}] * $/U '; $ valid = false; $ zhStr = preg_replace ($ enPattern, '', $ value,-1, $ count); $ enLen = $ count; $ zhLen = mb_strlen ($ zhStr, $ this-> charset); $ len = $ enLen + $ zhLen * 2; if ($ len> $ this-> maxLen | $ len <$ this-> minLen) {if (! $ This-> message) $ this-> message = 'incorrect Character Count ';} else if (! Preg_match ($ zhPattern, $ zhStr) {if (! $ This-> message) $ this-> message = 'invalid characters';} else {$ valid = true;} return $ valid? Null: [$ this-> message, [] ;}}
Call the LoginForm validate method in PassportController for verification:
Namespace app \ controllers; use Yii; use yii \ web \ Controller; use app \ models \ LoginForm; class PassportController extends Controller {public $ enableCsrfValidation = false; public $ layout = '@ app/views/passport/main. php'; public function actionLogin () {if (\ yii: $ app-> request-> isPost) {$ login = new LoginForm (); $ login-> load (\ yii: $ app-> request-> post (); if ($ login-> validate () {echo 'valid pass ';} else {echo $ login-> errors; }}$ this-> view-> title = 'login'; return $ this-> render ('login ');}}
The result is that the validateValue method in the Custom app \ components \ NameValidator class is not executed. In the actionLogin methodRun $ login-> validate ()
Always output the valid pass string. I don't know what this is about?
I know this can be achieved through the in-row validators, but I want to ask what is going on here? Why is the verification method not executed here?
Validator: validateAttribute () and Validator: validateValue () are two implementation methods.
Take a closer look at the document
You can use Yii: log () for debugging ()