Why does the YII2 custom validator not execute?

Source: Internet
Author: User
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 validator that I have customized:

  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 ', ' 123 ');//test 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 * *; if ($len > $this->maxlen | | $len < $this->minlen) {if (! $this->message) $this->message= ' character count is not        Correct ';        }else if (!preg_match ($zhPattern, $zhStr)) {if (! $this->message) $this->message= ' character unqualified ';        }else{$valid =true; } return $valid?    Null: [$this->message,[]]; }}

In Passportcontroller, call LoginForm's Validate method for validation:

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='登录';        return $this->render('login');    }}

The result is that the Validatevalue method in my custom App\components\namevalidator class is not executed, and the 执行$login->validate() valid pass string is always output in the Actionlogin method. Don't know what's going on here?

I know it's possible to do this with an inline validator, but I want to ask what's going on here? Why is the verification method not implemented 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 validator that I have customized:

  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 ', ' 123 ');//test 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 * *; if ($len > $this->maxlen | | $len < $this->minlen) {if (! $this->message) $this->message= ' character count is not        Correct ';        }else if (!preg_match ($zhPattern, $zhStr)) {if (! $this->message) $this->message= ' character unqualified ';        }else{$valid =true; } return $valid?    Null: [$this->message,[]]; }}

In Passportcontroller, call LoginForm's Validate method for validation:

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='登录';        return $this->render('login');    }}

The result is that the Validatevalue method in my custom App\components\namevalidator class is not executed, and the 执行$login->validate() valid pass string is always output in the Actionlogin method. Don't know what's going on here?

I know it's possible to do this with an inline validator, but I want to ask what's going on here? Why is the verification method not implemented here?

    1. Validator::validateattribute () and Validator::validatevalue () are two ways to implement

    2. Take a closer look at the document

    3. Commissioning can be made with Yii::log ()

  • Related Article

    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.