$form->addInput(string $type, string $name, string $value = '', string $label = '', string $attr = '');
/**
* Adds an input element to the form.
*
* @param string $type The type of the input element.
* Accepts all input html5 types except checkbox and radio :
* button, color, date, datetime, datetime-local,
* email, file, hidden, image, month, number, password,
* range, reset, search, submit, tel, text, time, url, week
* @param string $name The name of the input element.
* @param string $value (Optional) The default value of the input element.
* @param string $label (Optional) The label of the input element.
* @param string $attr (Optional) Any additional attributes for the input element.
* Can be any HTML input attribute or js event.
* attributes must be listed separated with commas.
* If you don't specify any ID as attr, the ID will be the input's name.
* Example : class=my-class,placeholder=My Text,onclick=alert(\'clicked\');
* @return \phpformbuilder\Form
*/
$form->addTextarea(string $name, string $value = '', string $label = '', string $attr = '');
/**
* Adds textarea to the form
* @param string $name The textarea name
* @param string $value (Optional) The textarea default value
* @param string $label (Optional) The textarea label
* @param string $attr (Optional) Can be any HTML input attribute or js event.
* attributes must be listed separated with commas.
* If you don't specify any ID as attr, the ID will be the name of the textarea.
* Example : cols=30, rows=4;
* @return \phpformbuilder\Form
*/
Always add your options BEFORE creating the select element
1Add your options
2Create your select
$form->addOption(string $selectName, string $value, string $txt, string $groupName = '', string $attr = '');
/**
* Adds option to the $selectName element
*
* IMPORTANT : Always add your options BEFORE creating the select element
*
* @param string $selectName The name of the select element
* @param string $value The option value
* @param string $txt The text that will be displayed
* @param string $groupName (Optional) the optgroup name
* @param string $attr (Optional) Can be any HTML input attribute or js event.
* attributes must be listed separated with commas.
* If you don't specify any ID as attr, the ID will be the name of the option.
* Example : class=my-class
* @return \phpformbuilder\Form
*/
$form->addSelect(string $selectName, string $label = '', string $attr = '', bool $displayGroupLabels = true);
addSelect()
function plays nice with bootstrap-select plugin and select2 plugin.
Add the selectpicker class for Bootstrap select or the select2 class for the Select2 plugin, data-attributes and phpformbuilder will do the job.
Don't forget to activate your plugins to add the required css/js files to your page.
/**
* Adds a select element
*
* IMPORTANT : Always add your options BEFORE creating the select element
*
* @param string $selectName The name of the select element
* @param string $label (Optional) The select label
* @param string $attr (Optional) Can be any HTML input attribute or js event.
* attributes must be listed separated with commas.
* If you don't specify any ID as attr, the ID will be the input's name.
* Example : class=my-class
* @param bool $displayGroupLabels (Optional) True or false.
* Default is true.
* @return \phpformbuilder\Form
*/
$form->addOption('select-with-groupnames', 'option-1', 'option 1', 'group 1');
$form->addOption('select-with-groupnames', 'option-2', 'option 2', 'group 1', 'selected=selected');
$form->addOption('select-with-groupnames', 'option-3', 'option 3', 'group 1');
$form->addOption('select-with-groupnames', 'option-4', 'option 4', 'group 2');
$form->addOption('select-with-groupnames', 'option-5', 'option 5', 'group 2');
$form->addSelect('select-with-groupnames', 'Select please: ', '');
Select please:
option 1 option 2 option 3 option 4 option 5
for ($i=1; $i < 11; $i++) {
$form->addOption('myMultipleSelectName[]', $i, 'option ' . $i);
}
$form->addSelect('myMultipleSelectName[]', 'Select please:(multiple selections)', 'multiple=multiple');
Select please:(multiple selections)
option 1 option 2 option 3 option 4 option 5 option 6 option 7 option 8 option 9 option 10
The addCountrySelect()
function generates a full country select dropdown list with all the countries options. Fortunately, you don't have to add the options one by one. The function does it for you.
You can choose your prefered plugin, which can be slimselect
(default), bootstrap-select
or select2
.
If you choose the bootstrap-select
plugin keep in mind that it requires Bootstrap's dropdown in your bootstrap.min.js
$form->addCountrySelect(string $selectName, string $label = '', string $attr = '', array $userOptions = []);
/**
* adds a country select list with flags
* @param string $selectName
* @param string $label (Optional) The select label
* @param string $attr (Optional) Can be any HTML input attribute or js event.
* attributes must be listed separated with commas.
* If you don't specify any ID as attr, the ID will be the name of the select.
* Example : class=my-class
* @param array $userOptions (Optional) :
* plugin : slimselect | select2 | bootstrap-select Default: 'slimselect'
* lang : MUST correspond to one subfolder of [$this->pluginsUrl]countries/country-list/country/cldr/
* *** for example 'en', or 'fr_FR' Default : 'en'
* flags : true or false. Default : true
* *** displays flags into option list
* flag_size : 16 or 32 Default : 32
* return_value : 'name' or 'code' Default : 'name'
* *** type of the value that will be returned
* @return \phpformbuilder\Form
*/
Live examples with code are available here:
javascript-plugins.php#slimselect-example javascript-plugins.php#bootstrap-select-example
javascript-plugins.php#select2-example
The addTimeSelect()
function generates an hours:minutes drop-down list. The options allow you to set a minimum time, a maximum time, the interval in minutes between each option, and the separator character.
$form->addTimeSelect(string $selectName, string $label = '', string $attr = '', array $userOptions = []);
/**
* adds an hours:minutes select dropdown
* @param string $selectName
* @param string $label (Optional) The select label
* @param string (Optional) Can be any HTML input attribute or js event.
* attributes must be listed separated with commas.
* If you don't specify any ID as attr, the ID will be the name of the select.
* Example : class=my-class
* @param array $userOptions (Optional) :
* min : the minimum time in hour:minutes Default: '00:00'
* max : the maximum time in hour:minutes Default: '23:59'
* step : the step interval in minutes between each option Default: 1
* format : '12hours' or '24hours' Default : '24hours'
* display_separator : the displayed separator character between hours and minutes Default : 'h'
* value_separator : the value separator character between hours and minutes Default : ':'
* @return \phpformbuilder\Form
*/
Live examples with code are available here:
https://amilma.digital/templates/bootstrap-5-forms/booking-form.php https://amilma.digital/templates/bootstrap-5-forms/car-rental-form.php
Add your radio buttons
Call printRadioGroup
$form->addRadio(string $groupName, string $label, string $value, string $attr = '');
/**
* Adds radio button to $groupName element
*
* @param string $groupName The radio button groupname
* @param string $label The radio button label
* @param string $value The radio button value
* @param string $attr (Optional) Can be any HTML input attribute or js event.
* attributes must be listed separated with commas.
* Example : checked=checked
* @return \phpformbuilder\Form
*/
$form->printRadioGroup(string $groupName, string $label = '', bool $inline = true, string $attr = '');
/**
* Prints radio buttons group.
*
* @param string $groupName The radio button group name
* @param string $label (Optional) The radio buttons group label
* @param bool $inline (Optional) True or false.
* Default is true.
* @param string $attr (Optional) Can be any HTML input attribute or js event.
* attributes must be listed separated with commas.
* Example : class=my-class
* @return \phpformbuilder\Form
*/
Example
$form->addRadio('myRadioBtnGroup', 'choice one', 'value 1');
$form->addRadio('myRadioBtnGroup', 'choice two', 'value 2', 'checked=checked');
$form->addRadio('myRadioBtnGroup', 'choice three', 'value 3');
$form->printRadioGroup('myRadioBtnGroup', 'Choose one please', true, 'required=required');
Choose one please *
choice one
choice two
choice three
Several plugins are available to replace the ugly browser default radio buttons with nice ones:
Add your checkboxes
Call printCheckboxGroup
$form->addCheckbox(string $groupName, string $label, string $value, string $attr = '');
/**
* Adds checkbox to $groupName
*
* @param string $groupName The checkbox button groupname
* @param string $label The checkbox label
* @param string $value The checkbox value
* @param string $attr The checkbox attributes
* @return \phpformbuilder\Form
*/
$form->printCheckboxGroup(string $groupName, string $label = '', bool $inline = true, string $attr = '');
/**
* Prints checkbox group.
*
* @param string $groupName The checkbox group name (will be converted to an array of indexed value)
* @param string $label (Optional) The checkbox group label
* @param bool $inline (Optional) True or false.
* Default is true.
* @param string $attr (Optional) Can be any HTML input attribute or js event.
* attributes must be listed separated with commas.
* Example : class=my-class
* @return \phpformbuilder\Form
*/
Example
$form->addCheckbox('myCheckboxGroup', 'choice one', 'value 1');
$form->addCheckbox('myCheckboxGroup', 'choice two', 'value 2', 'checked=checked');
$form->addCheckbox('myCheckboxGroup', 'choice three', 'value 3', 'checked=checked');
$form->printCheckboxGroup('myCheckboxGroup', 'Check please: ', true, 'required=required');
Check please: *
choice one
choice two
choice three
Several plugins are available to replace the ugly browser default checkboxes with nice ones:
For a single button, just call addBtn()
and let $btnGroupName empty.
For button group, call addBtn()
for each button, give a name to $btnGroupName, then call printBtnGroup()
$form->addBtn(string $type, string $name, string $value, string $text, string $attr = '', string $btnGroupName = '');
/**
* Adds a button to the form
*
* If $btnGroupName is empty, the button will be automatically displayed.
* Otherwise, you'll have to call printBtnGroup to display your btnGroup.
*
* @param string $type The html button type
* @param string $name The button name
* @param string $value The button value
* @param string $text The button text
* @param string $attr (Optional) Can be any HTML input attribute or js event.
* attributes must be listed separated with commas.
* If you don't specify any ID as attr, the ID will be the input's name.
* Example : class=my-class,onclick=alert(\'clicked\');
* @param string $btnGroupName (Optional) If you wants to group several buttons, group them then call printBtnGroup.
* @return \phpformbuilder\Form
*/
$form->printBtnGroup(string $btnGroupName, string $label = '');
/**
* Prints buttons group.
*
* @param string $btnGroupName The buttons' group name
* @param string $label (Optional) The buttons group label
* @return \phpformbuilder\Form
*/
$form->addBtn('submit', 'myBtnName', 1, 'Submit form', 'class=btn btn-primary');
$form->addBtn('submit', 'mySubmitBtnName', 1, 'Submit form', 'class=btn btn-primary', 'myBtnGroup');
$form->addBtn('button', 'myCancelBtnName', 0, 'Cancel', 'class=btn btn-danger, onclick=alert(\'cancelled\');', 'myBtnGroup');
$form->printBtnGroup('myBtnGroup');
You can add some HTML code at any place you want when creating your form.
This way, you can:
$form->addHtml(string $html, string $elementName = '', string $pos = 'after');
/**
* Adds HTML code at any place of the form
*
* @param string $html The html code to add.
* @param string $elementName (Optional) If not empty, the html code will be inserted
* just before or after the element.
* @param string $pos (Optional) If $elementName is not empty, defines the position
* of the inserted html code.
* Values can be 'before' or 'after'.
* @return \phpformbuilder\Form
*/
When your HTML is linked to an element, always call addHtml()
BEFORE creating the element.
To add helper texts, icons, buttons, or any input group addon, use the shortcut functions:
$form->addInput('text', 'input-name', '', 'Your name: ');
$form->addHtml('<p class="alert alert-danger">Your email address is required</p>');
$form->addInput('text', 'email', '', 'Your email', 'required');
Your name:
Your email address is required
Your email *
$form->addInput('text', 'input-name-2', '', 'Your name: ');
$form->addHtml('<p class="form-text alert alert-danger">Your email address is required</p>', 'email-2', 'after');
$form->addInput('text', 'email-2', '', 'Your email', 'required');
Your name:
Your email *
Your email address is required
wrapper can be one or two html elements
$form->addInputWrapper(string $html, string $elementName);
/**
* Wraps the element with HTML code.
*
* @param string $html The HTML code to wrap the element with.
* The HTML tag must be opened and closed.
* Example :
* @param string $elementName The form element to wrap.
* @return \phpformbuilder\Form
*/
Example
$form->addInputWrapper('<div class="bg-dark rounded p-2"><div class="bg-white rounded p-2"></div></div>', 'imput-wrapped');
$form->addInput('text', 'imput-wrapped', '', 'Input wrapped with custom divs');
Input wrapped with custom divs