Yii2 Getting user input from a form

I am doing a search query for a user to find a property in a price range. I dont know how Yii2 gets user input. Here is my code for the form:

  <?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(); ?>    

      

And here is my model:

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;
}
}

      

I added the public $ minprice and $ maxprice when I get this error:

Getting an unknown property: app \ models \ PropertiesSearch :: minprice

However, the request does not work, my url shows user input but the request fails. I would think that ($ model, 'minprice') gives the field a name and that $ this-minprice gets that value. It works when I post $ minprice = '$ 100' and $ maxprice = '$ 300' so they have to overwrite user input, but if I delete them I get the previous error again, what am I doing wrong?

+3


source to share


1 answer


Try entering the propositions $ minprice and $ maxprice in the model app \ models \ Properties and add a validation confirmation.



class Properties extends ActiveRecord
{

 public $minprice;
 public $maxprice;

...

      

0


source







All Articles