Why is the custom validator of yii2 not executed?

Source: Internet
Author: User
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?

  1. Validator: validateAttribute () and Validator: validateValue () are two implementation methods.

  2. Take a closer look at the document

  3. You can use Yii: log () for debugging ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.