Answer the question
In order to leave comments, you need to log in
Search form output in yii2 template?
Goodnight.
We need to organize a search on the site. The form should be located on all pages of the site.
I made a separate file with the form, which I include in layouts/main.
I can't figure out how to correctly pass the search model to the form.
Form file (shortened form code)
<?php $form = ActiveForm::begin([
'method' => 'get',
'action' => ['/search-flights'],
]) ?>
<div class="secondRow clearfix">
<?= $form->field($model, 'city_from')->widget(Select2::className())->label(false) ?>
<?= $form->field($model, 'date_from')->widget(DatePicker::className())->label(false) ?>
<?= Html::input('submit',null,'Найти билеты', ['class' => 'peopleSubmit']) ?>
</div>
<?php ActiveForm::end() ?>
class SearchController extends Controller
{
public function actionSearchFlights()
{
$searchModel = new FlightsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('search-flight', [
'dataProvider' => $dataProvider,
]);
}
}
<?php
namespace app\modules\main\models\search;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\modules\airlines\models\Flights;
class FlightsSearch extends Model
{
public $city_from;
public $date_from;
public static function tableName()
{
return '{{%flights}}';
}
public function rules()
{
return [
['date_from','city_from'], 'required'],
['city_from', 'integer'],
['date_from', 'date', 'format' => 'php:d.m.Y' ],
];
}
public function attributeLabels()
{
return [
'city_from' => Yii::t('module', 'SEARCH_CITY_START'),
'date_from' => Yii::t('module', 'SEARCH_DATE_START'),
];
}
public function search($params)
{
$query = Flights::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if(!$this->validate()){
/*$query->where('0=1');*/
return $dataProvider;
}
$query->andFilterWhere(['>=', 'date_start', $this->date_from ? strtotime($this->date_from . ' 00:00:00') : null])
->andFilterWhere(['=', 'city_start_id', $this->city_from])
return $dataProvider;
}
}
<?= $this->render('includes/_header', ['model' => new FlightsSearch()]); ?>
When creating widgets, you should adhere to the basic principles of the MVC concept. In general, the main logic should be located in the widget class, while separating the code responsible for the markup into views.
Answer the question
In order to leave comments, you need to log in
It makes sense to make a widget if the form needs to be parameterized, while the model can also be transferred to it.
But in your case, it is more logical to get the model directly in the view with the form
<?php
$model = new FlightsSearch();
$form = ActiveForm::begin([
'method' => 'get',
'action' => ['/search-flights'],
]) ?>
<div class="secondRow clearfix">
<?= $form->field($model, 'city_from')->widget(Select2::className())->label(false) ?>
<?= $form->field($model, 'date_from')->widget(DatePicker::className())->label(false) ?>
<?= Html::input('submit',null,'Найти билеты', ['class' => 'peopleSubmit']) ?>
</div>
<?php ActiveForm::end() ?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question