Home  >  Article  >  Backend Development  >  yii2自定义的validator为何不执行呢?

yii2自定义的validator为何不执行呢?

WBOY
WBOYOriginal
2016-06-06 20:23:281673browse

这个是登录表单模型

<code>class LoginForm extends Model
{
    public $account;
    public $password;

    /**
     * @return array the validation rules.
     */

    public function rules()
    {
        return [
            ['account','app\components\NameValidator','maxLen'=>12]
        ];
    }
}</code>

app\components\NameValidator是我自定义的一个验证器:

<code>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 || $lenminLen)
        {
            if(!$this->message)$this->message='字符数不正确';
        }else if(!preg_match($zhPattern,$zhStr))
        {
            if(!$this->message)$this->message='字符不合格';
        }else{
            $valid=true;
        }
        return $valid ? null : [$this->message,[]];
    }
}</code>

在PassportController中调用LoginForm的validate方法进行验证:

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

结果就是我自定义的app\components\NameValidator类中的validateValue方法并没有执行,在actionLogin方法中执行$login->validate()时总是输出valid pass字符串。不知道这个是怎么回事?

我知道可以通过行内验证器可以达到这样的目的,但我想问下这里的情况是怎么回事?为何这里的验证方法不执行呢?

回复内容:

这个是登录表单模型

<code>class LoginForm extends Model
{
    public $account;
    public $password;

    /**
     * @return array the validation rules.
     */

    public function rules()
    {
        return [
            ['account','app\components\NameValidator','maxLen'=>12]
        ];
    }
}</code>

app\components\NameValidator是我自定义的一个验证器:

<code>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 || $lenminLen)
        {
            if(!$this->message)$this->message='字符数不正确';
        }else if(!preg_match($zhPattern,$zhStr))
        {
            if(!$this->message)$this->message='字符不合格';
        }else{
            $valid=true;
        }
        return $valid ? null : [$this->message,[]];
    }
}</code>

在PassportController中调用LoginForm的validate方法进行验证:

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

结果就是我自定义的app\components\NameValidator类中的validateValue方法并没有执行,在actionLogin方法中执行$login->validate()时总是输出valid pass字符串。不知道这个是怎么回事?

我知道可以通过行内验证器可以达到这样的目的,但我想问下这里的情况是怎么回事?为何这里的验证方法不执行呢?

  1. Validator::validateAttribute() 与 Validator::validateValue() 是两种实现方式, 你用混了

  2. 仔细看文档

  3. 调试可以用 Yii::log()

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn