kartik-v datepicker and validation

I’m creating a form where users have to say their birth date.

To generate the field I’m using kartik-v datepicker, it appears on the page, but now that I’m trying to set the validation I have an error on the page.

Inside the model the field is set as required and it is associated to a custom function to validate the format:

[‘birth_date’, ‘required’],

[‘birth_date’, ‘checkDateFormat’],

This is what I have about the datepicker into the form’s view file




$model->birth_date = date(Yii::$app->params['dateFormat'], $model->birth_date);


// this is not automatically generated by the widget

echo '<div class="form-group field-signupform-birth_date">';


echo '<label class="control-label" for="signupform-birth_date"></label>';

echo DatePicker::widget([

    'name' => 'birth_date',

    'id' => 'signupform-birth_date',

    'model' => $model,

    'type' => DatePicker::TYPE_COMPONENT_APPEND,

    'value' => '',

    'form' => $form,

    'attribute' => 'birth_date',

    'options' => ['placeholder' => Yii::t('user','Birth Date')],

    'pluginOptions' => [

        'autoclose'=>true,

        'format' => 'yyyy/mm/dd'

    ]

]);


// this is not automatically generated by the widget

echo '<p class="help-block help-block-error"></p>';

echo '</div>';



But when I go to the page it returns an error "Undefined index: dateFormat".

Could you tell me what’s wrong with my code?

I know what "Undefined index" means, but where and how should I change it?

Once solved the index problem, will I have to set anything else to make the validation work both on client side and server side and make the error messages correctly appear under the input field?

I’ve found the problem:

  1. I don’t need

$model->birth_date = date(Yii::$app->params[‘dateFormat’], $model->birth_date);

at all.

  1. I had to use the Html helper’s function to generate the field’s paragraph where the error is shown.

This is the correct code




echo '<div class="form-group field-signupform-birth_date">';

echo '<label class="control-label" for="signupform-birth_date"></label>';

echo DatePicker::widget([

    'model' => $model,

    'form' => $form,

    'name' => 'birth_date',

    'id' => 'signupform-birth_date',

    'type' => DatePicker::TYPE_COMPONENT_APPEND,

    'attribute' => 'birth_date',

    'options' => ['placeholder' => Yii::t('user','Birth Date')],

    'pluginOptions' => [

        'autoclose'=>true,

        'format' => 'yyyy-mm-dd'

    ]

]);

echo Html::error($model, 'birth_date');

echo '</div>';