Welcome Guest, Not a member yet? Register   Sign In
set_checkbox() third parameter - ANOTHER form validation bug?
#1

[eluser]slowgary[/eluser]
Hi guys.

I know I'm stupid sometimes but this just draws the line! I've been toying with form validation and everything works except these stupid checkboxes. The validation part is fine, I just can't seem to get the checkboxes unchecked by default. I've searched the forums and have seen that there seems to be some other issues with form validation in regards to radios and checkboxes, but no one mentioned the inability to get them unchecked by default.

So here's my view:
Code:
<input type='checkbox' name='testing' value='dingdong'<?php echo set_checkbox('testing', 'dingdong', FALSE); ?>/>

I'm only talking about the initial page load without a form submission. No matter what I try, the third parameter seems to have no effect. I've tried TRUE, FALSE, 1, 0, and omitting it altogether but this function seems to always want to echo "checked='checked'". What gives? Help a brotha out. Thanks.
#2

[eluser]slowgary[/eluser]
DOH! There I go again being stupid. I was loading the form helper for my initial form display, so CI wasn't throwing an error when I called the set_checkbox() function, but the set_checkbox() function seems to always echo " checked='checked'" unless you've also loaded the form_validation library, which I was NOT loading for my initial form display.

Is this a bug?
#3

[eluser]slowgary[/eluser]
Sorry for the bump guys but this doesn't make sense.

What is the purpose of a set_checkbox() function in the form helper if it only responds to the 3rd parameter when the form validation library is also loaded? IMO this function should not be part of the form helper since it doesn't work. Why is it part of the form helper AND the validation library in the first place?

Apologies if I'm missing something big here.
#4

[eluser]TheFuzzy0ne[/eluser]
[quote author="slowgary" date="1246514710"]Sorry for the bump guys but this doesn't make sense.

What is the purpose of a set_checkbox() function in the form helper if it only responds to the 3rd parameter when the form validation library is also loaded? IMO this function should not be part of the form helper since it doesn't work. Why is it part of the form helper AND the validation library in the first place?
[/quote]

It just happens to be a part of the form_helper which invokes the form_validation library if it's instantiated (I'm sure you can see the connection now). Wink

You can use the function without the form_validation library, and it will grab the results directly from the post array. From my understanding (and I'm sure I might be wrong), if the form validation library is not instantiated, if the post data exists in the $_POST array, it is returned. If the form validation library is instantiated, then the data comes from the form validation library. These appear to be identical, but they are not. There is one major difference that I can see - if the form validation library is instantiated, even if the field exists in the $_POST array, if no rule has been defined for that field, then the default value will be returned instead.

I hope this helps, and I haven't misunderstood your question. You seem very confused, and I think it's contagious (plus it's just too darn hot for my brain to function effectively). If there's anything I've missed, please say so, and I'll be happy to see if I can offer some more answers, since it's a good learning experience for me.
#5

[eluser]slowgary[/eluser]
I dig it, but I'm not sure I posted my point correctly. Set aside the idea of validation or repopulation and consider only the DEFAULT VALUE, which shouldn't require the form_validation library just to check the 3rd parameter. So:
Code:
echo set_checkbox('field', 'value', FALSE);
Field, value, and post data aside, I would expect this function to complete the simple task of checking the 3rd parameter and returning " checked='checked'" if it's true, and nothing if it's not. Unfortunately that's not the case. If you've loaded the form_helper, and the form has not yet been submitted, and this is your code:
Code:
<input type='checkbox' name='lollerskates_jealousy' value='true' <?php echo set_checkbox('lollerskates_jealousy', 'true', FALSE); ?> />
Your checkbox will ALWAYS default to checked. The form helper set_checkbox() ignores the 3rd parameter. As soon as your load the form_validation library, it becomes intelligent again and observes the 3rd parameter, allowing you to default your checkbox to unchecked.

I understand that the form helper and form_validation library go hand in hand, but is there any good reason that the set_checkbox() function from the form helper alone can't check the 3rd parameter and set the checkbox's default without having it's big brother around?

I sure do wish I had some skates Sad
#6

[eluser]TheFuzzy0ne[/eluser]
[quote author="slowgary" date="1246590751"]
Your checkbox will ALWAYS default to checked. The form helper set_checkbox() ignores the 3rd parameter. As soon as your load the form_validation library, it becomes intelligent again and observes the 3rd parameter, allowing you to default your checkbox to unchecked.[/quote]

Aaaaah - the penny drops. Now I understand you! Sorry. The problem was not that you didn't explain clearly, but rather my inept ability to read paragraphs correctly. I think that just might be a bug there. I don't quite follow why it does what it does. I think that:
Code:
if ( ! isset($_POST[$field]))
{
    if (count($_POST) === 0)
    {
        return ' checked="checked"';
    }
    return '';
}

Should just be this:
Code:
if ( ! isset($_POST[$field]))
{
    return '';
}

If the post data doesn't exist, then the box should not be checked, and the function assumes that if the form hasn't been submitted, the box should be checked.

I suspect that if you do something like this:
Code:
$_POST[1] = '';

Then you'll get the result you'd expect, but obviously that's not the natural way to do things.

Very well spotted. I'm surprised it wasn't spotted before. I have only really used set_checkbox once before:
Code:
<input type="checkbox" name="accepted_tos" value="accepted" <?php echo set_checkbox('accepted_tos', 'accepted'); ?> />

but this is not automatically checked. Of course, as you mentioned, this is because the form validation library has been instantiated. My gut tells me this is a bug, but one that you can fix easily by overriding the helper.

As far as I can see, set_select and set_radio share virtually identical code. That should work for them, because radio buttons and select boxes usually contain multiple items, whereas a checkbox should only ever contain a single item. I know that sounds really dodgy, but it's 1AM here, and I'm struggling to find the words I need to explain myself clearly - I may try again in the morning, but I'm pretty sure you'll understand what I'm saying. Essentially, we have a checkbox being passed off as a set of radio buttons. It's almost as if someone copied the code over from set_radio, and forgot to modify it to work with a checkbox. I think this means that we shouldn't even need the whole "is_array" thing.

Here's my proposed replacement, which will probably be very similar to anything you may have already written. It's subject to possible change in the morning.

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

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

        return ($_POST[$field] == $value) ? ' checked="checked' : '';
    }

    return $OBJ->set_checkbox($field, $value, $checked);
}

