[[+content_image]]
P
P
Pavel2018-12-03 16:50:27
Yii
Pavel, 2018-12-03 16:50:27

How to set unique date and date >= current day in validation rules?

Hello!
There are validation rules in the model:

[['object_id', 'release_date'], 'required'],
[['release_date'], 'date', 'format' => 'dd.MM.yyyy', 'timestampAttribute' => 'release_date'],
[['object_id'], 'integer'],
            [['object_id', 'release_date'], 'unique',
                'targetAttribute' => ['object_id', 'release_date']],
[['object_id'], 'exist', 'skipOnError' => true, 'targetClass' => Object::class, 'targetAttribute' => ['object_id' => 'id']],

Only one date can be added per object and the minimum date must be the current date.
Everything works fine with these rules, but you can add past dates.
I add 'min' => time() to release_date description and get error
SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid value for integer: "12/02/2018"
The SQL being executed was: SELECT EXISTS(SELECT * FROM "test" WHERE ("test"."object_id"='3') AND ("test"."release_date"='12/02/2018'))
The date is stored in the database as a timestamp.
The calendar is displayed by the DatePicker widget:
echo $form->field($model, 'release_date')->widget(DatePicker::class, [
                'clientOptions' => [
                    'minDate' => 'new Date()',
                ],
                'dateFormat' => 'dd.MM.yyyy',
                'options' => ['class' => 'form-control'],
            ])

I don't understand what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
Maxim Timofeev, 2018-12-03
@html_profi

It seems to me easier to write your validator in 3 lines:
https://www.yiiframework.com/doc/guide/2.0/en/inpu...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question