wbraganca / yii2-dynamicform

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

Why checkbox/radio values are prefilled with integer values? #73

Open fabioaccetta opened 9 years ago

fabioaccetta commented 9 years ago

Looking at row 72 of yii2-dynamic-form.js I see:

$template.find('input[type="checkbox"], input[type="radio"]').each(function() {
   var inputName = $(this).attr('name');
   var $inputHidden = $template.find('input[type="hidden"][name="' + inputName + '"]').first();
   if ($inputHidden) {
      $(this).val(1); // <=== this row
      $inputHidden.val(0); // <=== and this row
   }
});

In my case radio inputs as textual values and not numerical ones, i.e.:

$options = [
   'type1' => 'Red',
   'type2' => 'Green',
   'type3' => 'Blue',
];
echo $form->field($model, 'type')->radioList($options);
?>

In my case, those ones break my form, because the output I get is:

<label class="control-label" for="color-type">Type</label>
<input name="Color[type]" value="0" type="hidden">
[...]
<label>
   <input name="Color[type]" value="1" type="radio"> Red
</label>
[...]
<label>
   <input name="Color[type]" value="1" type="radio"> Green
</label>
[...]
<label>
   <input name="Color[type]" value="1" type="radio"> Blue
</label>

instead of

<label class="control-label" for="color-type">Type</label>
<input name="Color[type]" value="" type="hidden">
[...]
<label>
   <input name="Color[type]" value="type1" type="radio"> Red
</label>
[...]
<label>
   <input name="Color[type]" value="type2" type="radio"> Green
</label>
[...]
<label>
   <input name="Color[type]" value="type3" type="radio"> Blue
</label>

So, why those rows are necessary? Thanks

fabioaccetta commented 9 years ago

Moreover, if I comment those lines, my output has radio inputs always with empty values instead of their right own values. This strange behavior seems to happen in line 59:

var $template = $(widgetOptions.template);

Even if widgetOptions.template (a text variable that contains html) has correct values in radio inputs, $template has all of these fields with empty values. In fact, if I continue in the same function with part of those commented rows:

$template.find('input[type="checkbox"], input[type="radio"]').each(function() {
   var value = $(this).val();
   alert(value);
});

I get always empty values instead of 'type1', 'type2', or 'type3'.

I can't understand way...

fabioaccetta commented 9 years ago

The version considered in my previous post is the latest stable, while in the last master version this problem seems to be resolved (even if the radio is always prefilled with 1).

The problem still exists, because _fixFormValidaton (row 298) fails trying to fix radio/check validation;

$(widgetOptionsRoot.widgetBody).find('input, textarea, select').each(function() {
   var id   = $(this).attr('id');
   var name = $(this).attr('name');

   if (id !== undefined && name !== undefined) {
[...]

In case of radio/checkbox list, last if fails because the input generated by field() method has no id and the expected id is owned by the div surrounding the radio/checkbox list.

smanikandan-btech commented 9 years ago

@fabioaccetta good catch. I too facing same problem. Is is possible to generate id attribute for radio buttons?

smanikandan-btech commented 9 years ago

For adding id attribute to the radio buttons I used below code.

<?= $form->field($client_allow_acces, "[$i]access_type")->radioList([1 => 'Allow access', 2 => 'Can\'t allow access'],[ 'item' => function($index, $label, $name, $checked, $value) {
                                    $return = '<label class="modal-radio">';
                                    $custom_id = preg_replace("/[^a-zA-Z0-9]+/", "-", strtolower($name)).$value;
                                    $return .= '<input type="radio" name="' . $name . '" value="' . $value . '" id="'.$custom_id.'" >';
                                    $return .= '<span>  ' . ucwords($label) . '</span>';
                                    $return .= '</label>';
                                    return $return;
                             }]); ?>

And also for fixing the radio buttons value issue I modified the package js file vendor/wbraganca/yii2-dynamicform/assets/yii2-dynamic-form.js on lines 74 and 77

$template.find('input[type="checkbox"], input[type="radio"]').each(function() {
            var inputName = $(this).attr('name');
            var inputID = '#'+$(this).attr('id'); //added newly
            var $inputHidden = $template.find('input[type="hidden"][name="' + inputName + '"]').first();
            if ($inputHidden) {
                $(this).val($(inputID).val()); //modified
                $inputHidden.val(0);
            }
        });
skySlec commented 7 years ago
        // $template.find('input, textarea, select').each(function() {
        $template.find("input:not([type='radio'], [type='checkbox']), textarea, select").each(function() {
            $(this).val('');
        });

        if ($inputHidden) {
                // $(this).val(1);
                $inputHidden.val(0);
        }

This ?

smanikandan-btech commented 7 years ago

@skySlec actually you are making static values. May I know what issue you are facing?

charmy93 commented 5 years ago

Has anyone found the solution for this problem, as i am currently facing the same and not sure what the solution is. I would like the radio list to use values that are provided in the array items but looks like widget is giving a static value of 1 to all of the items

<?= $form->field($carrierProfileVendor,"[{$index}]" . 'pulldata') ->radioList([ '1' => 'Yes', '0' => 'No', '-1' => 'On Hold' ])->label('Pulling Satellite Tracking Data:')?>

Instead of giving value = 0 to radio button 'No' it contains value of 1 and same goes for 'On Hold' radio button