\Cake\FormForm

Form abstraction used to create forms not tied to ORM backed models, or to other permanent datastores. Ideal for implementing forms on top of API services, or contact forms.

Building a form

This class is most useful when subclassed. In a subclass you should define the _buildSchema, _buildValidator and optionally, the _execute methods. These allow you to declare your form's fields, validation and primary action respectively.

You can also define the validation and schema by chaining method calls off of $form->schema() and $form->validator().

Forms are conventionally placed in the App\Form namespace.

Summary

Methods
Properties
Constants
eventManager()
getEventManager()
setEventManager()
dispatchEvent()
validator()
getValidator()
setValidator()
hasValidator()
validationDefault()
__construct()
implementedEvents()
schema()
buildValidator()
validate()
errors()
getErrors()
setErrors()
execute()
getData()
setData()
__debugInfo()
No public properties found
VALIDATOR_PROVIDER_NAME
BUILD_VALIDATOR_EVENT
createValidator()
validationMethodExists()
_buildSchema()
_buildValidator()
_execute()
$_eventManager
$_eventClass
$_validatorClass
$_validators
$_schemaClass
$_schema
$_errors
$_validator
$_data
N/A
No private methods found
No private properties found
N/A

Constants

VALIDATOR_PROVIDER_NAME

VALIDATOR_PROVIDER_NAME = 'form' : string

The alias this object is assigned to validators as.

BUILD_VALIDATOR_EVENT

BUILD_VALIDATOR_EVENT = 'Form.buildValidator' : string

The name of the event dispatched when a validator has been built.

Properties

$_eventClass

$_eventClass : string

Default class name for new event objects.

Type

string

$_validatorClass

$_validatorClass : string

Validator class.

Type

string

$_validators

$_validators : array<mixed,\Cake\Validation\Validator>

A list of validation objects indexed by name

Type

array<mixed,\Cake\Validation\Validator>

$_schemaClass

$_schemaClass : string

Schema class.

Type

string

$_schema

$_schema : \Cake\Form\Schema

The schema used by this form.

Type

\Cake\Form\Schema

$_errors

$_errors : array

The errors if any

Type

array

$_data

$_data : array

Form's data.

Type

array

Methods

eventManager()

eventManager(\Cake\Event\EventManager|null  $eventManager = null) : \Cake\Event\EventManager

Returns the Cake\Event\EventManager manager instance for this object.

You can use this instance to register any new listeners or callbacks to the object events, or create your own events and trigger them at will.

Parameters

\Cake\Event\EventManager|null $eventManager

the eventManager to set

Returns

\Cake\Event\EventManager

getEventManager()

getEventManager() : \Cake\Event\EventManager

Returns the Cake\Event\EventManager manager instance for this object.

You can use this instance to register any new listeners or callbacks to the object events, or create your own events and trigger them at will.

Returns

\Cake\Event\EventManager

setEventManager()

setEventManager(\Cake\Event\EventManager  $eventManager) : $this

Returns the Cake\Event\EventManager manager instance for this object.

You can use this instance to register any new listeners or callbacks to the object events, or create your own events and trigger them at will.

Parameters

\Cake\Event\EventManager $eventManager

the eventManager to set

Returns

$this

dispatchEvent()

dispatchEvent(string  $name, array|null  $data = null, object|null  $subject = null) : \Cake\Event\Event

Wrapper for creating and dispatching events.

Returns a dispatched event.

Parameters

string $name

Name of the event.

array|null $data

Any value you wish to be transported with this event to it can be read by listeners.

object|null $subject

The object that this event applies to ($this by default).

Returns

\Cake\Event\Event

validator()

validator(\Cake\Validation\Validator|null  $validator = null) : \Cake\Validation\Validator

Get/Set the validator for this form.

This method will call _buildValidator() when the validator is first built. This hook method lets you configure the validator or load a pre-defined one.

Parameters

\Cake\Validation\Validator|null $validator

The validator to set, or null.

Returns

\Cake\Validation\Validator

the validator instance.

getValidator()

getValidator(string|null  $name = null) : \Cake\Validation\Validator

Returns the validation rules tagged with $name. It is possible to have multiple different named validation sets, this is useful when you need to use varying rules when saving from different routines in your system.

If a validator has not been set earlier, this method will build a valiator using a method inside your class.

For example, if you wish to create a validation set called 'forSubscription', you will need to create a method in your Table subclass as follows:

public function validationForSubscription($validator)
{
 return $validator
 ->add('email', 'valid-email', ['rule' => 'email'])
 ->add('password', 'valid', ['rule' => 'notBlank'])
 ->requirePresence('username');
}
$validator = $this->getValidator('forSubscription');

You can implement the method in validationDefault in your Table subclass should you wish to have a validation set that applies in cases where no other set is specified.

If a $name argument has not been provided, the default validator will be returned. You can configure your default validator name in a DEFAULT_VALIDATOR class constant.

Parameters

string|null $name

The name of the validation set to return.

Returns

\Cake\Validation\Validator

setValidator()

