On this page
Class Form
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.
Properties summary
-
$_errors
protectedarray
-
$_schema
protectedCake\Form\Schema;
-
$_validator
protectedCake\Validation\Validator;
Method Summary
-
__debugInfo() publicGet the printable version of a Form instance.
-
_buildSchema() protectedA hook method intended to be implemented by subclasses.
-
_buildValidator() protectedA hook method intended to be implemented by subclasses.
-
_execute() protectedHook method to be implemented in subclasses.
-
errors() publicGet the errors in the form
-
execute() publicExecute the form if it is valid.
-
schema() publicGet/Set the schema for this form.
-
validate() publicUsed to check if $data passes this form's validation.
-
validator() publicGet/Set the validator for this form.
Method Detail
__debugInfo()source public
__debugInfo( )
Get the printable version of a Form instance.
Returns
array_buildSchema()source protected
_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.
Parameters
Cake\Form\Schema
$schema
- The schema to customize.
Returns
Cake\Form\Schema
The schema to use.
_buildValidator()source protected
_buildValidator( Cake\Validation\Validator $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()source protected
_execute( array $data )
Hook method to be implemented in subclasses.
Used by execute()
to execute the form's action.
Parameters
-
array
$data
- Form data.
Returns
booleanerrors()source public
errors( )
Get the errors in the form
Will return the errors from the last call to validate()
or execute()
.
Returns
arrayLast set validation errors.
execute()source public
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.
Parameters
-
array
$data
- Form data.
Returns
booleanFalse on validation failure, otherwise returns the result of the _execute()
method.
schema()source public
schema( Cake\Form\Schema $schema null )
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
$schema
optional null- The schema to set, or null.
Returns
Cake\Form\Schema
the schema instance.
validate()source public
validate( array $data )
Used to check if $data passes this form's validation.
Parameters
-
array
$data
- The data to check.
Returns
booleanWhether or not the data is valid.
validator()source public
validator( Cake\Validation\Validator $validator null )
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
$validator
optional null- The validator to set, or null.
Returns
Cake\Validation\Validator
the validator instance.
Properties detail
© 2005–2016 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.
http://api.cakephp.org/3.2/class-Cake.Form.Form.html