Again, after googling for few days – couldn’t find any decent PHP class that is lightweight and configurable at the same time.
Form class for rendering and submission
Inspired by Drupal’s built-in form functions.
So for more info on functions – you can also consult Drupal docs.
Version 1.1
Example code
// Create form elements as an array
$form = array();
$form['#form] = array(
'callback' => 'my_submit_function'
'redirection' => 'http://mysite.net/submitted',
);
$form['my_field'] = array(
'#type' => 'textfield',
'#title' => 'Name'
'#description' => 'Enter your name',
'#name' => 'name'
);
$form['submit'] = array(
'#type' => 'submit'
'#value' => 'Send',
);
// Create form object
$my_form = new Enlimbo_Forms();
// Set auto handle (sets form, detects submission)
$my_form->autoHandle('my_form', $form);
// Render form
echo '<form method="post" action="">';
echo $my_form->renderForm();
echo '</form>';
Field Types
- fieldset
- textfield
- textarea
- password
- checkbox
- checkboxes
- radios
- select
- markup
- hidden
- submit
- button
- reset
Example Parameters
Elements can use this parameters (if available for specific type). This will give you general idea how to use class.
Required
'#type' => 'radios'
'#name' => 'my_form_data[my_radio]'
Optional
'#title' => 'Choose something'
'#description' => 'This selection is required'
'#value' => $data['my_radio']
'#attributes' => array(
'style' => 'width:100%',
'onclick' => 'jQuery(this).hide();'
)
'#markup' => 'Some HTML formatted output'
'#options' => array(
'Select me' => 'select_me'
'No! Select me! => 'no_select_me'
// or full option element
'select_me' => array(
'#title' => 'Option One',
'#description' => 'if you choose this...'
),
)
'#default_value' => 'select_me'
'#before' => '<div class="wrapper">'
'#after' => '</div>'
'#prefix' => 'prefix:<span>'
'#suffix' => ':suffix</span>'
'#inline' => true,
'#pattern' => '<BEFORE><LABEL><ERROR><PREFIX><ELEMENT><SUFFIX><DESCRIPTION><AFTER>'
'#collapsible' => true
'#collapsed' => false
Example code for nested elements
$form['fieldset_1'] = array(
'#type' => 'fieldset'
'#title' => 'Fieldset One'
'#description' => 'Fieldset contains group of fields',
);
$form['fieldset_1']['textfield'] = array(
'#type' => 'textfield'
...
$form['fieldset_1'] = array(
'#type' => 'fieldset'
'#title' => 'Fieldset One'
'#description' => 'Fieldset can contain fieldset',
);
$form['fieldset_1']['textfield'] = array(
'#type' => 'textfield'
...
$form['fieldset_1']['fieldset_1_1'] = array(
'#type' => 'fieldset'
...
Validation
You have to set your own validation function that will be called on specific element if ‘#validate’ parameter is set. You can configure ‘#validate’ parameter any way you like.
Example
$my_form->setValidationFunc('my_validation');
$form['element'] = array(
'#type' => 'textfield',
'#validate' => array(
'required' => true,
'date_format' => 'd/m/Y',
...
),
);
function my_validation($validate, $value) {
if (!empty($validate['required']) && empty($value)) {
return false;
}
...
}
This will automatically trigger error on form.
More
You don’t have to use class this way. Autohandle is not a must. You can simply render any fields with $my_form->renderElements($elements) call or do some improvisation.
There are more methods to be documented. Updates will come…