Hidden Inputs In Activeform

When I use ActiveForm widget with method=get, parameters from $_GET appears inside form tag as hidden inputs. Is it a bug? How to get rid of these inputs?

Search model:


class OrderSearch extends Order

{

    public function formName()

    {

        return '';

    }


    public function rules()

    {

        return [

            [['id'], 'integer'],

        ];

    }


    public function search($params)

    {

        $query = Order::find();

        if (!($this->load($params) && $this->validate())) {

            return $query;

        }

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

        return $query;

    }

}

Controller action:


    public function actionIndex()

    {

        $searchModel = new OrderSearch();

        $dataProvider = new ActiveDataProvider([

            'query' => $searchModel->search($_GET),

        ]);

        return $this->render('index', ['dataProvider' => $dataProvider, 'searchModel' => $searchModel]);

    }

View:


    <?php $form = ActiveForm::begin([

        'id' => 'order-search-form',

        'method' => 'get',

    ]) ?>

        <?= $form->field($searchModel, 'id') ?>

        <?= Html::submitButton('Find', ['class' => 'btn btn-primary']) ?>

    <?php ActiveForm::end() ?>

Resulting form HTML:


<form id="order-search-form" action="/order/index" method="get">

    <input type="hidden" name="id" value="111">

    <div class="form-group field-id">

        <label class="control-label" for="id">Number</label>

        <input type="text" id="id" class="form-control" name="id" value="111">

        <div class="help-block"></div>

    </div>

    <button type="submit" class="btn btn-primary">Find</button>

</form>

1 Like

Never experienced this.

Are you by chance using any extension, jquery plugin, or widget on the same page?

no it is not an bug and in yii1 this is what happening the get params are written in hidden field for example the route param r is written in hidden field

Kartik V, no, I don’t use any extensions, plugins or another widgets on this page.

Ahamed Rifaideen, if there is no way to turn off this behavior, it IS a bug. The problem of this issue is in repeating get-params after each form submitting:

Initial uri is "order/index". After submitting with id=111 I get "order/index?id=111". After submitting again I get "order/index?id=111&id=111" etc.

Found a solution: https://github.com/yiisoft/yii2/issues/3353

Great… I missed the GET method part earlier in your active form call. Thanks for sharing. Will create a wiki for this - so its useful.

It’s feature of Yii, coz query parameters in the action are ignored for GET method, so Yii adds hidden fields to add them back.

You need specify action without GET params.
Doing something will solve your problem:

$form = ActiveForm::begin([
‘method’ => ‘get’,
‘action’ => Url::current([$model->formName() => null]),
]);