wbraganca / yii2-dynamicform

It is widget to yii2 framework to clone form elements in a nested manner, maintaining accessibility.
Other
436 stars 439 forks source link

radio list display not checked but it is checked #217

Closed RookieLittle closed 6 years ago

RookieLittle commented 7 years ago

in the picture below is update view the radio button is not checked in the display,

image

but when u inspect element u will found that it actually checked (like the picture below).

image

it only happened on radio list the dropdown list is work perfectly, anyone have the same issue or anyone did use the radiolist but it dont have such issue?

gladx commented 7 years ago

hi @RookieLittle , Inspect Element make difference code, please screen-shot from source code (View Page Source in Firefox ). maybe this snippet code help you :

<?= $form->field($model, 'color')->radioList(
        ["white"=> "White", "blue"=> 'Blue', "red"=> 'Red',"black"=> 'Black'], 
        [
            'item' => function($index, $label, $name, $checked, $value) {

                $return = '<label class="modal-radio">';
/* ----->*/     if($checked ) $return .= '<input checked  type="radio" name="' . $name . '"value="' . $value . '" tabindex="3">';
/* ----->*/     else          $return .= '<input  type="radio" name="' . $name . '"value="' . $value . '" tabindex="3">';
                $return .= '<i ></i>';
                $return .= '<span style="background: '. $value .'">'.  ucwords($label) . '</span>';
                $return .= '</label>';

                return $return;
            }
        ]
    ) ?>
RobinKamps commented 7 years ago

"checked" is automatically removed from template in method _parseTemplate in yii2-dynamic-form.js. Comment out the following lines, to see it in action:

if ($inputHidden && count === 1) {
     $(this).val(1);
     $inputHidden.val(0);
 }

$(this).prop('checked', false);

This should work in most cases to preselect values on create. On update, this will not work properly, because the first element will be cloned and therefore the selected value of the first element will be selected in the clone too (hence the general deletion of preselections as a default behavior). However you can just add some jquery expressions tailored to your specifications etc. in a method beeing called on add Item (restorespecialjs e.g.) which selects the new clone and always gets the desired radio checked