在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],
];
}
}
skipOnempty
,skipOnError
无法解决该问题。请问,谁经历过这个?帮我解决。