T
T
tincap2015-09-02 11:36:48
Yii
tincap, 2015-09-02 11:36:48

Why doesn't the model see the GET parameter?

There is a model

class SearchForm extends Model
{
    public $age;
}

I send data using the GET method. After passing the data, my URL looks like this
?SearchForm%5Bage%5D=20
And there is a controller that uses this model
$model = new SearchForm();
if ($model->load(Yii::$app->request->get())) {
    die($model->age); // пусто
}

But for some reason the controller doesn't see $model->age. Doesn't see Yii::$app->request->get('age');
Looked at debug. It says SearchForm::age = 20. I can't figure out how to get that damned age out of the url. Yii sees age only if it's set like ?age=20

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
matperez, 2015-09-02
@tincap

Is the age attribute in the validation rules marked safe for mass assignment? Try adding [['age'], 'safe'] to your validation rules.

A
Anton, 2015-09-02
@karminski

Since age is clearly age, it's better then:

public function rules()
{
    return [
        ['age', 'integer', 'min' => 1, 'max' => 100]
    ];
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question