Forms

The Forms service provides great functionality for creating and managing forms.


Form Manager

Part of the Forms component is the Form Manager. This holds all your forms. The best way to utilise the manager is to add forms to it through Service Providers.

The easiest way to use the Form Manager is to make sure your Controllers extend Tomahawk\Forms\FormsManagerInterface. You then have access to it through $this->forms.

Other wise just add the following parameter to the construct method of your Controller Tomahawk\Forms\FormsManagerInterface and it will get injected in through the Service Container.


Creating a Form

To create a form you create an instance of Tomahawk\Forms\Form.

The Form class has the following constructor __construct($url, $method = 'POST', $model = null, array $oldInput = array(), array $attributes = array())

  • $url - URL form goes to. Populates the action attribute of the form.
  • $method - Form Method. Populates the method attribute of the form.
  • $model - Model, Person class for instance. Gets populated by form data.
  • $oldInput - Old form data, usually taken from the Request or Input class.
  • $attributes - Additional attributes to add to the form during output.

You then add Form Elements to your Form. See the next section to see what Form Elements there are and how to add them.


Form Elements

Below are a list of available Form elements you can add to your form:

Checkbox

The Checkbox class has the following constructor __construct($name, $value = null, $checked = false)

You can create a new Checkbox element doing the following:

Date

The Date class has the following constructor __construct($name, $value = null)

The Date Element renders a HTML5 input element with a type of "date".

You can create a new Date element doing the following:

Email

The Email class has the following constructor __construct($name, $value = null)

The Email Element renders a HTML5 input element with a type of "email".

You can create a new Email element doing the following:

Hidden

The Hidden class has the following constructor __construct($name, $value = null)

The Hidden Element renders a HTML input element with a type of "hidden".

You can create a new Hidden element doing the following:

Number

The Number class has the following constructor __construct($name, $value = null)

The Number Element renders a HTML5 input element with a type of "text" and a pattern attribute set to [0-9]+ for browser compatibility.

You can create a new Number element doing the following:

Password

The Password class has the following constructor __construct($name)

You can create a new Password element doing the following:

Phone

The Phone class has the following constructor __construct($name, $value = null).

The Phone Element renders a HTML5 input element with a type of "tel".

You can create a new Phone element doing the following:

Radio

The Radio class has the following constructor __construct($name, $value = null).

In version 1.0.0, setting the radio as checked can't be done via the constructor.

This will be fixed in the next version.

You can create a new Radio element doing the following:

Search

The Search class has the following constructor __construct($name, $value = null).

The Search Element renders a HTML5 input element with a type of "search".

You can create a new Search element doing the following:

Select

The Select class has the following constructor __construct($name, array $list = array(), $selected = null).

You can create a new Select element doing the following:

Submit

The Submit class has the following constructor __construct($name, $value = 'Submit').

You can create a new Submit element doing the following:

Text

The Text class has the following constructor __construct($name, $value = null).

You can create a new Text element doing the following:

TextArea

The TextArea class has the following constructor __construct($name, $value = null).

You can create a new TextArea element doing the following:

Url

The Url class has the following constructor __construct($name, $value = null).

You can create a new Url element doing the following:


CSRF Protection

Tomahawk comes with a CSRF Bundle, giving you access to a CSRF Middleware that checks on a POST request if a route requires a CSRF token and that it is valid


Configuration

First make sure the CSRFBundle is being loaded in the AppKernel

The default name of the input for the CSRF token is _csrf_token. You can change it by setting the csrf_token_name value in app/config/security.php


Using the Token Manager

The easiest way to use the Token Manager is to add the following parameter to the construct method of your Controller Tomahawk\Bundle\CSRFBundle\Token\TokenManagerInterface and it will get injected in through the Service Container.

You can also access it through the service container by doing:


Create the Form Control

Now you have access to the token manager you can create the form control.

All you need to do is add a hidden form input to the form using a token generated via the token manager.

Data Transformers

Data transformers allow you to change a value before it is rendered by a form control or when input is being bound to a model.

Tomahawk comes with one simple data transformer CallableDataTransformer.

You pass it 2 closures, 1st first for transforming a value, the 2nd for reversing the transform.

For example say you had dob property on model and it was an instance of \DateTime, you would want to transform it to a string before it is rendered by the form control. And when the dob is taken from the request and given to the form, it will be reverse transformed before being given to the model.