Yii2不能自定义独立验证器(内联也是如此)

时间:2016-10-12 11:41:02

标签: php validation yii2

在Yii2中,我尝试编写一个独立的验证器,但它不起作用。在我的代码下面:

独立验证器:

namespace app\components;

use yii\validators\Validator;

class UsernameValidator extends Validator {
    public function validateAttribute($model, $attribute)
    {
        $this->addError($model, $attribute, 'Test.');
    }
}

型号代码:

namespace app\models;

use Yii;
use yii\base\Model;
use app\components\UsernameValidator;

class SignUpForm extends Model {
    public $username;
    public $password;
    public $confirmPassword;

    public function rules(){
        return [
            [['username', 'password', 'confirmPassword'], 'required'],      
            ['password', 'compare', 'compareAttribute' =>    'confirmPassword', 'operator' => '=='],
            ['confirmPassword', 'compare', 'compareAttribute' => 'password', 'operator' => '=='],
            ['username', UsernameValidator::className(), 'skipOnEmpty' => false],
        ];
   }
}

skipOnemptyskipOnError无法解决该问题。请问,谁经历过这个?帮我解决。

0 个答案:

没有答案