yii2自訂的validator為何不執行呢?

來源:互聯網
上載者:User
這個是登入表單模型

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是我自訂的一個驗證器:

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');//測試方法是否執行。        $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='字元數不正確';        }else if(!preg_match($zhPattern,$zhStr))        {            if(!$this->message)$this->message='字元不合格';        }else{            $valid=true;        }        return $valid ? null : [$this->message,[]];    }}

在PassportController中調用LoginForm的validate方法進行驗證:

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');    }}

結果就是我自訂的app\components\NameValidator類中的validateValue方法並沒有執行,在actionLogin方法中執行$login->validate()時總是輸出valid pass字串。不知道這個是怎麼回事?

我知道可以通過行內驗證器可以達到這樣的目的,但我想問下這裡的情況是怎麼回事?為何這裡的驗證方法不執行呢?

回複內容:

這個是登入表單模型

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是我自訂的一個驗證器:

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');//測試方法是否執行。        $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='字元數不正確';        }else if(!preg_match($zhPattern,$zhStr))        {            if(!$this->message)$this->message='字元不合格';        }else{            $valid=true;        }        return $valid ? null : [$this->message,[]];    }}

在PassportController中調用LoginForm的validate方法進行驗證:

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');    }}

結果就是我自訂的app\components\NameValidator類中的validateValue方法並沒有執行,在actionLogin方法中執行$login->validate()時總是輸出valid pass字串。不知道這個是怎麼回事?

我知道可以通過行內驗證器可以達到這樣的目的,但我想問下這裡的情況是怎麼回事?為何這裡的驗證方法不執行呢?

  1. Validator::validateAttribute() 與 Validator::validateValue() 是兩種實現方式, 你用混了

  2. 仔細看文檔

  3. 調試可以用 Yii::log()

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.