Hello,

I am developing a web site with codeigniter.

I have a form where I have a few group radio buttons.

When validation fails, one group radio keeps its value but the other doesn't.

I can't find why.

My code for the radio that does not work:

<label for="accommodationYes" class="accomm" id="accommY">
    <input name="accommodation" type="radio" id="accommodationYes" style="display:none;" value="Yes"<?php set_radio('accommodation', 'Yes'); ?>>
    <span class="custom radio"></span> Yes
</label>
<label for="accommodationNo" class="accomm" id="accomN">
    <input name="accommodation" type="radio" id="accommodationNo" style="display:none;" value="No"<?php set_radio('accommodation', 'No'); ?>>
    <span class="custom radio"></span> No
</label>

My code for the radio group that does work:

 <label for="eft"><input name="payment" type="radio" id="eft" style="display:none;" value="EFT"<?php echo set_radio('payment', 'EFT'); ?>><span class="custom radio"></span> EFT</label>
 <label for="cash"><input name="payment" type="radio" id="cash" style="display:none;" value="Cash"<?php echo set_radio('payment', 'Cash'); ?>><span class="custom radio"></span> Cash</label>
 <label for="cheque"><input name="payment" type="radio" id="cheque" style="display:none;" value="Cheque"<?php echo set_radio('payment', 'Cheque'); ?>><span class="custom radio"></span> Cheque</label>

My validation code:

$this -> form_validation -> set_rules('accommodation', 'Accommodation', 'required|xss_clean' );     
$this -> form_validation -> set_rules('payment', 'Payment', 'required|xss_clean' ); 

Can someone please help me find the problem?

You are missing the echo in the first group:

<label for="accommodationYes" class="accomm" id="accommY">
<input name="accommodation" type="radio" id="accommodationYes" style="display:none;" value="Yes"<?php echo set_radio('accommodation', 'Yes'); ?>>
<span class="custom radio"></span> Yes
</label>
<label for="accommodationNo" class="accomm" id="accomN">
    <input name="accommodation" type="radio" id="accommodationNo" style="display:none;" value="No"<?php echo set_radio('accommodation', 'No'); ?>>
    <span class="custom radio"></span> No
</label>

EDIT
You don't even imagine what I've done to find this.. I didn't think about echo until I checked all the mechanism.. LOL! I'm pasting my previous text just to let you see how much dumb I'm feeling now x_x :D

========================================================================

It seems a bug but I don't understand why, I made a test and I got your same results, here's my code.

The Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Test extends CI_Controller
{

    public function start()
    {
        $this->load->helper(array('form', 'url'));
        if(validation_errors())
        {
            $error_object =& _get_validation_object();
            $d['errors'] = $error_object;
        }
        else
        {
            $d['errors'] = false;
        }
        $this->load->view('test/start',$d);
    }

    public function form()
    {
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');

        $this->form_validation->set_rules('name', 'Name', 'required|xss_clean');
        $this->form_validation->set_rules('accommodation', 'Accommodation', 'required|xss_clean' );     
        $this->form_validation->set_rules('payment', 'Payment', 'required|xss_clean' ); 
        $this->form_validation->set_error_delimiters('<li>', '</li>');

        if($this->form_validation->run() === FALSE)
        {
            $this->start();
        }
    }
}
The View
<!DOCTYPE html>
<html>
<head></head>
<body>
<?php

    echo form_open('/test/form');
?>
<label for="name">Name <input type="text" name="name" /></label>
<label for="accommodationYes" class="accomm" id="accommY">
    <input name="accommodation" type="radio" id="accommodationYes" value="Yes"<?php set_radio('accommodation', 'Yes'); ?>>
    <span class="custom radio"></span> Yes
</label>
<label for="accommodationNo" class="accomm" id="accomN">
    <input name="accommodation" type="radio" id="accommodationNo" value="No"<?php set_radio('accommodation', 'No'); ?>>
    <span class="custom radio"></span> No
