-
Notifications
You must be signed in to change notification settings - Fork 55
scbForms
scbForms is a class that simplifies form generation.
Why you should use it:
- automatically handles escaping; no need to worry about XSS attacks anymore
- given formdata, it finds the appropriate value and sets checked/selected attributes
- wraps checkboxes and radio buttons in <label> tags so they're easier to click
This is the main method, with which you can generate one or more form elements of the same type, including <select>s and <textarea>s.
$args is an associative array of arguments that define what form element should be generated.
General args:
- type - string (mandatory)
- name - string | array (mandatory)
- value - string | array
- desc - string | array | bool
- desc_pos - 'before' | 'after' | 'foo %input% bar' (default: after)
- extra - string | array Extra HTML attributes like class, style etc.
'checkbox' specific arguments:
- choices - array(value => label) | The possible values
- checked - bool | string Force the checkbox button to be checked or not
'radio' and 'select' specific arguments:
- text - bool | string By default, an empty option is generated. This can be disabled by setting the text argument to false
- choices - array(value => label) | The possible values
- numeric - bool (default: false) If set to false, the method will attempt to convert a numeric choices array to an associative one
- selected - string Force a certain option to be selected
$formdata is also an associative array with the data with which to pre-fill the elements.
Simple text input:
scbForms::input(array(
'type' => 'text',
'name' => 'title',
'value' => 'Insert title here',
));
The input will be prefilled with the text 'Insert title here'.
Multiple radio buttons:
$data = array( 'size' => 'medium' );
scbForms::input(array(
'type' => 'radio',
'name' => 'size',
'choices' => array('small', 'medium', 'large')
), $data);
This will generate three radio buttons, with the middle one selected.
A dropdown element:
$data = array( 'language' => 'en' );
scbForms::input(array(
'type' => 'select',
'name' => 'language',
'choices' => array( 'en' => 'English', 'fr' => 'French', 'de' => 'German' )
), $data);
This will generate a <select> element with 3 options. The selected one will be 'en'.
More examples can be found here:
http://plugins.trac.wordpress.org/browser/scb-framework/trunk/example.php