/** * Creates a new Messages model. * If creation is successful, the response will be a 'success'=true. * @return mixed * * $(document).on('submit','#messages-form',function(e) { * e.preventDefault(); * var $form = $(this); * $.ajax({ * url: '<?php echo y('.urlManager')->createUrl("/contact/default/create-ajax");?>', * dataType: 'json', * type: 'post', * data: $form.serialize(), * success: function (data){ * * console.log(data); * * $.each($form.serializeArray(), function(index, name) { * $('[name='+name.name+']') * .parent() * .find('#validate-'+name.name) * .remove(); * }); * * if(data.success) { * // here submit * alert(data.message); * * } else { * * $.each(data.data, function(name, errors) { * $('[name='+name+']') * .parent() * .append($('<p id="validate-'+name+'" class="help-block text-danger">'+errors.join(',<br>')+'</p>')); * }); * } * } * }); * }); * */ public function actionCreateAjax() { y('.response')->format = 'json'; $model = new Messages(); $model->attributes = $_REQUEST; if ($model->save()) { return ['success' => 1, 'data' => $model, 'message' => y('app', 'Message sent, Thanks for contact us')]; } else { return ['success' => 0, 'data' => $model->getErrors()]; } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Messages::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'created_at' => $this->created_at, 'sent' => $this->sent, 'read' => $this->read]); $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'email', $this->email])->andFilterWhere(['like', 'phone', $this->phone])->andFilterWhere(['like', 'message', $this->message]); return $dataProvider; }
/** * Finds the Messages model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Messages the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Messages::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }