Yii2: gii generated code: Html::resetButton not working after first search is performed

Created on 30 May 2015  路  5Comments  路  Source: yiisoft/yii2

I'm reporting a bug.
Problem: Reset button does not clear input fields after first search is performed.

I'm using the advanced template in Yii2 and working on the frontend. I used gii to generate model and crud code for 1 table in MySQL called "Maps" with no further modifications made.

MapsController / index action points to the views/maps/index.php view. In that view file, I uncommented the following code to perform searches on model attributes.

echo $this->render('_search', ['model' => $searchModel]);

The above code enables an ActiveForm in the ( "_search.php") view file with input boxes and a "search" & "reset" buttons. If I enter data into any of the form fields WITHOUT performing a search function, then hit the "reset" button, the form fields will be cleared of any text I put in.

The problem occurs after I enter text, hit the "search" button, then hit the "reset" button. It does NOT clear text in the input boxes.
views/maps/index.php file:

<?php

use yii\helpers\Html;
use yii\grid\GridView;

/* @var $this yii\web\View */
/* @var $searchModel app\models\MapsSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = 'Maps';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="maps-index">

    <h1><?= Html::encode($this->title) ?></h1>
    <?php echo $this->render('_search', ['model' => $searchModel]); ?>

    <p>
        <?= Html::a('Create Maps', ['create'], ['class' => 'btn btn-success']) ?>
    </p>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            'mapID',
            'name',
            'description:ntext',
            'updated',
            'updatedBy',
            // 'make',
            // 'model',
            // 'year',
            // 'eManufacturer',
            // 'eName',
            // 'baffleType',
            // 'eSize',
            // 'isCustomerMap',
            // 'customerID',
            // 'isOriginalMap',
            // 'photoUrl:url',
            // 'created',

            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>

</div>

views/maps/_search.php file:

use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $model app\models\MapsSearch */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="maps-search">

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

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

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

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

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

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

    <?php // echo $form->field($model, 'make') ?>

    <?php // echo $form->field($model, 'model') ?>

    <?php // echo $form->field($model, 'year') ?>

    <?php // echo $form->field($model, 'eManufacturer') ?>

    <?php // echo $form->field($model, 'eName') ?>

    <?php // echo $form->field($model, 'baffleType') ?>

    <?php // echo $form->field($model, 'eSize') ?>

    <?php // echo $form->field($model, 'isCustomerMap') ?>

    <?php // echo $form->field($model, 'customerID') ?>

    <?php // echo $form->field($model, 'isOriginalMap') ?>

    <?php // echo $form->field($model, 'photoUrl') ?>

    <?php // echo $form->field($model, 'created') ?>

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

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

</div>
gii

Most helpful comment

Reloading the page with an action worked for me as a 'get around'.
Use a link, Html::a , instead of Html::resetButton ,with the action ['index'], and retain the class, ['class' => 'btn btn-default'] . Replacement code enclose in php. Html::a('Reset', ['index'], ['class' => 'btn btn-default'])

All 5 comments

More Detail:

screen shot 2015-05-29 at 4 16 25 pm

In the views/maps/index.php file, the input box's name attributes resulting from this code:

echo $this->render('_search', ['model' => $searchModel]);

are the same names for the input boxes when you have the GridView::widget's filterModel enabled.

So there are two inputs with the same names, not sure if that's a problem. But it's definitely interfering with the reset button.

When you type in the input at the top, hit "search" the bottom input box will fill in with the same text you typed in above.

An html reset button sets the value of the input text to the one that had when the page loaded, it doesn't clear the value, see this fiddle.
http://jsfiddle.net/znw0gy15/2/

When you submit the form it is reloaded with the values you entered as the default ones so the reset button will reset them to that value not to empty.

Thanks!

Reloading the page with an action worked for me as a 'get around'.
Use a link, Html::a , instead of Html::resetButton ,with the action ['index'], and retain the class, ['class' => 'btn btn-default'] . Replacement code enclose in php. Html::a('Reset', ['index'], ['class' => 'btn btn-default'])


Was this page helpful?
0 / 5 - 0 ratings

Related issues

gpoehl picture gpoehl  路  47Comments

alexraputa picture alexraputa  路  53Comments

rosancoderian picture rosancoderian  路  46Comments

Faryshta picture Faryshta  路  48Comments

vercotux picture vercotux  路  47Comments