Validation in v1.1 of CakePHP was quite simple, and many found it lacking in features and flexibility. This is evidenced by the number of alternatives that people have written such as Daniel Hofstetter, Evan Sagge and Adeel Khan’s ruby-esque approach.
However in CakePHP 1.2 there has been a major rework of the Validation class’s inner workings, and the way Model::invalidFields() works.
The New $validate
The new $validate can take a number of different constructs now. There are 3 ways to define your validation rules and you can mix and match as needed.
Construct 1: CakePHP 1.1 way
The old CakePHP 1.1 $validate construct will still work
var $validate = array('fieldName' => 'ruleName')
Construct 2: Single Rule per field
You can now define more complex rules using the following construct. (Parameters explained later)
var $validate = array(
'fieldName' => array(
'rule' => 'ruleName' // or 'rule' => array('ruleName', 'param1', 'param2' ...)
'required' => true,'allowEmpty' => false,
'on' => 'create', // or update
'message' => 'Your Error Message'
)
);
Constrct 3: Multiple Rules per field
Similar to Construct 2, however you can define multiple validation rules for a single field.
var $validate = array(
'fieldName' => array(
'rule_index' => array(
'rule' => 'ruleName' // or 'rule' => array('ruleName', 'param1', 'param2' ...)
'required' => true,'allowEmpty' => false,
'on' => 'create', // or update
'message' => 'Your Error Message'
)
)
);
The new Parameters
The new $validate supports a number of parameters. The only required parameter is ‘rule’.
rule – mixed:
The Rule parameter defines the validation method and takes either a single value or an array. Rule may be (in order of preference) a method of your model, a method of the Validation class or a Regular Expression. If Rule is an array and ‘ruleName’ is a method, all other members of the array will be passed to the method. eg.
var $validate = array('username' => array('rule' => array('between', 6, 20)));
allowEmpty – bool:
This defines the behaviour when an empty value for the field is found. ‘allowEmpty’ => false will cause the validation to fail when the field data is empty. N.B: This rule is only enforced when there is an actual fieldName index in the data array.
Default is false.
required – bool:
‘required’ => true means that an index with fieldName must exist in the data array, i.e. validation will fail if isset($data['ModelName']['fieldName']) fails. N.B Required does not care if the value is empty – see allowEmpty above.
Default is false.
on – string (‘create’ or ‘update’):
If on is defined , the validation rule will only be applied on model ‘create’ or ‘update’. If not defined it is applied everytime. This may be useful for situations such as created_by is required on record creation, but on update it must not be defined.
Default is null, i.e. apply rule everytime.
message – string:
Message is the error message that will be stored in validationErrors. If message is not define, it will attempt to use rule_index (in Construct 3) if it is a string, otherwise it will default to ‘This field cannot be left blank’.
There is also another parameter ‘last’ defined in the code but it is not used as yet. Not sure what it’s use will be either.
A Full Example
This is the actual validate variable from my User model. It contains a mixture of the above constructs and techniques.
var $validate = array(
'username' => array(
VALID_NOT_EMPTY,
'alphanumeric' => array(
'rule' => 'alphanumeric',
'message' => 'Username may only consist of letter and numbers'),
'length' => array(
'rule' => array('between', 6, 20),
'message' => 'Username must be between 6 and 20 characters in length')
),
'name' => VALID_NOT_EMPTY,
'email' => array(
'Invalid email format' => VALID_EMAIL,
VALID_NOT_EMPTY
),
'passwrd' => array(
VALID_NOT_EMPTY,
'length' => array(
'rule' => array('minLength', 6),
'message' => 'Password must be at least 6 characters in length'),
'strong' => array(
'rule' => 'isStrong',
'message' => 'Your password is not strong enough')
)
);
Disclaimer: My knowledge of the new validation was gained from the Bakery Article Multiple Rules of Validation, CakeBaker’s article Validation with CakePHP 1.2 and mostly from looking at the source code. If I have interpreted something incorrectly, please let me know.
Recent Comments