2amigos / yii2-date-picker-widget

Bootstrap DatePicker Widget for Yii2
https://2amigos.us
Other
130 stars 77 forks source link

Datepicker widget not passing validation #27

Closed codepolariz closed 7 years ago

codepolariz commented 8 years ago

I used the datepicker to display date of birth with model rules and controller validate(), unfortunately validation does not work. VIEW

     <?= DatePicker::widget([
    'model' => $model,
    'form' => $form,
    'attribute' => 'dob',
    'template' => '{addon}{input}',
        'clientOptions' => [
        'format' => 'L',
        'autoclose' => true,
        'format'  => 'yyyy-mm-dd', 
        'endDate'=> '1970-12-31', 
        ]
]);?>

MODEL RULE

[['dob'], 'required'],
[['dob'], 'date'],

CONTROLLER

if ($model->load(Yii::$app->request->post())&& $model->validate())
 {
   // save record.
}
tonydspaniard commented 8 years ago

Could be that you hace changed its template

codepolariz commented 8 years ago

No, i didn't modify the template in any way this is my view code


<?= DatePicker::widget([
'model' => $model,
'attribute' => 'dob',
'template' => '{addon}{input}',
'clientOptions' => [
'format' => 'L',
'autoclose' => true,
'format' => 'yyyy-mm-dd', 
'endDate'=> '1970-12-31', 
]
]);?>
tonydspaniard commented 8 years ago
'template' => '{addon}{input}',

What if we use:

<?= $form->field($model, 'dob')->widget(
                    DatePicker::className(),
                    [
                        'clientOptions' => [
                            'autoclose' => true,
                            'format' => 'yyyy-mm-dd'
                        ]
                    ]
                ); ?>
philippfrenzel commented 8 years ago

Hey, have the same issue - any updates on this one?

tonydspaniard commented 8 years ago

@philippfrenze I need more info as I do use the widget and certainly the validation happens.

mahesheu commented 7 years ago

i had the same issue but when i gave it like this

<?= $form->field($model, 'dob')->widget(
                    DatePicker::className(),
                    [
                        'clientOptions' => [
                            'autoclose' => true,
                            'format' => 'yyyy-mm-dd'
                        ]
                    ]
                ); ?>

instead of

 <?= DatePicker::widget([
                'model' => $model,
                 'attribute' => 'dob',
                'template' => '{addon}{input}',
                'clientOptions' => [
                'autoclose' => true,
                'format' => 'yyyy-mm-dd'
                ]
            ]);?>

it started to work.

faniuta commented 7 years ago

use regx and preg_match($format, $date) after get time and check valid time ,u can define validator in model

tonydspaniard commented 7 years ago

@mahesheu already gave the solution and is as I suspected on the client side for the template.

The problem on the server could be that somehow the dob is filled with values and thats why is not firing when validating. The solution provided by @faniuta could be the best and also including trim filter on our rules too.