K
K
ktotaika2018-11-19 19:30:46
Yii
ktotaika, 2018-11-19 19:30:46

How to make yii2 date validation work correctly?

Hello. Faced such a problem that the validation rules for the date field do not work.
Essence: there is a field "date of registration" and "date of dismissal". The first field is required, the second is not, but it must be checked that the registration date is less than the dismissal date.
In fact: when choosing any date of registration, an error pops up. The value of "Reg Date" must be less than the value of "Expel Date", no matter whether the correct date of dismissal is selected or not selected at all.

public function rules()
    {
        return
        [
            [['f_fio', 'i_fio', 'o_fio'], 'match', 'pattern' => '/^[A-Za-zА-Яа-яЁё\-\s\']*$/'],
            [['f_fio', 'i_fio', 'sex', 'id_organization', 'reg_date', 'birthday'], 'required'],
            ['reg_date', 'compare', 'compareAttribute' => 'expel_date', 'operator' => '<'],
            ['expel_date', 'date', 'skipOnEmpty' => true],
            [['specializations', 'o_fio',  'id_user', 'expel_date'], 'safe'],

        ];
    }

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
K
ktotaika, 2018-11-20
@ktotaika

I solved the problem on my own, this version of the validator helped:
Removed the line with skipOnEmpty, it is not needed

D
Dmitry, 2018-11-19
@slo_nik

Good evening.
I hope that this example will help you solve the problem.

['date_to', 'date', 'when' => function($model){
     return strtotime($model->date_to) < strtotime($model->date_from);
}, 'message' => 'Дата прилёта должна быть больше или равна Дате вылета']

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question