CodeIgniter Forums
Found a Bug with the "set_radio()" function - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: Found a Bug with the "set_radio()" function (/thread-77725.html)



Found a Bug with the "set_radio()" function - mlurie - 10-11-2020

There is a strange bug with the set_radio() function.  I have two sets of radio bullets.  Lets call them $set1 and $set2.  Each set is made up of two radio bullets where the value is either 1 for "yes" or 0 for "no". 

If the form initially loads with no radio bullets marked as checked set_radio() works just fine.  If validation fails, the form remembers if "yes" or "no" is checked for $set1 and $set2. If the values are changed to from "yes" to "no" or vice-versa, it works even after multiple forced validation failures and multiple radio bullet selections.

The sames follows if the form initially loads with the "yes" radio bullets marked as checked.  Each set remembers the POST value after validation fails when the form reloads. If the values are changed to from "yes" to "no", it works. If the values are changed back to "yes" from "no" after a validation failures, it works.

This is where it gets weird.  If the form initially loads with the "no" radio bullets already marked as checked, set_radio() no longer works correctly.  The set value always defaults to "no". If the values are changed from "no" to "yes" and a validation error occurs when the form is submitted, the radio bullets flip back to "no" when the form view loads after the validation failure.  set_radio() always gets a value of 0 when 0 is the initial checked value.

No, I did not use the optional third parameter to set a default value.

I've included some code from my view.  Please let me know if I am missing something.

Code:
            <div class="form-row">
                <div class="form-group col-6">
                    <legend class="col-form-label pt-0" style="font-weight: 600;">HVAC Installed:</legend>
                    <div class="form-check form-check-inline">
                    <?php
                        $attributes = [
                            'name' => 'hvac',
                            'id' => 'hvac_yes',
                            'class' => 'form-check-input' . is_invalid($errors['hvac']),
                            //'required' => 'required',
                        ];
                        echo form_radio($attributes, '1', ($unit->hvac == '1' ? TRUE : FALSE), set_radio('hvac', '1'));
                        $attributes = [
                            'class' => 'form-check-label',
                        ];
                        echo form_label('Yes', 'hvac', $attributes);
                    ?>
                    </div>
                    <div class="form-check form-check-inline">
                    <?php
                        $attributes = [
                            'name' => 'hvac',
                            'id' => 'hvac_no',
                            'class' => 'form-check-input' . is_invalid($errors['hvac']),
                            //'required' => 'required',
                        ];
                        echo form_radio($attributes, '0', ($unit->hvac == '0' ? TRUE : FALSE), set_radio('hvac', '0'));
                        $attributes = [
                            'class' => 'form-check-label',
                        ];
                        echo form_label('No', 'hvac', $attributes);
                    ?>
                    </div>
                    <?= invalid_feedback($errors['hvac']) ?>
                </div>
                <div class="form-group col-6">
                    <legend class="col-form-label pt-0" style="font-weight: 600;">3ϕ Power Installed:</legend>
                    <div class="form-check form-check-inline">
                    <?php
                        $attributes = [
                            'name' => 'three_phase',
                            'id' => 'three_phase_yes',
                            'class' => 'form-check-input' . is_invalid($errors['three_phase']),
                            //'required' => 'required',
                        ];
                        echo form_radio($attributes, '1', ($unit->three_phase == '1' ? TRUE : FALSE), set_radio('three_phase', '1'));
                        $attributes = [
                            'class' => 'form-check-label',
                        ];
                        echo form_label('Yes', 'three_phase', $attributes);
                    ?>
                    </div>
                    <div class="form-check form-check-inline">
                    <?php
                        $attributes = [
                            'name' => 'three_phase',
                            'id' => 'three_phase_no',
                            'class' => 'form-check-input' . is_invalid($errors['three_phase']),
                            //'required' => 'required',
                        ];
                        echo form_radio($attributes, '0', ($unit->three_phase == '0' ? TRUE : FALSE), set_radio('three_phase', '0'));
                        $attributes = [
                            'class' => 'form-check-label',
                        ];
                        echo form_label('No', 'three_phase', $attributes);
                    ?>
                    </div>
                    <?= invalid_feedback($errors['three_phase']) ?>
                </div>
            </div>

I guess this has already been reported. I just found this on GitHub via Google: https://github.com/codeigniter4/CodeIgniter4/issues/2728.  It says this has been fixed, but I'm using 4.0.4 and still having the same issue.