</label>
 <label for="eft"><input name="payment" type="radio" id="eft" value="EFT"<?php echo set_radio('payment', 'EFT'); ?>><span class="custom radio"></span> EFT</label>
 <label for="cash"><input name="payment" type="radio" id="cash" value="Cash"<?php echo set_radio('payment', 'Cash'); ?>><span class="custom radio"></span> Cash</label>
 <label for="cheque"><input name="payment" type="radio" id="cheque" value="Cheque"<?php echo set_radio('payment', 'Cheque'); ?>><span class="custom radio"></span> Cheque</label>
 <input type="submit" name="submit" value="submit" />
</form>
<?php

    # $_POST
    echo '<pre>';
    print_r($_POST);
    echo '</pre>';

    # this will print the validation object
    echo '<pre>';
    print_r($errors);
    echo '</pre>';
?>
</body>
</html>
Output

Now, if you run this:

you end in http://localhost/test/form and get the My_Form_validation Object from which it appears the selected radios:

[_field_data:protected] => Array
    (
        [name] => Array
            (
                [field] => name
                [label] => Name
                [rules] => required|xss_clean
                [is_array] => 
                [keys] => Array
                    (
                    )

                [postdata] => 
                [error] => The Name field is required.
            )

        [accommodation] => Array
            (
                [field] => accommodation
                [label] => Accommodation
                [rules] => required|xss_clean
                [is_array] => 
                [keys] => Array
                    (
                    )

                [postdata] => Yes
                [error] => 
            )

        [payment] => Array
            (
                [field] => payment
                [label] => Payment
                [rules] => required|xss_clean
                [is_array] => 
                [keys] => Array
                    (
                    )

                [postdata] => Cash
                [error] => 
            )

    )

[_config_rules:protected] => Array
    (
    )

[_error_array:protected] => Array
    (
        [name] => The Name field is required.
    )
MY_form_helper.php

So I did a little edit to form_helper.php, I placed MY_form_helper.php inside /application/helpers/ writing this:

<?php

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

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

        $field = $_POST[$field];

        if (is_array($field))
        {
            if ( ! in_array($value, $field))
            {
                return '';
            }
        }
        else
        {
            if (($field == '' OR $value == '') OR ($field != $value))
            {
                return '';
            }
        }

        return ' checked="checked"';
    }

    # this is what I modified:
    $a = $OBJ->set_radio($field, $value, $default);
    $b = array(
        'field'     => $field,
        'value'     => $value,
        'default'   => $default,
        'return'    => $a
    );
    log_message('debug',json_encode($b));
    return $a;
}
End Results

Then I checked the /applications/logs/log-2013-02-01.php and this is the output:

DEBUG - 2013-02-01 16:34:40 --> {"field":"accommodation","value":"Yes","default":false,"return":""}
DEBUG - 2013-02-01 16:34:40 --> {"field":"accommodation","value":"No","default":false,"return":" checked=\"checked\""}
DEBUG - 2013-02-01 16:34:40 --> {"field":"payment","value":"EFT","default":false,"return":" checked=\"checked\""}
DEBUG - 2013-02-01 16:34:40 --> {"field":"payment","value":"Cash","default":false,"return":""}
DEBUG - 2013-02-01 16:34:40 --> {"field":"payment","value":"Cheque","default":false,"return":""}

As you see it returns checked="checked" for the values I tested i.e.: No and EFT and for some reason it doesn't ECHO! O_O

doublefacepalm

OH MY GOD !!!! LOL wow, thank you for taking the time to help, I am so impressed !!!
If you were my neighbour I would buy you a bottle of wine.

And I love the Double Facepalm... just love it! You're a star !!!

You're welcome, hope it was useful.. :D bye!

I am more likely the guy on the left, shifted to face-palming after all the hair were pulled out...oouchhhh that hurts. :)..

Let me do a test also....maybe we can change those faces to a happy face.. :) :)..

commented: lol +0

All is good, echo helped.

Hair is back, nothing to see here...

hi,

can you please marked this solved.. just to let the volunteers know... landed on this page the third time :), and there is nothing to see here as you noted...

thanks,.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.