Form Helper - personaclix/PersonaClixEngine GitHub Wiki

The Form Helper is designed to help with the creation of HTML forms.

To start using the helper, you must add a use statement to the top of the file.

use \PersonaClix\Engine\Helpers\Form;

Open the Form

Generate the form opening tag. Takes 3 optional parameters. Method (GET or POST), Action (/something), and Name.

echo Form::open("POST", "/process", "myForm");

Input Fields

Generate an input field with an optional label. Takes an optional associative array with Key => Value options for the form.

Supported Options: type, name, id, value, class, style, label

Set the label option to generate a label beside the field with the provided text. It requires either the name or id option to also be specified.

$options = [
	'type' => 'text',
	'name' => 'txtBox',
	'label' => 'My Textbox: '
];

echo Form::input( $options );

Close the Form

Generates the closing form tag.

echo Form::close();

Cross Site Request Forgery (CSRF) Token

It is a good idea to protect the form from fraudulent submissions. Other websites that the user happens to be on could have malicious code that performs a fraudulent submission of the form and could cause a permanent action to be authorised without the user's knowledge. Using a token to verify that the request actually came from your site is recommended.

Adding this protection is easy with the Form Helper.

In the form, add the hidden form field with a uniquely generated token.

echo Form::csrf_token();

After the form has been submitted, verify the token. Passing true to the csrf_token() function will return only the token.

// Retrieve the generated token.
$token = Form::csrf_token(true);

// Check if token is set and that it matches the token provided with the form.
if( $token && $token == Request::post('csrf_token') ) {
	// Token verified. Proceed with form processing.
}