[Solar-talk] Validating a checkbox
Clay Loveless
clay at killersoft.com
Thu Aug 2 04:43:29 CDT 2007
On Aug 1, 2007, at 10:31 PM, Clay Loveless wrote:
> I'll keep messing around; I think there's a solution possible by
> checking the options for the checkbox. Would also be nice to come
> up with a checkbox-group validation, along the lines of "at least
> two of these 5 boxes must be checked".
For the time being, here's what I've come up with. In my
'Foo_App_Registration' application, I have a public method called
'validChecked':
public function validChecked($value, $checked_option = null)
{
return ($value == 1 || $value == $checked_option);
}
Then, in the same class, in a method that defines the full
registration form, the checkbox with a label saying "I agree to the
Terms and Conditions" is defined like this:
'members[terms_agreement]' => array(
'type' => 'checkbox',
'label' => $this->locale('LABEL_TERMS_AGREEMENT'),
'valid' => array(
array('callback', $this->locale('VALID_NOTCHECKED'), array
($this, 'validChecked'))
),
),
... the form will fail validation if the checkbox is not checked.
To take advantage of the optional "options" array for a checkbox
definition (as described [here][]), validChecked can also compare the
value to a passed $checked_option parameter ... aka what the value
should be if the checkbox is actually checked. Like so:
'members[t_and_c_agreement]' => array(
'type' => 'checkbox',
'label' => $this->locale('LABEL_T_AND_C_AGREEMENT'),
'options' => array('y', 'n'),
'valid' => array(
array('callback', $this->locale('VALID_NOTCHECKED'), array
($this, 'validChecked'), 'y')
),
),
Voilá -- not as intuitive as I'd prefer, but seems to be working in
my initial testing and gets the job done for the time being.
-Clay
[here]: http://solarphp.com/manual/Form_processing/Set_form_elements
--
Killersoft.com
More information about the Solar-talk
mailing list