setValidator(string  $name, \Cake\Validation\Validator  $validator) : $this

This method stores a custom validator under the given name.

You can build the object by yourself and store it in your object:

$validator = new \Cake\Validation\Validator($table);
$validator
 ->add('email', 'valid-email', ['rule' => 'email'])
 ->add('password', 'valid', ['rule' => 'notBlank'])
 ->allowEmpty('bio');
$this->setValidator('forSubscription', $validator);

Parameters

string $name

The name of a validator to be set.

\Cake\Validation\Validator $validator

Validator object to be set.

Returns

$this

hasValidator()

hasValidator(string  $name) : boolean

Checks whether or not a validator has been set.

Parameters

string $name

The name of a validator.

Returns

boolean

validationDefault()

validationDefault(\Cake\Validation\Validator  $validator) : \Cake\Validation\Validator

Returns the default validator object. Subclasses can override this function to add a default validation set to the validator object.

Parameters

\Cake\Validation\Validator $validator

The validator that can be modified to add some rules to it.

Returns

\Cake\Validation\Validator

__construct()

__construct(\Cake\Event\EventManager|null  $eventManager = null) 

Constructor

Parameters

\Cake\Event\EventManager|null $eventManager

The event manager. Defaults to a new instance.

implementedEvents()

implementedEvents() : array

Get the Form callbacks this form is interested in.

The conventional method map is:

  • Form.buildValidator => buildValidator

Returns

array

schema()

schema(\Cake\Form\Schema|null  $schema = null) : \Cake\Form\Schema

Get/Set the schema for this form.

This method will call _buildSchema() when the schema is first built. This hook method lets you configure the schema or load a pre-defined one.

Parameters

\Cake\Form\Schema|null $schema

The schema to set, or null.

Returns

\Cake\Form\Schema

the schema instance.

buildValidator()

buildValidator(\Cake\Event\Event  $event, \Cake\Validation\Validator  $validator, string  $name) : void

Callback method for Form.buildValidator event.

Parameters

\Cake\Event\Event $event

The Form.buildValidator event instance.

\Cake\Validation\Validator $validator

The validator to customize.

string $name

Validator name

validate()

validate(array  $data) : boolean

Used to check if $data passes this form's validation.

Parameters

array $data

The data to check.

Returns

boolean —

Whether or not the data is valid.

errors()

errors() : array

Get the errors in the form

Will return the errors from the last call to validate() or execute().

Returns

array —

Last set validation errors.

getErrors()

getErrors() : array

Get the errors in the form

Will return the errors from the last call to validate() or execute().

Returns

array —

Last set validation errors.

setErrors()

setErrors(array  $errors) : $this

Set the errors in the form.

$errors = [
     'field_name' => ['rule_name' => 'message']
];

$form->setErrors($errors);

Parameters

array $errors

Errors list.

Returns

$this

execute()

execute(array  $data) : boolean

Execute the form if it is valid.

First validates the form, then calls the _execute() hook method. This hook method can be implemented in subclasses to perform the action of the form. This may be sending email, interacting with a remote API, or anything else you may need.

Parameters

array $data

Form data.

Returns

boolean —

False on validation failure, otherwise returns the result of the _execute() method.

getData()

getData(string|null  $field = null) : mixed

Get field data.

Parameters

string|null $field

The field name or null to get data array with all fields.

Returns

mixed

setData()

setData(array  $data) : $this

Set form data.

Parameters

array $data

Data array.

Returns

$this

__debugInfo()

__debugInfo() : array

Get the printable version of a Form instance.

Returns

array

createValidator()

createValidator(string  $name) : \Cake\Validation\Validator

Creates a validator using a custom method inside your class.

This method is used only to build a new validator and it does not store it in your object. If you want to build and reuse validators, use getValidator() method instead.

Parameters

string $name

The name of the validation set to create.

Throws

\RuntimeException

Returns

\Cake\Validation\Validator

validationMethodExists()

validationMethodExists(string  $name) : boolean

Checks if validation method exists.

Parameters

string $name

Validation method name.

Returns

boolean

_buildSchema()

_buildSchema(\Cake\Form\Schema  $schema) : \Cake\Form\Schema

A hook method intended to be implemented by subclasses.

You can use this method to define the schema using the methods on Cake\Form\Schema, or loads a pre-defined schema from a concrete class.

Parameters

\Cake\Form\Schema $schema

The schema to customize.

Returns

\Cake\Form\Schema

The schema to use.

_buildValidator()

_buildValidator(\Cake\Validation\Validator  $validator) : \Cake\Validation\Validator

A hook method intended to be implemented by subclasses.

You can use this method to define the validator using the methods on Cake\Validation\Validator or loads a pre-defined validator from a concrete class.

Parameters

\Cake\Validation\Validator $validator

The validator to customize.

Returns

\Cake\Validation\Validator

The validator to use.

_execute()

_execute(array  $data) : boolean

Hook method to be implemented in subclasses.

Used by execute() to execute the form's action.

Parameters

array $data

Form data.

Returns

boolean