• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Checkboxes, checked by default, how to ?

#9
[eluser]moonbeetle[/eluser]
You could extend the form helper with an additional function as follows:

Code:
/**
* Conditional Checkbox Field
*
* @access public
* @param mixed
* @param string
* @return string
*/

function form_checkbox_conditional($data = '', $value = '', $extra = '')
{
$defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);

if (is_array($data) AND array_key_exists('compareVal', $data)){
  $checked = ($data['value'] != $data['compareVal']) ? FALSE : TRUE;
  unset($data['compareVal']);
}
if ($checked == TRUE)
  $defaults['checked'] = 'checked';
else
  unset($defaults['checked']);

return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
}

(leaving the original function form_checkbox() to co-exist. The code above could be written simpler if you don't want to pass a $data array as an argument)

And in you view files you can call this new function as follows:

Code:
// name : the name of the checkbox field
// value : the (default)value that you want to check against
// compareVal : the value that you want to compare

// Example:

$checkboxData = array(
'name'   => 'lang',
'value'  => $checkboxVal,
'compareVal'  => $compareVal
);
echo form_checkbox_conditional($checkboxData);

Determining the checked state by comparing values can be done as the form gets displayed or re-displayed (after validation) as long as the controller sends back the necessary variables.


Messages In This Thread
Checkboxes, checked by default, how to ? - by El Forum - 07-20-2007, 10:16 AM
Checkboxes, checked by default, how to ? - by El Forum - 07-20-2007, 10:30 AM
Checkboxes, checked by default, how to ? - by El Forum - 07-20-2007, 10:50 AM
Checkboxes, checked by default, how to ? - by El Forum - 07-20-2007, 07:53 PM
Checkboxes, checked by default, how to ? - by El Forum - 07-20-2007, 10:07 PM
Checkboxes, checked by default, how to ? - by El Forum - 07-21-2007, 02:00 AM
Checkboxes, checked by default, how to ? - by El Forum - 07-21-2007, 02:08 AM
Checkboxes, checked by default, how to ? - by El Forum - 07-21-2007, 08:18 AM
Checkboxes, checked by default, how to ? - by El Forum - 07-23-2007, 05:56 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.