Class: Zebra_Form_Checkbox
source file: /includes/Checkbox.php
Class Overview
XSS_Clean
|
--Zebra_Form_Control
|
--Zebra_Form_Checkbox
Class for checkbox controls.
Author(s):
Copyright:
- (c) 2006 - 2013 Stefan Gabos
Class methods
constructor __construct()
void
__construct (
string
$id
,
mixed
$value
,
[
array
$attributes
= '']
)
Constructor of the class
Adds an <input type="checkbox"> control to the form. Do not instantiate this class directly! Use the add() method instead! // create a new form
// single checkbox
$obj = $form->add('checkbox', 'my_checkbox', 'my_checkbox_value');
// multiple checkboxes
// notice that is "checkboxes" instead of "checkbox"
// checkboxes values will be "0", "1" and "2", respectively, and will be available in a custom template like
// "mycheckbox_0", "mycheckbox_1" and "mycheckbox_2".
// label controls will be automatically created having the names "label_mycheckbox_0", "label_mycheckbox_1" and
// "label_mycheckbox_2" (label + underscore + control name + underscore + value with anything else other than
// letters and numbers replaced with an underscore)
// $obj is a reference to the first checkbox
$obj = $form->add('checkboxes', 'mycheckbox',
array(
'Value 1',
'Value 2',
'Value 3'
)
);
// multiple checkboxes with specific indexes
// checkboxes values will be "v1", "v2" and "v3", respectively, and will be available in a custom template like
// "mycheckbox_v1", "mycheckbox_v2" and "mycheckbox_v3".
// label controls will be automatically created having the names "label_mycheckbox_v1", "label_mycheckbox_v2" and
// "label_mycheckbox_v3" (label + underscore + control name + underscore + value with anything else other than
// letters and numbers replaced with an underscore)
$obj = $form->add('checkboxes', 'mycheckbox',
array(
'v1' => 'Value 1',
'v2' => 'Value 2',
'v3' => 'Value 3'
)
);
// multiple checkboxes with preselected value
// "Value 2" will be the preselected value
// note that for preselecting values you must use the actual indexes of the values, if available, (like
// in the current example) or the default, zero-based index, otherwise (like in the next example)
$obj = $form->add('checkboxes', 'mycheckbox',
array(
'v1' => 'Value 1',
'v2' => 'Value 2',
'v3' => 'Value 3'
),
'v2' // note the index!
);
// "Value 2" will be the preselected value.
// note that for preselecting values you must use the actual indexes of the values, if available, (like
// in the example above) or the default, zero-based index, otherwise (like in the current example)
$obj = $form->add('checkboxes', 'mycheckbox',
array(
'Value 1',
'Value 2',
'Value 3'
),
1 // note the index!
);
// multiple checkboxes with multiple preselected values
$obj = $form->add('checkboxes', 'mycheckbox[]',
array(
'v1' => 'Value 1',
'v2' => 'Value 2',
'v3' => 'Value 3'
),
array('v1', 'v2')
);
// custom classes (or other attributes) can also be added to all of the elements by specifying a 4th argument;
// this needs to be specified in the same way as you would by calling <a href="../Generic/Zebra_Form_Control.html#methodset_attributes">set_attributes()</a> method:
$obj = $form->add('checkboxes', 'mycheckbox[]',
array(
'1' => 'Value 1',
'2' => 'Value 2',
'3' => 'Value 3',
),
'', // no default value
array('class' => 'my_custom_class')
);
// don't forget to always call this method before rendering the form
if ($form->validate()) {
// put code here
}
// output the form using an automatically generated template
$form->render();
By default, for checkboxes, radio buttons and select boxes, the library will prevent the submission of other
values than those declared when creating the form, by triggering the error: "SPAM attempt detected!". Therefore,
if you plan on adding/removing values dynamically, from JavaScript, you will have to call the
disable_spam_filter() method to prevent that from happening!
Parameters:
| string |
$id |
Unique name to identify the control in the form. $id needs to be suffixed with square brackets if there are more checkboxes
sharing the same name, so that PHP treats them as an array! The control's name attribute will be as indicated by $id argument while the control's id attribute will be $id, stripped of square brackets (if any), followed by an underscore and followed by $value with all the spaces replaced by underscores. So, if the $id arguments is "my_checkbox" and the $value argument is "value 1", the control's id attribute will be my_checkbox_value_1. This is the name to be used when referring to the control's value in the POST/GET superglobals, after the form is submitted. This is also the name of the variable to be used in custom template files, in order to display the control. // in a template file, in order to print the generated HTML
// for a control named "my_checkbox" and having the value of "value 1",
// one would use:
echo $my_checkbox_value_1;
Note that when adding the required rule to a group of checkboxes (checkboxes
sharing the same name), it is sufficient to add the rule to the first checkbox! |
| mixed |
$value |
Value of the checkbox. |
| array |
$attributes |
(Optional) An array of attributes valid for input controls (disabled, readonly, style, etc) Must be specified as an associative array, in the form of attribute => value. // setting the "checked" attribute
$obj = $form->add(
'checkbox',
'my_checkbox',
'v1',
array(
'checked' => 'checked'
)
);
See set_attributes() on how to set attributes, other than through the constructor. The following attributes are automatically set when the control is created and should not be altered manually:
type, id, name, value, class |
Top
method toHTML()
string
toHTML (
)
Generates the control's HTML code.
This method is automatically called by the render() method!
Tags:
| return: |
The control's HTML code |
Top
|