The Validation package provides simple evaluation of user input via easy-to-make Rules classes. The Rules classes extend the basic functionality of the Validation_Component class.
The Validation_Component provides the core of the validation process. The following validation features are inherent:
Additionally, the Validation_Component provides the following abstract methods that you must extend in one or more Rules classes:
Lastly, the following stub methods are supplied, and can be optionally extended in a Rule class:
A Rule class is used to extend the Validation_Component, and provides any extra values needed to validate a specific field type. Several Rule classes are included in the default installation of the Validation Package, and are demonstrated in the Validation_Test_Controller:
On construct, a Rule is supplied with the value of the field to be validated. On the subsequent call to the Validation_Component::run() method, the body of the validation() method specified in the Rule class will be executed. Additional checks are performed automatically by the Validation_Component, and the outcome of these tests depends on the setup established in the Rule class.
The Validation_Component class defines a public member, 'required'. By default, this value is set to boolean FALSE. After creating an instance of a Rule, this value can be set to boolean TRUE. Doing so will make this field required. The Validation_Component::check_required() method is executed during the run() method, and if the field has an empty string or NULL value, it will return FALSE (for invalid) or TRUE (valid) otherwise.
The extending Rule class can override the 'required' member and set it to TRUE, in which case all instances of the Rule will treat the field as required by default.
The Validation_Component class defines an empty public member array, 'range'. This should be overriden in any extending Rule class and provided one or two numeric values. If only one numeric value is supplied, then the Validation_Component::check_range() will check if the value is exactly that length. If two numeric values are supplied, the Validation_Component::check_range() method will check if the value is of a length between (inclusive) the two numeric values.
The Validation_Component class defines a stub method, clean(). This method lacks any logic, and simply returns boolean TRUE. An extending Rule class may override this method and clean the field as desired.
The Validation_Component class also provides a standard_cleaners() method, which applies some simple cleaning to the field during the run() method. This method can be overriden in the extending Rule class.
The Validation_Component::run() method performs its validation in the following order:
In the event that all of the tests pass, the Validation_Component 'valid' member will contain boolean TRUE. If any of the checks fail, the 'valid' member will be boolean FALSE. The value of Validation_Component::$valid will be returned at the end of the run() method.
All of the Validation_Component methods, except for run(), are declared public, so they may be called independently of the run() method, which may be useful in some situations.
During the validation process, any errors generated by the Validation_Component, or any of the predefined Rule classes, will be stored in the Validation_Component::$errors array. It is recommended that any Rule classes extending the Validation_Component supply errors to this array from within the validate() method, in the event that something goes wrong. Doing so makes debugging and logging much easier.
© Copyright 2010 negative11.com