Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unselect value in activeRadioList #1610

Closed
kmergen opened this issue Dec 25, 2013 · 5 comments
Closed

Unselect value in activeRadioList #1610

kmergen opened this issue Dec 25, 2013 · 5 comments

Comments

@kmergen
Copy link

kmergen commented Dec 25, 2013

activeRadioList automatically create a unselect value "0" (hidden field) for a given model attribute.

I think that is not a good solution for a comman usage, because when this attribute is required than the required validator add no error because the value is "0".

in Model rules

 public function rules()
    {
        return [
            ['color', 'required', 'message' => 'Please select a color'],
        ];
    }

That means that I must set the unselect option to null that the required validator works.

<?= $form->field($model, 'color')->radioList($this->context->module->getColors(), ['unselect' => null]) ?>
@kmergen
Copy link
Author

kmergen commented Dec 25, 2013

Thanks.
But what is the benefit to set the unselect value in a radioList.
If you don't set it the required validatator works fine because after a attribute assignment the attribute is null.
And when you want to check the attribute in a controller you can use the isset function.
Only when you need a special value for the unselect attribute, then you can set the unselect option.

@qiangxue
Copy link
Member

The reason is that if you don't select anything, the corresponding form field will not be submitted (if there's no hidden field for the unselect value). As a result, if you are modifying a model, the corresponding field will not be cleared (because it doesn't receive any input for it).

@kmergen
Copy link
Author

kmergen commented Dec 25, 2013

Ok.
I must try this, because normaly when I submit a form I do the following steps in the controller

  1. $model = new Model; (model attributes are cleared, or what do you mean with cleared)
  2. assign submitted attributes to the model $model->load($_POST);
  3. validate the model

@qiangxue
Copy link
Member

For model creation, there's no problem. The problem only happens for updating model.

@kmergen
Copy link
Author

kmergen commented Dec 26, 2013

Ok. You are right.
I think for checkboxes you must have this unselect hidden field, but for a radioList you only need it when you can manipulate the radioList wiht js.
I think the changes from fix #1610 are for all purposes the best.
Thanks.

tonydspaniard added a commit to tonydspaniard/yii2 that referenced this issue Dec 26, 2013
* upstream: (2012 commits)
  doc fix.
  Changed the signature of `urlCreator` and button creators for `yii\gridview\ActionColumn`
  parameter adjustment.
  The signature for `yii\gridview\ActionColumn::urlCreator` is changed - the `$action` parameter is moved to the first
  Fixed the signature of Schema::findUniqueIndexes().
  reverted yiisoft#1598 and added a test for it
  Fix wrong array index in unique indexes for MySql
  Making accesors public
  Get DB unique indexes information
  Fixes yiisoft#1610: `Html::activeCheckboxList()` and `Html::activeRadioList()` will submit an empty string if no checkbox/radio is selected
  Gii should keep horizontal layout
  Documentation at "yii\authclient" updated.
  Doc comments at "yii\authclient" updated.
  Auth clients "Choice" doc comments updated.
  Auth clients "Choice" widget javascript advanced.
  Bootstrap's dropdown encodes also trailing caret tag
  Auth clients "Choice" widget markup updated.
  Gii should keep horizontal layout
  extended from codeception testcase, added docs
  Auth clients for Facebook, GitHub, LinkedIn added.
  ...

Conflicts:
	framework/yii/helpers/BaseInflector.php
	tests/unit/framework/helpers/InflectorTest.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants