Yii2从表单获取用户输入

时间:2015-04-25 10:43:35

标签: php forms input yii2

我正在为用户创建搜索查询以查找价格范围内的属性。我不知道Yii2如何获得用户输入。这是我的表格代码:

  <?php $form = ActiveForm::begin([
    'action' => ['index'],
    'method' => 'get',
]); ?>

<?= $form->field($model, 'address') ?>

<?= $form->field($model, 'minprice')->dropDownList(['£100' => '£100','£200' => '£200','£300' => '£300']) ?>

 <?= $form->field($model, 'maxprice')->dropDownList(['£100' => '£100','£200' => '£200','£300' => '£300']) ?>

<div class="form-group">
    <?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
    <?= Html::resetButton('Reset', ['class' => 'btn btn-default']) ?>
</div>

<?php ActiveForm::end(); ?>    

这是我的模特:

class PropertiesSearch extends Properties
{

 public $minprice;
 public $maxprice;

public function search($params)
{
    $query = Properties::find();

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);

    $this->load($params);

    if (!$this->validate()) {
        return $dataProvider;
    }

    $query->andFilterWhere([
        'id' => $this->id,
        'NoofBedrooms' => $this->NoofBedrooms,
        'type_id' => $this->type_id,
    ]);

    $query->andFilterWhere(['like', 'address', $this->address])
        ->andFilterWhere(['like', 'city', $this->city])
        ->andFilterWhere(['like', 'Postcode', $this->Postcode])
        ->andFilterWhere(['>=', 'price', $this->minprice])
        ->andFilterWhere(['<=', 'price', $this->maxprice])
        ->andFilterWhere(['=', 'option', $this->option])
        ->andFilterWhere(['like', 'description', $this->description]);
   return $dataProvider;
}
}

我在收到此错误时添加了公共$ minprice和$ maxprice:

获取未知属性:app \ models \ PropertiesSearch :: minprice

但是,查询无效,在我的网址中显示用户输入,但查询未完成。我原以为($ model,&#39; minprice&#39;)给该字段命名,$ this-minprice获得该值。当我公开$ minprice =&#39; $ 100&#39;和$ maxprice =&#39; $ 300&#39;,所以他们必须覆盖用户输入,但如果我删除它们我再次得到上一个错误,我做错了什么?

1 个答案:

答案 0 :(得分:0)

尝试在模型app \ models \ Properties中放置propetries $ minprice和$ maxprice 并添加条件验证。

class Properties extends ActiveRecord
{

 public $minprice;
 public $maxprice;

...