This recipe shows how to not only use some basic validation rules provided by CakePHP, but also how to use more than one of these rules per field.
To go through this recipe we need a sample table to work with. Create a table named profiles
using the following SQL statement:
CREATE TABLE `profiles`( `id` INT UNSIGNED AUTO_INCREMENT NOT NULL, `email` VARCHAR(255) NOT NULL, `name` VARCHAR(255) default NULL, `twitter` VARCHAR(255) default NULL, PRIMARY KEY(`id`) );
We proceed now to create the required model. Create the model Profile
in a file named profile.php
and place it in your app/models
folder with the following contents:
<?php class Profile extends AppModel { public $validate = array( 'email' => array('rule' => 'notEmpty'), 'name' => array('rule' => 'notEmpty') ); } ?>
Create its appropriate controller ProfilesController
in a file named profiles_controller.php
and place it in your app/controllers
folder with the following contents:
<?php class ProfilesController extends AppController { public function add() { if (!empty($this->data)) { $this->Profile->create(); if ($this->Profile->save($this->data)) { $this->Session->setFlash('Profile created'), $this->redirect('/'), } else { $this->Session->setFlash('Please correct the errors'), } } } } ?>
Create a folder named profiles
in your app/views
folder. Create the view to hold the form in a file named add.ctp
, and place it in your app/views/profiles
folder, with the following contents:
<?php echo $this->Form->create(); echo $this->Form->inputs(array( 'email', 'name', 'twitter' )); echo $this->Form->end('Create'), ?>
We already have basic validation rules set for the email
and name
fields, which guarantee that none of these fields can be empty. We now want to add another validation rule to ensure that the email entered is always a valid e-mail address. Edit the Profile
model and change the defined validation rule as follows:
class Profile extends AppModel {
public $validate = array(
'email' => array(
'valid' => array(
'rule' => 'email',
'message' => 'The email entered is not a valid email address'
),
'required' => array(
'rule' => 'notEmpty',
'message' => 'Please enter an email'
)
),
'name' => array('rule' => 'notEmpty')
);
}
If we now browse to http://localhost/profiles/add
and click the Create button without entering any information, we should see the customized error message for the email
field and the default error message for the name
field as shown in the following screenshot:
If we instead specify an invalid e-mail address, the validation message should change to the one specified in the view.
Each field specified in the model's validate
property can contain any number of validation rules. When we specify more than one rule, we wrap them in an array, indexing it with a descriptive key to help us identify which rule failed. Therefore, we chose to index the notEmpty
rule with a required
key, and the email
rule with a valid
key.
When we specify more than one validation rule, CakePHP will evaluate each rule in the order we used when adding them to the validate
property. If more than one validation rule fails for a field, the last rule that failed is the one that is used to trigger the error message. In our case, the first rule is valid
, and the second one required
. Therefore if both rules fail, the field is set to have failed the required
rule.
If we wanted to ensure that a particular rule is executed after all others have, we use the last
rule setting. Setting it to true
will ensure that a particular rule is executed after all others. In our example, we could have defined the required
validation first in the list of rules for the email
field and set its last
setting to true
, which would have the same result as defining the required
rule after all others.
In this recipe, we used the model to specify what error message is shown for each failing rule. We could instead choose to do it in the view.
Using the indexes that identify each rule, we can specify which error message should be shown whenever one of these rules fails validation. We do so by setting the error
option in the field definition to an array of error messages, each indexed by a matching validation rule key (in our case, one of required
and valid
for the email
field).
Edit the app/views/profiles/add.ctp
file and change the email
field definition as follows:
<?php
echo $this->Form->create();
echo $this->Form->inputs(array(
'email' => array(
'error' => array(
'required' => 'Please enter an email',
'valid' => 'The email entered is not a valid email address'
)
),
'name',
'twitter'
));
echo $this->Form->end('Create'),
?>
52.14.39.59