General

construct

$form = new Form(string $formId, string $layout = 'horizontal', string $attr = '', string $framework = 'bs5');
/**
 * Defines the layout (horizontal | vertical).
 * Default is 'horizontal'
 * Clears values from the PHP session if static::clear has been called before
 * Catches posted errors
 * Adds hidden field with the form ID
 * Sets elements wrappers
 *
 * @param  string $formId   The ID of the form
 * @param  string $layout    (Optional) Can be 'horizontal' or 'vertical'
 * @param  string $attr      (Optional) Can be any HTML input attribute or js event EXCEPT class
 *                           (class is defined in layout param).
 *                           attributes must be listed separated with commas.
 *                           Example : novalidate,onclick=alert(\'clicked\');
 * @param  string $framework (Optional) bs4 | bs5 | bulma | foundation | material | tailwind | uikit
 *                           (Bootstrap 4, Bootstrap 5, Bulma, Foundation, Material design, Tailwind, UIkit)
 * @return \phpformbuilder\Form
 */

options

Call setOptions() only if you change defaults options

Go to Options for more details

$form->setOptions($options);
    /**
    * Sets form layout options to match your framework
    * @param array $userOptions (Optional) An associative array containing the
    *                            options names as keys and values as data.
    */

Mode (production vs. development)

Default mode is production.

$form->setMode(string $mode);
/**
 * set the form mode to 'development' or 'production'
 * in production mode, all the plugins dependencies are combined and compressed in a single css or js file.
 * the css | js files are saved in plugins/min/css and plugins/min/js folders.
 * these 2 folders have to be wrirable (chmod 0755+)
 *
 * @param  string $mode 'development' | 'production'
 * @return \phpformbuilder\Form
 */

Detailed explanations available here: Optimization (CSS & JS dependencies)

Action

Default action is htmlspecialchars($_SERVER["PHP_SELF"]).

htmlspecialchars prevents attackers from exploiting the code by injecting HTML or JavaScript code (Cross-site Scripting attacks) in forms (recommended on http://www.w3schools.com/php/php_form_validation.asp)

Call setAction() only if you change default action

$form->setAction(string $url, bool $addGetVars = true);
/**
 * Redefines form action
 *
 * @param string  $url          The URL to post the form to
 * @param boolean $addGetVars (Optional) If $addGetVars is set to false,
 *                              url vars will be removed from destination page.
 *                              Example : www.myUrl.php?var=value => www.myUrl.php
 *
 * @return \phpformbuilder\Form
 */

Layout

Set the layout of the form ("horizontal" or "vertical")

$form->setLayout(string $layout);
/**
 * Set the layout of the form.
 *
 * @param string $layout The layout to use. Can be "horizontal" or "vertical".
 * @return \phpformbuilder\Form
 */

Method

The default method is POST.

Call setMethod() only if you change default method

$form->setMethod(string $method);
/**
 * set sending method
 *
 * @param  string $method POST|GET
 * @return \phpformbuilder\Form
 */

Start Fieldset

Start your fieldset with an optional legend.
Don't forget to call endFieldset to end your fieldset.

You can add several fieldsets on the same form.

$form->startFieldset(string $legend = '', string $fieldsetAttr = '', string $legendAttr = '');
/**
 * Starts a fieldset tag.
 *
 * @param string $legend (Optional) Legend of the fieldset.
 * @param string $fieldsetAttr (Optional) Fieldset attributes.
 * @param string $legendAttr (Optional) Legend attributes.
 * @return \phpformbuilder\Form
 */

End Fieldset

$form->endFieldset();
/**
 * Ends a fieldset tag.
 * @return \phpformbuilder\Form
 */

startDependentFields

Start a dependent fields block.
The dependent fields block is hidden and will be shown if $parentField changes to one of $showValues.
Don't forget to call endDependentFields to end your Dependent Fields block.

if $inverse is true, dependent fields will be shown if any other value than $showValues is selected

Each Dependent fields block can include one or several dependent fields blocks.

Dependent fields MUST NOT START with the same fieldname as their parent field.

$form->startDependentFields($parentField, $showValues[, $inverse = false]);
    /**
    * Start a hidden block
    * which can contain any element and html
    * Hiden block will be shown on $parentField change
    * if $parentField value matches one of $showValues
    * @param  string $parentField name of the field which will trigger show/hide
    * @param  string $showValues  single value or comma separated values which will trigger show.
    * @param  boolean $inverse  if true, dependent fields will be shown if any other value than $showValues is selected.
    * @return void
    */

Live examples with code are available here: javascript-plugins.php#dependent-fields-example

endDependentFields

$form->endDependentFields();
    /**
    * Ends Dependent Fields block.
    */

$attr argument

Several element functions use a $attr argument.

The $attr argument accepts any HTML attribute or javascript event.

Use comma-separated values (see examples below).

Examples

    $form->addInput('text', 'name', '', 'Your name: ', 'id=my-id, placeholder=My Text, required=required');
    $form->addInput('password', 'pass', '', 'Your password: ', 'required=required, pattern=(.){7\,15}');
    $form->addTextarea('my-textarea', '', 'Your message: ', 'cols=30, rows=4');
    $form->addBtn('button', 'myBtnName', 1, 'Cancel', 'class=btn btn-danger, onclick=alert(\'cancelled\');');

Your name: *

Your password: *

Your message:

Elements

Input

$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
 */

Textarea

$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
*/