Home > Backend Development > PHP Tutorial > 自己写的yii 独立验证器不能使用

自己写的yii 独立验证器不能使用

WBOY
Release: 2016-06-06 20:20:08
Original
1263 people have browsed it

namespace app\models;

use Yii;
use yii\base\Model;

class RegisterForm extends Model
{

<code>public $email;
public $password;
public $password_repeat;
</code>
Copy after login
Copy after login
<code>public function rules()
{
    return [
        [['email', 'password', 'password_repeat'], 'required'],
        ['email', 'email'],
        //['email', 'unique', 'targetClass' => '', 'message' => '邮箱名已存在。'],
        [['password', 'password_repeat'], 'string', 'length' => [6, 20]],
        ['password', 'checkPassword'],
        ['password_repeat','compare','compareAttribute' =>'password','message'=>'两次输入的密码不一致。'],
    ];
}

public function register()
{
    //处理一些东西
    //注册相关的
    return false;
}

public function attributeLabels()
{
    return [
        'email' => '邮箱',
        'password' => '密码',
        'password_repeat' => '重复密码',
    ];
}

public function checkPassword($attribute, $params)
{
    if (!ctype_alnum($this->$attribute)) {
        $this->addError($attribute, '必须包含字母或数字。');
    }
}</code>
Copy after login
Copy after login

}

checkPassword 这个独立验证器 使用不了 我代码书写有问题吗 各位大哥们

回复内容:

namespace app\models;

use Yii;
use yii\base\Model;

class RegisterForm extends Model
{

<code>public $email;
public $password;
public $password_repeat;
</code>
Copy after login
Copy after login
<code>public function rules()
{
    return [
        [['email', 'password', 'password_repeat'], 'required'],
        ['email', 'email'],
        //['email', 'unique', 'targetClass' => '', 'message' => '邮箱名已存在。'],
        [['password', 'password_repeat'], 'string', 'length' => [6, 20]],
        ['password', 'checkPassword'],
        ['password_repeat','compare','compareAttribute' =>'password','message'=>'两次输入的密码不一致。'],
    ];
}

public function register()
{
    //处理一些东西
    //注册相关的
    return false;
}

public function attributeLabels()
{
    return [
        'email' => '邮箱',
        'password' => '密码',
        'password_repeat' => '重复密码',
    ];
}

public function checkPassword($attribute, $params)
{
    if (!ctype_alnum($this->$attribute)) {
        $this->addError($attribute, '必须包含字母或数字。');
    }
}</code>
Copy after login
Copy after login

}

checkPassword 这个独立验证器 使用不了 我代码书写有问题吗 各位大哥们

写法是正确的,你是凭什么判定不能使用的呢?

这是我的测试用例:

<code>class TestModel extends \yii\base\Model
{
    public $password;

    public function rules()
    {
        return [
            ['password', 'checkPassword']
        ];
    }

    public function checkPassword($attribute, $params)
    {
        if (!ctype_alnum($this->$attribute)) {
            $this->addError($attribute, 'password error');
        }
    }
}</code>
Copy after login

test case1:

<code>$model = new TestModel();
$model->password = '1121312&&UJ9123/.';

$model->validate();

var_dump($model->getErrors()); //会打印出有错误</code>
Copy after login

test case 2:

<code>$model = new TestModel();
$model->password = '1121312';

$model->validate();

var_dump($model->getErrors()); //无错误</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template