自己寫的yii 獨立驗證器不能使用

來源:互聯網
上載者:User
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 這個獨立驗證器 使用不了 我代碼書寫有問題嗎 各位大哥們

回複內容:

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 這個獨立驗證器 使用不了 我代碼書寫有問題嗎 各位大哥們

寫法是正確的,你是憑什麼判定不能使用的呢?

這是我的測試案例:

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()); //無錯誤
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.