Welcome Guest, Not a member yet? Register   Sign In
Possible bug in set_checkbox() function in form_helper library
#11

[eluser]drewbee[/eluser]
I cannot replicate this issue. When loading default values from a database, you have to do the comparison manually. The third parameter only accepts a TRUE or FALSE to determine if it should be checked intially.

Code:
// $record variable passed in from controller containing record information

<input type="radio" name="group" id="group_1" value="1" <? echo set_radio('group', '1', (($record->group == 1) ? TRUE : FALSE)); ?> />

<input type="radio" name="group" id="group_2" value="2" <? echo set_radio('group', '2', (($record->group == 2) ? TRUE : FALSE)); ?> />

<input type="radio" name="group" id="group_3" value="3" <? echo set_radio('group', '3', (($record->group == 3) ? TRUE : FALSE)); ?> />

this will result in the correct radio being checked based on what is passed from the database, and on form validation errors will display what was correctly checked. Same deal with the checkbox.
#12

[eluser]Cambo[/eluser]
HI drewbee

This is the situation where I had trouble

First, my version:
Code:
// CI Version
define('CI_VERSION',    '1.7.0');

In my View is this:

Code:
<label>&lt;input type="radio" name="leads" value="Y" &lt;?php echo set_radio('leads', 'Y', TRUE); ?&gt; /&gt; YES </label>
<br />
<label>&lt;input type="radio" name="leads" value="N" &lt;?php echo set_radio('leads', 'N'); ?&gt; /&gt; NO</label>

When the page is rendered, I believe that should give me:
Code:
<label>&lt;input type="radio" name="leads" value="Y"  checked="checked" /&gt; YES </label>
<br />
<label>&lt;input type="radio" name="leads" value="N"  /&gt; NO</label>

but what I actually got was:
Code:
<label>&lt;input type="radio" name="leads" value="Y"  checked="checked" /&gt; YES </label>
<br />
<label>&lt;input type="radio" name="leads" value="N"  checked="checked" /&gt; NO</label>

I followed gustavofarias's friends advice and changed both set_checkbox and set_radio in /system/helpers/form_helper.php

FROM:
Code:
function set_checkbox($field = '', $value = '', $default = FALSE)
    {
        $OBJ =& _get_validation_object();

        if ($OBJ === FALSE)
        {
            if ( ! isset($_POST[$field]))
            {
                if (count($_POST) === 0)
                {
                    return ' checked="checked"';
                }
                return '';
            }

TO:
Code:
function set_checkbox($field = '', $value = '', $default = FALSE)
    {
        $OBJ =& _get_validation_object();

        if ($OBJ === FALSE)
        {
            if ( ! isset($_POST[$field])  && $default)
            {
                if (count($_POST) === 0)
                {
                    return ' checked="checked"';
                }
                return '';
            }

Added was && $default in the sixth line.
It then rendered the correct html, and the radio and checkboxes now work as expected.

Hope this helps you to replicate and check if it is a bug.
#13

[eluser]cwt137[/eluser]
Have you guys checked out the repo. recently? They just committed some changes that affect checkboxes. Try it out and see if it solves your problems.
#14

[eluser]e-mike[/eluser]
I had the same problem with no submitted form, the default value is always checked.

(for an array as name > name="options[]")

I have version 1.7.1 and changed the following

Code:
function set_checkbox($field = '', $value = '', $default = FALSE)
{
    $OBJ =& _get_validation_object();

    if ($OBJ === FALSE)
    {
        if ( ! isset($_POST[$field]))
        {
            if (count($_POST) === 0)
            {
                return ' checked="checked"';
            }
            return '';
        }

To the following

Code:
function set_checkbox($field = '', $value = '', $default = FALSE)
{
    $OBJ =& _get_validation_object();

    if ($OBJ === FALSE)
    {
        if ( ! isset($_POST[$field]))
        {
            if ($default)
            {
                return ' checked="checked"';
            }
            return '';
        }

Another thing is that the way set_checkbox works and form_checkbox works is not handy to use them both the same time.

You have to this

Code:
form_checkbox('options[]',$value,false,set_checkbox('options[]',$value,false))

or this

Code:
form_checkbox('options[]',$value,((set_checkbox('options[]',$value)!='')?true:false))

what you want is

Code:
form_checkbox('options[]',$value,set_checkbox('options[]',$value,false))

Does anyone agree?
#15

[eluser]shreder[/eluser]
Hey.

I am using 1.7.1 and have a what seems to be a bug with the set_radio() function.

This is my radio buttons view:
Code:
&lt;?= form_radio('domain', 'mydomain', set_radio('domain','mydomain', TRUE)); ?&gt; I will use my own domain
</p>
<p>
www.&lt;?= form_input('mydomain_base',set_value('mydomain_base')); ?&gt;.&lt;?= form_dropdown('mydomain_tld',$domain_tlds); ?&gt;
</p>
<hr />
<p>
&lt;?= form_radio('domain', 'regdomain', set_radio('domain','regdomain')); ?&gt; I would like you to register a domain for me

The form loads fine with the first radio button correctly checked. The problem is after I submit the form, I get none of the radio buttons checked, both are empty and when viewing the source of the page there isn't even the "checked=checked" in the radio input.

I have tried the fix suggested here but it still does not work.

This is my controller for step1:
Code:
function step1()
    {
        $data['title'] = "Step 1";
        $data['domain_tlds'] = array(
                  'com'   => 'com',
                  'net'   => 'net',
                  'org'   => 'org',
                  'co.il' => 'co.il',
        );

        if($this->input->post('domain') == "mydomain")
        {
            $this->form_validation->set_rules('mydomain_base', 'Your Domain', 'required|min_length[5]');        

        } elseif($this->input->post('domain') == "regdomain") {
            $this->form_validation->set_rules('regdomain_base', 'Registered Domain', 'required|min_length[5]');                
        }
        
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('signup/step1',$data);
        } else {
            $this->session->set_userdata('mydomain_base',$_POST[mydomain_base]);
            $this->session->set_userdata('mydomain_tld',$_POST[mydomain_tld]);
            $this->session->set_userdata('regdomain_base',$_POST[regdomain_base]);
            $this->session->set_userdata('regdomain_tld',$_POST[regdomain_tld]);
            redirect('signup/step2');
        }

    }

Am I doing anything wrong or is this really a bug?

Thanks!
#16

[eluser]alyvest[/eluser]
Quote:if ( ! isset($_POST[$field]))
{
if (count($_POST) === 0 && $default)
{
return ' checked="checked"';
}
return '';
}

It's make my checkbox work ! Thanks
#17

[eluser]alyvest[/eluser]
For the radio buttons, it's work without changing the form helper.
To check, you have to do manually the test :
Yes &lt;input type="radio" name="test" value="1" &lt;?php echo set_radio('test', '1', (boolean)$test == 1); ?&gt; /&gt;
NO &lt;input type="radio" name="test" value="0" &lt;?php echo set_radio('test', '0', (boolean)$test == 0); ?&gt; /&gt;

where $test is your default value
#18

[eluser]Joshua Logsdon[/eluser]
Just for reference for anyone else looking further into the issues:

The /system/libraries/Form_validation.php class seems to have the proper set_* methods.
The /system/helpers/form_helper.php set_* functions are what don't process the $default parameter.

It seems to be a carry-over from the older Validation.php class methods.




Theme © iAndrew 2016 - Forum software by © MyBB