Form & Fb classes
- Home
- Creating forms
- Form & Fb classes
The two related classes Form
and Fb
are mostly used for rendering HTML form fields, with Form
being the abstracted, user-friendly variant, and Fb
being the low-level variant. Many Form
methods are derived from the underlying Fb
methods of the same name.
For example, the method Fb::text
generates the minimal HTML required for the input, like the following:
<input class="textbox" type="text" name="some_field" value="">
The corresponding method, Form::text
, produces HTML for a label and input, with appropriate classes for CSS styling or JS manipulation, like the following:
<div class="field-element field-element--text field-element--required"><div class="field-label"><label for="field0">Some field <span class="field-label__required">required</span></label></div><div class="field-input"><input id="field0" class="textbox" type="text" name="some_field" value=""></div></div>
We recommend that you always use the Form
class.
Important methods that you should be aware of are:
Form::loadFromSession
, which checks saved session data for form field values and errors associated with form submissions.
Form::nextFieldDetails
, which sets a friendly name, mandatory status, and optionally brief explanatory text for the next field to be displayed.
A controller method to display a form should create a view which contains the actual form fields, and could look something like this:
modules/Example/Controllers/ExampleController.php
// ... public function displayForm() { Form::loadFromSession('example_form'); $view = new View('modules/Example/example_form'); $skin = new View('skin/inner'); $skin->page_title = 'Example form'; $skin->main_content = $view->render(); echo $skin->render(); }
The view used for the form could look something like this:
modules/Example/views/example_form.php
<?php use Sprout\Helpers\Form; Form::nextFieldDetails('Name', true); echo Form::text('name'); Form::nextFieldDetails('Email address', true); echo Form::email('email'); Form::nextFieldDetails('Phone number', false); echo Form::phone('phone');