checkbox in custom component

For Joomla! 1.5 Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general
Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
geenzelf
Joomla! Apprentice
Joomla! Apprentice
Posts: 7
Joined: Tue Apr 14, 2009 1:32 pm

checkbox in custom component

Postby geenzelf » Tue Apr 14, 2009 2:21 pm

I started building my own joomla component in component.php & component.html.php style (not mvc)

I'd like to transform my form to a custom Joomla component and everything works fine, except for the checkboxes.

The problem: If the checkbox is checked in the add-function and I go to the edit-function, uncheck the checkbox. I stays checked. Below is my code. I can understand the it stays checked with my code, but can't think of a solution, without using javascript(don't have any javascript knowledge).

Hopefully someone can help.


<tr>
<td><label for="label">label</label></td>
<td><input id="label" name="energielabel" type="checkbox" value="1"<?php if($task = 'edit' and $row->label == '1'){echo 'checked';}?> ></td>
</tr>

jnodwell
Joomla! Ace
Joomla! Ace
Posts: 1284
Joined: Thu Jul 10, 2008 2:51 am
Contact:

Re: checkbox in custom component

Postby jnodwell » Tue Apr 14, 2009 2:31 pm

$task = 'edit' should be $task == 'edit'

The way you have it, you are saying set $task equal to 'edit' (which always returns true as the assignment of value never fails)
http://nodwell.net
http://cjeweb.com
Custom Web Development & Joomla! Templates

geenzelf
Joomla! Apprentice
Joomla! Apprentice
Posts: 7
Joined: Tue Apr 14, 2009 1:32 pm

Re: checkbox in custom component

Postby geenzelf » Tue Apr 14, 2009 3:12 pm

you are right! Thanks.

I still have the problem that unselecting a checkbox saving it, doesn't put the value to 0 in my db.

jnodwell
Joomla! Ace
Joomla! Ace
Posts: 1284
Joined: Thu Jul 10, 2008 2:51 am
Contact:

Re: checkbox in custom component

Postby jnodwell » Tue Apr 14, 2009 5:28 pm

value="1"

You set the value of it to 1. It's always going to be 1 (or null if it is not checked).
http://nodwell.net
http://cjeweb.com
Custom Web Development & Joomla! Templates

geenzelf
Joomla! Apprentice
Joomla! Apprentice
Posts: 7
Joined: Tue Apr 14, 2009 1:32 pm

Re: checkbox in custom component

Postby geenzelf » Tue Apr 14, 2009 5:49 pm

I know what you mean, but what would be a solution?

I just enabled tamper data in my firefox browser. And if I select a checkbox it is send by $_POST, but if I unselect a checkbox it will not be send by $_POST.

jnodwell
Joomla! Ace
Joomla! Ace
Posts: 1284
Joined: Thu Jul 10, 2008 2:51 am
Contact:

Re: checkbox in custom component

Postby jnodwell » Tue Apr 14, 2009 7:44 pm

Yes, that's correct behavior. So, check each checkbox in $_POST - if the value is 1, you know it's checked, and if it is '' (null) you know it wasn't.
http://nodwell.net
http://cjeweb.com
Custom Web Development & Joomla! Templates

User avatar
ianmac
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 4785
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

Re: checkbox in custom component

Postby ianmac » Wed Apr 15, 2009 3:50 am

jnodwell wrote:Yes, that's correct behavior. So, check each checkbox in $_POST - if the value is 1, you know it's checked, and if it is '' (null) you know it wasn't.

As Jennifer has said, the item will only be sent if the checkbox is selected.

This has always made the checkbox seem to me an odd and difficult form element type because you often have to use special logic to decode it as opposed to other form types where you can generally count on the variable being there in the request.

Ian

geenzelf
Joomla! Apprentice
Joomla! Apprentice
Posts: 7
Joined: Tue Apr 14, 2009 1:32 pm

Re: checkbox in custom component

Postby geenzelf » Wed Apr 15, 2009 6:36 am

So checkboxes are not done if I understand it right?

There is no simple solution so I could better choose a dropdown with yes/no or something like it?

jnodwell
Joomla! Ace
Joomla! Ace
Posts: 1284
Joined: Thu Jul 10, 2008 2:51 am
Contact:

Re: checkbox in custom component

Postby jnodwell » Wed Apr 15, 2009 10:39 am

I don't see how it's not simple. Check for the variable, if it exists, it's set. If it doesn't, it's not set.

If it's easier to conceptualize for you, use a dropdown. Sure.
http://nodwell.net
http://cjeweb.com
Custom Web Development & Joomla! Templates

User avatar
ianmac
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 4785
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

Re: checkbox in custom component

Postby ianmac » Wed Apr 15, 2009 1:33 pm

jnodwell wrote:I don't see how it's not simple. Check for the variable, if it exists, it's set. If it doesn't, it's not set.

If it's easier to conceptualize for you, use a dropdown. Sure.


What's more complicated, IIRC (and it has been a while since I've used it), is that if you have a checkbox on your form and you use JTable::bind, you will never be able to uncheck your checkbox unless you add specific code for your checkbox, which makes things more complicated than if checkboxes sent a true or false value.

If you aren't using bind, then you should have no trouble.

Ian

kersurk
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Sun Aug 08, 2010 11:10 pm

Re: checkbox in custom component

Postby kersurk » Sun Aug 08, 2010 11:26 pm

Sorry for bumping an old topic, just sharing my solution (more for MVC type component) in case anyone googles here.

It's ugly, but works with current Joomla.

1) Adding the value="1" is good, as else it would be "on" instead, but noone would want to keep that string in database. Polluting the html is the down-side, though.

2) In model, where table store is called set an argument with true, which would set updateNulls to true, so that unchecked checkboxes would update the old value.

3) Set values in JTable inherited classes as null and default values in model, if you would set default value other than null also in JTable, it would be used when it's not sent via POST (when it's unchecked).

That's all. Hope it's better in Joomla 1.6 framework.

EDIT: Oh, one more thing: if you don't want to have null and 1 in database but 0 and 1, then set the value in JTable inherited class as 0 instead of null.

coolgrafix
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Tue Jan 12, 2010 6:46 pm

Re: checkbox in custom component

Postby coolgrafix » Thu Sep 01, 2011 6:43 am

Having this same problem where an unchecked checkbox doesn't get automatically adjusted by JTable::bind().

Luckily, I had some experience with inserting variables into the $_POST array artificially using JRequest::setVar.

So I just check for the existence of the variable in the $_POST array, and if it's not there then I insert it with a blank value.

Example:

Code: Select all

// Because we have checkboxes on our form, we have to take special precautions.
// An unchecked checkbox does not get included in the $_POST send.
// So we have to check for it's existence and add it if not present.
if(JRequest::getVar("attendeeInterest1") == ""){
   JRequest::setVar( "attendeeInterest1", "");
}
if(JRequest::getVar("attendeeInterest2") == ""){
   JRequest::setVar( "attendeeInterest2", "");
}
if(JRequest::getVar("attendeeInterest3") == ""){
   JRequest::setVar( "attendeeInterest3", "");
}


I added the above just before calling using $row->bind(JTable::get('post')), which marries the $_POST array to your database table instance.

Additional info:
http://api.joomla.org/Joomla-Framework/Environment/JRequest.html#setVar

Anyway, hope this helps. =)
Voila, it works! =)


Return to “Joomla! 1.5 Coding”

Who is online

Users browsing this forum: No registered users and 12 guests