Welcome Guest, Not a member yet? Register   Sign In
help with set_checkbox function
#1

[eluser]Unknown[/eluser]
What in the world am I doing wrong?! Following the user guide... when there are validation errors none of the check boxes repopulate... I am new to CI and just trying out the form validation class, tinkered with it for hours and exhausted every possibility, it is probably something simple that will make me look like a dufus.. but whatever just want it to work...

here is my controller code for checkboxes...

Code:
#Checkbox attributes
        $data['first_option'] = array('name' => 'seminar[]', 'id' => 'Option_1', 'value' => '1', 'checked' => FALSE);
        $data['second_option'] = array('name' => 'seminar[]', 'id' => 'Option_2', 'value' => '2', 'checked' => FALSE);
        $data['third_option'] = array('name' => 'seminar[]', 'id' => 'Option_3', 'value' => '3', 'checked' => FALSE);
        $data['fourth_option'] = array('name' => 'seminar[]', 'id' => 'Option_4', 'value' => '4', 'checked' => FALSE);

Here is what it is in the view

Code:
<p>&lt;?php echo form_checkbox($first_option, set_checkbox('seminar', '1')); ?&gt; <label for="first_option">Option 1</label></p>
  <p>&lt;?php echo form_checkbox($second_option, set_checkbox('seminar', '2')); ?&gt; <label for="second_option">Option 2</label></p>
  <p>&lt;?php echo form_checkbox($third_option, set_checkbox('seminar', '3')); ?&gt; <label for="third_option">Option 3</label></p>
  <p>&lt;?php echo form_checkbox($fourth_option, set_checkbox('seminar', '4')); ?&gt; <label for="fourth_option">Option 4</label></p>

anyone see any red flags? I have input fields and the set_value() is working just fine. Just cant seem to get it working with checkboxes...
#2

[eluser]darkhouse[/eluser]
Don't you need to do set_checkbox("seminar[]", ... noting the []'s. I don't use the form helper functions much, but from what I remember from reading the docs, I thought you needed those for set_checkbox to work correctly.
#3

[eluser]darkhouse[/eluser]
I just read the docs, check out Class Reference -> Form Validation Class and go right to the bottom of the page. That's exactly how you set that, at least according to the docs.
#4

[eluser]Unknown[/eluser]
I tried it both ways... still didn't work, however I did manage to get to work without using the form helper... It still wouldn't repopulate unless I attempted to validate it... I wanted it to repopulate without havent to set any validation rules, which i still had to do this...

Code:
$this->form_validation->set_rules('test', 'Test', '');

which looked like some pretty dookie code...

and this in the view...

Code:
<p>&lt;input type="checkbox" name="test[]" value="one" &lt;?= set_checkbox('test', 'one') ?&gt;/&gt; <label for="one">one</label></p>
  <p>&lt;input type="checkbox" name="test[]" value="two" &lt;?= set_checkbox('test', 'two') ?&gt;/&gt; <label for="two">two</label></p>

There has to be a better solution...




Theme © iAndrew 2016 - Forum software by © MyBB