Self-written Yii standalone authenticator cannot be used

Source: Internet
Author: User
Tags yii
namespace app\models;

Use Yii;
Use Yii\base\model;

Class Registerform extends Model
{

public $email;public $password;public $password_repeat;
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, '必须包含字母或数字。');    }}

}

Checkpassword This standalone authenticator can't use my code. You guys, guys.

Reply content:

namespace app\models;

Use Yii;
Use Yii\base\model;

Class Registerform extends Model
{

public $email;public $password;public $password_repeat;
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, '必须包含字母或数字。');    }}

}

Checkpassword This standalone authenticator can't use my code. You guys, guys.

How do you decide not to use the correct wording?

Here is my test case:

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

Test CASE1:

$model = new TestModel();$model->password = '1121312&&UJ9123/.';$model->validate();var_dump($model->getErrors()); //会打印出有错误

Test Case 2:

$model = new TestModel();$model->password = '1121312';$model->validate();var_dump($model->getErrors()); //无错误
  • 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.