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

Comments

Projects
None yet
2 participants
@kmergen

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]) ?>

@qiangxue qiangxue closed this in dba7c02 Dec 25, 2013

@kmergen

This comment has been minimized.

Show comment
Hide comment
@kmergen

kmergen 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.

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

This comment has been minimized.

Show comment
Hide comment
@qiangxue

qiangxue Dec 25, 2013

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).

Member

qiangxue commented Dec 25, 2013

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

This comment has been minimized.

Show comment
Hide comment
@kmergen

kmergen 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

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

This comment has been minimized.

Show comment
Hide comment
@qiangxue

qiangxue Dec 25, 2013

Member

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

Member

qiangxue commented Dec 25, 2013

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

@kmergen

This comment has been minimized.

Show comment
Hide comment
@kmergen

kmergen 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.

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

Merge branch 'upstream' into 364-toAscii
* 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 #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 #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