Welcome Guest, Not a member yet? Register   Sign In
checkbox issue
#1

[eluser]robert.fulcher[/eluser]
I have been looking at this for a few hours and have searched a lot and can't figure out what is going on.

I am using the form helper and validation in conjunction. I can't get a checkbox to work correctly.

view
Code:
option 1

<?php echo  form_checkbox('billable','1',set_checkbox('billable','1')); ?>

option 2

<?php echo form_checkbox('billable','1',set_checkbox('billable',$note->billable)); ?>

option 3

<input type="checkbox" name="billable" value="1" <?php echo set_checkbox('billable','1'); ? />

option 4

<input type="checkbox" name="billable" value="1" <?php echo set_checkbox('billable',$note->billable);?> />

options 5


<?php echo form_checkbox('billable','1', $note->billable); ?>

Option 5 is the only one that works but now I am out of validateion. Which for some reason I can only get my validation on the add part of this form to work the 1st time. the second time somone submits with an error it will not remember that they check the field.

I also know for sure there is either a 0 or a 1 in the billable field because I echo it out later down the form just to check.

Any help is greatly appreciated.
#2

[eluser]Tim Reynolds[/eluser]
I am am having the exact debate on the best way to reset check boxes while using validation and the form helper. Can anyone help?
#3

[eluser]TheFuzzy0ne[/eluser]
I don't think set_checkbox() is meant to be used with the form_checkbox helper, but rather when you're actually writing the HTML yourself. I'd recommend you try using set_value() as the third parameter for the form_checkbox() helper.
#4

[eluser]Tim Reynolds[/eluser]
I have just solved this issue by doing,


echo form_checkbox('active', '1', $this->input->post('active'));

This probably only works as I have set the value to 1.
#5

[eluser]TheFuzzy0ne[/eluser]
Try this:
Code:
echo form_checkbox('active', 1, set_value('active'));
#6

[eluser]richwalkup[/eluser]
The real problem is that there is a strange bit of code within the Form Helper & Form Validation library. For some reason if you are using both the Form Helper and Form Validation (who doesn't?) together, the set_checkbox and set_radio methods within the helper use the active validation library set_checkbox and set_radio methods. If you do not have an empty validation rule set for the field, it completely disregards it altogether and returns an empty string. Check out this forum post:

http://ellislab.com/forums/viewthread/105849/
#7

[eluser]robert.fulcher[/eluser]
Thanks for all of your help in this. I am still not 100% sure how these work but I did get this situation to work the way it should.

Code:
<?php
            $attributes_billable = array(
                'data'    => 'billable',
                'class' => 'textinput',
                'name'  => 'billable',
                'value' => '1',
                'checked'    =>    set_value('billable', $todoaction->billable)
            );
            echo form_checkbox($attributes_billable);
        ?>

Any critique of the code is welcome. I want to learn how to do this right.

Thanks
#8

[eluser]Thorpe Obazee[/eluser]
There's nothing wrong with it. Especially if it already works Smile
#9

[eluser]Thorpe Obazee[/eluser]
[quote author="robert.fulcher" date="1245128904"]Thanks for all of your help in this. I am still not 100% sure how these work but I did get this situation to work the way it should.

Code:
<?php
            $attributes_billable = array(
                'data'    => 'billable',
                'class' => 'textinput',
                'name'  => 'billable',
                'value' => '1',
                'checked'    =>    set_value('billable', $todoaction->billable)
            );
            echo form_checkbox($attributes_billable);
        ?>

Any critique of the code is welcome. I want to learn how to do this right.

Thanks[/quote]

There's nothing wrong with it. Especially if it already works Smile

'checked' will equate to TRUE if there's a value given by the set_value helper.
#10

[eluser]robert.fulcher[/eluser]
Quote:'checked' will equate to TRUE if there's a value given by the set_value helper.

In my case I am saving an empty value (not 0 or NULL). empty is evaluated as not having a value. At leas that is how I percieve it to work. Is there a better way?

Thanks




Theme © iAndrew 2016 - Forum software by © MyBB