Jens
Jens

Reputation: 291

How to add error messages for DateValidation?

In my model I use DateValidation

['date_birthday', 'date', 'format' => 'd.m.yy', 'min' => '01.01.1900', 'max' => date('d.m.yy'), 'tooSmall'=>'The date is from past. Try another','tooBig' => 'The date is from future. Try another', 'message' => 'Try to input the date'],

In view I call the widget

 <?php echo $form->field($modelForm, 'date_birthday')->widget(\kartik\date\DatePicker::classname(), [
                        'type' => \kartik\date\DatePicker::TYPE_COMPONENT_APPEND,
                        'pickerButton' => false,
                        'options' => [
                            'placeholder' => '',
                        ],
                        'pluginOptions' => [
                            'format' => 'dd.mm.yyyy',
                            'autoclose' => true,
                            'showMeridian' => true,
                            'startView' => 2,
                            'minView' => 2,
                        ]
                    ]) ?>

It checks for min and max dates, but show no error message. I think its because of different date formats in model and view. How to fix it?

Upvotes: 0

Views: 1043

Answers (1)

Pahanium
Pahanium

Reputation: 56

If you submit form you will see error messages. According to this issue https://github.com/yiisoft/yii2/issues/7745 yii2 have not client-side validations of date

You can enable ajax validation. Add in create and update action before if statement

if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
    Yii::$app->response->format = yii\web\Response::FORMAT_JSON;
    return ActiveForm::validate($model);
}

And add use yii\widgets\ActiveForm; on the top of controller class. In your _form.php file enable ajax for whole form

<?php $form = ActiveForm::begin([
    'enableAjaxValidation' => true,
]); ?>

, or for the field only

<?php echo $form->field($model, 'date_birthday', ['enableAjaxValidation' => true])->widget(\kartik\date\DatePicker::classname(), [
...

Also, you can add plugin options for limit date with startDate and endDate (https://bootstrap-datepicker.readthedocs.io/en/latest/options.html#startdate)

 <?php echo $form->field($model, 'date_birthday', ['enableAjaxValidation' => true])->widget(\kartik\date\DatePicker::classname(), [
                    'type' => \kartik\date\DatePicker::TYPE_COMPONENT_APPEND,
                    'pickerButton' => false,
                    'options' => [
                        'placeholder' => '',
                    ],
                    'pluginOptions' => [
                        'format' => 'dd.mm.yyyy',
                        'autoclose' => true,
                        'showMeridian' => true,
                        'startView' => 2,
                        'minView' => 2,
                        'startDate' => '01.01.1900',
                        'endDate' => date('d.m.Y'),
                    ]
                ]) ?>

Upvotes: 2

Related Questions