Yii2: how to use custom validation function for activeform?

前端 未结 12 1160
独厮守ぢ
独厮守ぢ 2020-12-25 14:18

In my form\'s model, I have a custom validation function for a field defined in this way

class SignupForm extends Model
{
    public function rules()
    {
          


        
12条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-25 14:45

    I had common problem.

    In your validation function:

    public function checkDateFormat($attribute, $params)
        {
            // no real check at the moment to be sure that the error is triggered
            $this->addError($attribute, Yii::t('user', 'You entered an invalid date format.'));
        }
    

    $params doesn`t get any value at all. It actually always equals to Null. You have to check for your attribute value in function:

    public function checkDateFormat($attribute, $params)
    {
        if($this->birth_date == False)
        {
            $this->addError($attribute, Yii::t('user', 'You entered an invalid date format.'));
        }
    }
    

    that`s how it worked for me.

提交回复
热议问题