cakephp checkbox default checked value snippet
so for a job vacancy form, I need to ask user if it requires a closing date as some jobs won’t. (they will alwys be looking for casual staff etc…)
so I added a field to my DB with default 1 (true therefore) and only later realised I would have been better just to reverse the semantics of the field name. But in the meantime I thought I ‘d just be able to set the checkbox default to true, but I only want this if the user has not specified it ever.
(so not to set it to checked if the user has ever set it to UNCHECKED)
here is how I Did it - a bit of code from my form element that I use in both add and edit actions’ views…
$fieldOptions = (strpos($this->action,'add') && !isset($this->data)) ? array('label'=>'Does this require a date?', 'after'=>'Only uncheck this if there is no closing date', 'checked'=>true) : array('label'=>'Does this require a date?', 'after'=>'Only uncheck this if there is no closing date') ; echo $form->input('needs_date', $fieldOptions);
love the ‘after’ field in Cake these days - the form helper is a work of art!
August 20th, 2008 at 4:25 pm
I think it is you who “does require a date”!
Also - code snippets not wrapping in IE7 (but I cutnPasted into textpad and could see there)