Hi, I have problem with my date validation ajax. My code not checks choosen date. I would like get array objects from database and check each attribute date.
my model
[['data'], 'safe'],
[['data'],'validateDate'],
[['id_movie'], 'exist', 'skipOnError' => true, 'targetClass' => Movie::className(), 'targetAttribute' => ['id_movie' => 'id_movie']],
[['id_room'], 'exist', 'skipOnError' => true, 'targetClass' => Room::className(), 'targetAttribute' => ['id_room' => 'id_room']],
];
}
public function validateDate($attribute, $params)
{
if (!$this->$attribute=='2016-02-10 10:55') {
$this->addError($attribute, 'The country must be either "USA" or "Web".');
}
}
controller
public function actionCreate()
{
$model = new Seanse();
//sprawdzenie daty seansu
if(Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
else if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->save()) {
Yii::$app->session->setflash('success','Utworzono Seans');
return $this->redirect(['view', 'id' => $model->id_seans]);
} else {
return $this->renderAjax('create', [
'model' => $model,
]);
}
}
view
$form = ActiveForm::begin([
'id'=>$model->formName(),
'enableAjaxValidation'=>true,
]); ?>
<?= $form->field($model, 'data')->widget(DateTimePicker::classname(), [
'options' => ['placeholder' => 'Enter event time ...'],
'pluginOptions' => [
'autoclose' => true
]
]);
?>