Form
A server-side form wrapper that handles form-group layout, labels with
required-field indicators, hint text, automatic CSRF tokens, and an auto-generated
client-side Validator — all from a single fluent builder.
The Form component is purely PHP; fields with required in their rules
automatically display a red asterisk.
Usage pattern: build the form chain, call ->submit() to
configure the submit button (returns a Button for fluent chaining), then
echo $form to render the complete HTML.
Auto-validating Form
Pass rules as the third argument to ->field(). The Form generates a
Validator automatically, deriving error messages from the label text.
Override with the optional fourth argument. CSRF token injected by default on POST.
Cancel & Reset Buttons
->cancel($text, $href, $icon) adds a secondary action button rendered as an
anchor link (when $href is given) or a history.back() button
when no href is supplied. ->reset($text, $icon) adds a
type="reset" button that clears all fields to their initial DOM values.
Both methods return the Form for chaining. Action buttons render in the order:
submit → cancel → reset.
Reset Button
Use ->reset() on filter or data-entry forms where users may want to clear all
inputs. No JavaScript needed — the browser handles it via type="reset".
All Three Action Buttons
Submit, cancel, and reset can all be combined. They render in one
form-actions div in the order: submit → cancel → reset.
Model Binding
Pass an associative array or object to ->model() to pre-populate field values.
Keys are matched to each component's name() attribute via value().
Layouts
->layout('horizontal') places labels and fields side-by-side.
->layout('inline') flows all fields into one row — useful for filter bars.
Default is vertical.
Horizontal
No Auto-Validation
->noValidation() suppresses the auto-generated Validator. Use this when the
Form is inside a Wizard (which manages validation per-step), or when you want to manage a
separate $m->validator() yourself.
Hidden Fields & Raw HTML
->hidden() injects a <input type="hidden">.
->html() inserts arbitrary markup — useful for captcha images, card wrappers,
or any content that doesn't fit the standard field pattern. Complex multi-section forms
can capture their body via ob_start()/ob_get_clean() and pass
the result to ->html(), letting the Form manage the outer tag and CSRF.
PHP PHP Methods (Fluent)
| Method / Property | Parameters | Description |
|---|---|---|
$m->form($id) | string | Create a Form. The $id is also used as the form's name attribute. |
->action($url) | string | Form action URL. Defaults to empty (submits to current page). |
->method($method) | string | HTTP method: post (default), get, put, delete. PUT/DELETE emit a hidden _method field. |
->name($name) | string | Override the form name attribute. Defaults to the ID. |
->model($data) | array|object | Bind an associative array or object to pre-populate field values. Keys are matched to component name() attributes. |
->field($component, $label, $rules, $errorMessage, $hint, $wrapperClass) | Component, string, array, string, string, string | Add a field wrapped in a form-group div. $rules drive the auto-Validator; $errorMessage overrides the label-derived default; $hint is small text shown below the field; required fields show a red asterisk. |
->hidden($name, $value) | string, string | Inject a <input type="hidden">. |
->html($html) | string | Insert raw HTML at the current position in the field list. |
->submit($text, $icon) | string, string | Add a submit button. Returns a Button instance for further fluent configuration (e.g. ->primary()). Echo the Form variable to render full HTML — not the Button return value. |
->cancel($text, $href, $icon) | string, string, string | Add a cancel action. With $href: renders as <a href="…">. Without: onclick="history.back()". Defaults: text Cancel, icon fa-times. Returns self. |
->reset($text, $icon) | string, string | Add a type="reset" button. Defaults: text Reset, icon fa-undo. Returns self. |
->layout($layout) | string | Form layout: vertical (default), horizontal, inline. |
->ajax() | | Set data-m-ajax="true" on the form element. |
->noValidation() | | Suppress the auto-generated client-side Validator. |
->noCsrf() | | Suppress the automatic csrf_token hidden input (injected by default on POST/PUT/DELETE). |
->formAttr($name, $value) | string, string | Add an arbitrary attribute to the <form> element (e.g. onsubmit, enctype, autocomplete). |