This code is untested but should work. Yes, it looks somewhat minimalistic, but I think that should work beautifully.

[quote author="slowgary" date="1246590751"]I understand that the form helper and form_validation library go hand in hand, but is there any good reason that the set_checkbox() function from the form helper alone can't check the 3rd parameter and set the checkbox's default without having it's big brother around?[/quote]

Apparently not. Although, looking at the user guide, the documentation doesn't seem to make much sense:

Quote:Permits you to display a checkbox in the state it was submitted. The first parameter must contain the name of the checkbox, the second parameter must contain its value, and the third (optional) parameter lets you set an item as the default (use boolean TRUE/FALSE).

Why on earth would you need to set a default? It's not a set of radio buttons and nor is it a select box...

[quote author="slowgary" date="1246590751"]I sure do wish I had some skates Sad[/quote]

lollerskates are overrated. I'm contemplating upgrading to rocket boots.
#7

[eluser]slowgary[/eluser]
You still might want to default your checkboxes to checked though, so being able to set the default is important. The problem is that the form helper doesn't actually LET you set the default.

It's not a problem for me because I need to load the form_validation library for this anyway, I just happened to have left it out and then proceeded to wrestle with trying to get my checkbox unchecked by default.

I get lost at your code right around line 3 as I'm not originally from the OOP world and my brain starts to fry at all this getting objects by reference. That being said, I think you've mismatched variable names and need some sleep.
Code:
function set_checkbox($field = '', $value = '', $checked = FALSE)
{
    $OBJ =& _get_validation_object();

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

        return ($_POST[$field] == $value) ? ' checked="checked' : '';
    }

    return $OBJ->set_checkbox($field, $value, $checked);
}

I wonder... should this function even BE in the form helper?
#8

[eluser]slowgary[/eluser]
This has actually already been caught by some people.

http://ellislab.com/forums/viewthread/104294

And a bug report has been filed as well.
#9

[eluser]TheFuzzy0ne[/eluser]
You're right, $default is not even defined, and therefore shouldn't have been used.

Yes, I still think this should be in the form helper. Have you tried my proposed replacement (with your suggested change)? You should find it works as you want it to. Whether the form validation object is instantiated or not, it should now respond to the default value.
#10

[eluser]slowgary[/eluser]
I didn't try your solution, sorry. I always have this unsettling feeling about making changes to the core, as I don't want to upset the portability of my applications. I like the idea of just grabbing a new copy of CI and throwing a few MVCs in it without needing to rewrite anything core-related.

It's moot at this point because if the form_validation library is instantiated, the offending bit of code gets skipped anyways and I don't see myseelf needing the helper without the library, as long as I now know the pitfall of not loading the library I think it'll be alright.

Thanks for your expertise though.




Theme © iAndrew 2016 - Forum software by © MyBB