Why `set_radio` doesn't work when “form_valida

2019-07-30 10:06发布

I am calling a view

function index() {
    $this->load->helper("form");
    $this->load->library("form_validation");

    $this->load->view("index");
}

And then I have

<?php echo form_open(); ?>

<input type="radio" name="radioname" value="x" <?php echo set_radio("radioname", "x", true); ?> />
<input type="radio" name="radioname" value="y" <?php echo set_radio("radioname", "y"); ?> />

</form>

At the first load I get the first radio checked. If I post the form the value goes successfully to $this->input->post(). But none of the radio boxes get checked. If I don't load the validation the code works.

I dug the code and found that if the form_validation is loaded it behaves differently.

$OBJ =& _get_validation_object();

if ($OBJ === FALSE)
{
    // returns formhelper set_radio
}

// this doesn't make sense for me
return $OBJ->set_radio($field, $value, $default);

If the form_validation is loaded it executes the set_radio from $OBJ, but it doesn't work.

What exactly is $OBJ in this context? What do I have to change to make it work?

1条回答
走好不送
2楼-- · 2019-07-30 10:43

You need to add fake rule to radioname like

$this->form_validation->set_rules("radioname", "", "trim");
查看更多
登录 后发表回答