1

Simple question but no solution yet. As we know

<?php $form = ActiveForm::begin(['method'=>'get']); ?>
<?= $form->field($formFilter, 'keyword')
            ->textInput(['placeholder' => \Yii::t('', 'keyword')]); ?>
...

will create simple form and input fields. Of course we will load $_POST data in action like

if ($this->isPost() && $formFilter->load($this->post())) {
        if ($formFilter->validate()) {
...

If we will look in $_POST we will see something like FormFilter[keyword] as name of field. So question is, how can I change it? I need (i think) somehow change in in form\model not in view, because we need proper loading in action.

Where it will be used? Any GET form will show ugly url with class names, for example using simple action and models we will get FormFilter[keyword] but I want change it to keyword, so url will be more understandable than 'long field names'.

Anyone know how to deal with this?

user1954544
  • 1,619
  • 5
  • 26
  • 53

2 Answers2

2

Sorry, later I found solution, I think it will help not just me...

Simple one is to redefine formName() method in our form/model. Using formName() we can even change it what ever we need or disable at all if will set such one

public function formName()
{
    return '';
}

So, if forName() returns empty string we will get url :

http://site/items?keyword=&locationID=&employmentType=&educationLevel=&salaryMin=

Default one will be:

http://site/items?FormVacanciesFilter[keyword]=&FormVacanciesFilter[locationID]=6&FormVacanciesFilter[employmentType]=&FormVacanciesFilter[educationLevel]=&FormVacanciesFilter[salaryMin]=
user1954544
  • 1,619
  • 5
  • 26
  • 53
  • What is the purpose to remove formName container from $_POST vars? In this way you could not use massive assignments from form. – Fabrizio Caldarelli Jun 08 '16 at 21:48
  • I think the main case of use this is in search forms with multiple inputs (example the definition of tire or rim and add this info to url (_GET) so we get clean and human readable URLs – Paweł Liszka Jun 08 '16 at 22:53
  • @FabrizioCaldarelli should be the same as for $_GET one, if you want remove className in $_POST var keys just change your `formName` method in form\model. Search throw form rendering of model (view) and you will see how creates name="fileName" part. – user1954544 Jun 10 '16 at 12:29
  • @PawełLiszka I used to use only $_POST method, where post keys can have ANY names, but not in $_GET... Because user sometimes looks in url. So yes, for $_POST method this feature is useless... ^_^ – user1954544 Jun 10 '16 at 12:31
0

You can change it per field in the view, e.g. I have a form based on yii\base\DynamicModel where I need to control the field names, and, for example:

echo $form->field($model, 'test')->hiddenInput(['name' => 'test'])->label(false);

will output:

<div class="form-group field-dynamicmodel-test">
<input type="hidden" id="dynamicmodel-test" class="form-control" name="test" value="{value of $model->test}">
<p class="help-block help-block-error"></p>
</div>
Rich Harding
  • 645
  • 6
  • 14