Model :
<?php
namespace app\models;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Message;
/**
* MessageSearch represents the model behind the search form of `app\models\Message`.
*/
class MessageSearch extends Message {
/**
* {@inheritdoc}
*/
public function rules() {
return [
[['id', 'message_type', 'status'], 'integer'],
[['message', 'message_type','sch_date'], 'safe'],
];
}
/**
* {@inheritdoc}
*/
public function scenarios() {
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params) {
$query = Message::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
// echo "
"; print_r($params);exit;
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
]);
$query->andFilterWhere(['like', 'message', $this->message]);
$query->andFilterWhere(['like', 'message_type', $this->message_type]);
return $dataProvider;
}
}
Form:
<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\jui\DatePicker;
/* @var $this yii\web\View */
/* @var $model app\models\Message */
/* @var $form yii\widgets\ActiveForm */
//echo "";print_r($model);
//echo "";print_r($model->id);exit;
$listData = array('1' => 'Text', '2' => 'Image', '3' => 'audio', '4' => 'video');
$this->registerJsFile("js/script.js");
?>
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
<!--<? = $form->field($model, 'message_type')->textInput() ?>-->
<?= $form->field($model, 'message_type')->dropDownList($listData, array('onchange' => 'getmessagetype(this.value)', 'id' => 'message_type', 'prompt' => 'Select Message Type')); ?>
<?php
if (isset($model->id)) {
if ($model->message_type == 1) {
echo $form->field($model, 'message')->textInput(['maxlength' => true, 'id' => 'text_msg']);
} else {
echo $form->field($model, 'message')->fileInput(['maxlength' => true, 'id' => 'text_msg']);
echo $form->field($model, 'hidden_path')->hiddenInput(['maxlength' => true, 'id' => 'hidden_path', 'name' => 'hidden_path', 'value' => $model->message])->label(false);
echo Html::a('View', Yii::getAlias('@imageUploadUrl') . '/' . $model->message, ['class' => 'btn btn-success', 'target' => '_blank', 'id' => 'view_btn']);
}
} else {
echo $form->field($model, 'message')->textInput(['maxlength' => true, 'id' => 'text_msg']);
}
?>
<?= $form->field($model, 'sch_date')->widget(DatePicker::classname(), ['clientOptions' => ['dateFormat' => 'yy-mm-dd', 'autocomplete' => 'off']]) ?>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>