You are viewing revision #8 of this wiki article.
This is the latest version of this article.
You may want to see the changes made in this revision.
Problem Statement ¶
You are using an ActiveForm in yii (both v1.x or 2), with a GET method like below:
// In Yii 2
$form = ActiveForm::begin([
'id' => 'order-search-form',
'method' => 'get',
]);
echo $form->field($searchModel, 'id');
echo Html::submitButton('Find', ['class' => 'btn btn-primary']);
ActiveForm::end();
// In Yii 1.x
$form = $this->beginWidget('CActiveForm', [
'id' => 'order-search-form',
'method' => 'get',
]);
echo $form->textInput($searchModel, 'id');
echo CHtml::submitButton('Find', ['class' => 'btn btn-primary']);
$this->endWidget();
On form submission you encounter this situation:
- Initial uri is
order/index
. - After submitting form with
id=111
your URL readsorder/index?id=111
. - After submitting form again your URL reads
order/index?id=111&id=111
... and so on. The id parameter above keeps on stacking repeatedly, and is not the correct way you want to be submitting a form with GET method.
Explanation ¶
The reason for the above behavior is due to a unique validation needed in Yii's Html::beginForm
method. The query parameters in the action are ignored for GET method so Yii's Html::beginForm
uses hidden inputs for each GET parameter to add them back. These hidden inputs gets stacked over the last URI call with every submission.
Solution ¶
To avoid repeated parameter stacking, ensure you pass the action method while defining your form like below:
// In Yii 2
$form = ActiveForm::begin([
'method' => 'get',
'action' => Url::to(['order/index']),
]);
// In Yii 1.x
$form = $this->beginWidget('CActiveForm', [
'method' => 'get',
'action' => $this->createUrl('order/index'),
]);
This will automatically use the action URI set above for each form submission rather than relying on the URL from the address bar. Thus the URL parameter repetition and stacking will not happen.
The same applies to Yii 1.1
For Yii 1.1
$form = $this->beginWidget('CActiveForm', [ 'method' => 'get', 'action' => $this->createUrl('order/index'), ]);
Updated for both versions
@toph - thanks - updated wiki for both yii versions
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.