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.
This class is most useful when subclassed. In a subclass you should define the _buildSchema
, validationDefault
and optionally, the _execute
methods. These allow you to declare your form's fields, validation and primary action respectively.
Forms are conventionally placed in the App\Form
namespace.
string
'Form.buildValidator'
string
'default'
string
'form'
array
Form's data.
array
The errors if any
string
Default class name for new event objects.
\Cake\Event\EventManagerInterface
Instance of the Cake\Event\EventManager this object is using to dispatch inner events.
\Cake\Form\Schema
The schema used by this form.
string
Schema class.
string
Validator class.
\Cake\Validation\Validator[]
A list of validation objects indexed by name
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.
Returns the default validator object. Subclasses can override this function to add a default validation set to the validator object.
__construct(?\Cake\Event\EventManager $eventManager)
Constructor
\Cake\Event\EventManager|null
$eventManager optional The event manager. Defaults to a new instance.
__debugInfo()
Get the printable version of a Form instance.
array
_buildSchema(\Cake\Form\Schema $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.
\Cake\Form\Schema
$schema The schema to customize.
\Cake\Form\Schema
The schema to use.
_execute(array $data)
Hook method to be implemented in subclasses.
Used by execute()
to execute the form's action.
array
$data Form data.
bool
createValidator(string $name)
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.
string
$name The name of the validation set to create.
\Cake\Validation\Validator
RuntimeException
dispatchEvent(string $name, ?array $data, ?object $subject)
Wrapper for creating and dispatching events.
Returns a dispatched event.
string
$name Name of the event.
array|null
$data optional Any value you wish to be transported with this event to it can be read by listeners.
object|null
$subject optional The object that this event applies to ($this by default).
\Cake\Event\EventInterface
execute(array $data)
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.
array
$data Form data.
bool
False on validation failure, otherwise returns the result of the _execute()
method.
getData(?string $field)
Get field data.
string|null
$field optional The field name or null to get data array with all fields.
mixed
getErrors()
Get the errors in the form
Will return the errors from the last call to validate()
or execute()
.
array
Last set validation errors.
getEventManager()
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.
\Cake\Event\EventManagerInterface
getSchema()
Get 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.
\Cake\Form\Schema
the schema instance.
getValidator(?string $name)
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.
string|null
$name optional The name of the validation set to return.
\Cake\Validation\Validator
hasValidator(string $name)
Checks whether or not a validator has been set.
string
$name The name of a validator.
bool
implementedEvents()
Get the Form callbacks this form is interested in.
The conventional method map is:
array
schema(?\Cake\Form\Schema $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.
\Cake\Form\Schema|null
$schema optional The schema to set, or null.
\Cake\Form\Schema
the schema instance.
set(mixed $name, mixed $value)
Saves a variable or an associative array of variables for use inside form data.
string|array
$name The key to write, can be a dot notation value. Alternatively can be an array containing key(s) and value(s).
mixed
$value optional Value to set for var
$this
setData(array $data)
Set form data.
array
$data Data array.
$this
setErrors(array $errors)
Set the errors in the form.
$errors = [ 'field_name' => ['rule_name' => 'message'] ]; $form->setErrors($errors);
array
$errors Errors list.
$this
setEventManager(\Cake\Event\EventManagerInterface $eventManager)
Returns the Cake\Event\EventManagerInterface 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.
\Cake\Event\EventManagerInterface
$eventManager the eventManager to set
$this
setSchema(\Cake\Form\Schema $schema)
Set the schema for this form.
\Cake\Form\Schema
$schema The schema to set
$this
setValidator(string $name, \Cake\Validation\Validator $validator)
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);
string
$name The name of a validator to be set.
\Cake\Validation\Validator
$validator Validator object to be set.
$this
validate(array $data)
Used to check if $data passes this form's validation.
array
$data The data to check.
bool
Whether or not the data is valid.
validationDefault(\Cake\Validation\Validator $validator)
Returns the default validator object. Subclasses can override this function to add a default validation set to the validator object.
\Cake\Validation\Validator
$validator The validator that can be modified to add some rules to it.
\Cake\Validation\Validator
validationMethodExists(string $name)
Checks if validation method exists.
string
$name Validation method name.
bool
Form's data.
array
The errors if any
array
Default class name for new event objects.
string
Instance of the Cake\Event\EventManager this object is using to dispatch inner events.
\Cake\Event\EventManagerInterface
The schema used by this form.
\Cake\Form\Schema
Schema class.
string
Validator class.
string
A list of validation objects indexed by name
\Cake\Validation\Validator[]
© 2005–present The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/4.1/class-Cake.Form.Form.html