user9628338
user9628338

Reputation:

Why Yii2 radioList() creates hidden input?

I'm using radio list in one of my forms, the code that I've written to generate this list is

$form->field($model, 'protection_option')->radioList(array('0'=>'Closed','1'=>'Open'));

When I inspect the output in browser I see a hidden input field with the same name, here is the image of inspector:

enter image description here

Is this behavior valid? If yes then what is the use of hidden field here and if no then please explain what is wrong with my code.

Upvotes: 0

Views: 1033

Answers (1)

rob006
rob006

Reputation: 22174

This field is used to send default value for this radio. On form submit only selected radio will be sent with form - if you does not select anything, radio would be completely ignored (like it was not in the form at all). Thanks to this you can define default value which will be sent in this case - it makes radio behavior similar to other inputs (if you don't fill text input, it still will be send with empty string as value).

You can controll this input using unselect option:

unselect: string, the value that should be submitted when none of the radio buttons is selected. You may set this option to be null to prevent default value submission. If this option is not set, an empty string will be submitted.

https://www.yiiframework.com/doc/api/2.0/yii-helpers-basehtml#activeRadio()-detail

Upvotes: 1

Related Questions