Quote from: airoctive at Dec 22, 2008, 08:08 PM
Where can I find some sample code or documentation about using xPDOObject validation. i.e. validation usage and rule creation/generation
Documentation efforts are in progress, but here’s some basic information...
Schema-defined validation rules
You can, at build time, have validation rules defined in your xPDO schema which are generated into the object map file. Here is an example from the sample model available in SVN:
<validation class="validation.xPDOValidator">
<rule field="dob" name="date_format" type="preg_match" rule="/\d{4}-\d{2}-\d{2}/" />
<rule field="password" name="password_length" type="xPDOValidationRule" rule="xPDOMinLengthValidationRule" value="6" />
</validation>
Programmatic validation rules
You can also add validation rules via the API. Here’s an example using the modX class, which extends xPDO:
<?php
$object = $modx->newObject('modDocument');
$object->set('alias', '1');
$object->addValidationRule('alias', 'alias_match', 'preg_match', '/^123/', array('message' => 'Invalid alias'));
$object->addValidationRule('alias', 'alias_length', 'xPDOValidationRule', 'xPDOMinLengthValidationRule', array('value' => 2, 'message' => 'Alias too short'));
$object->validate();
?>
Manual validation vs. validation on save
Regardless of whether you have pre-defined rules in your object, or have added them dynamically at run-time in a script, you can then use manual validation and/or auto-validation that occurs on save. Using our example programmatic rules from above, you can now simply do the following to find out if it’s valid:
<?php
$valid = $object->validate();
if (!$valid) {
// handle the exception
}
?>
Additionally, you can inspect the results of validation and/or messages generated by the process easily by using the object’s validator:
<?php
$validator = $object->getValidator();
if (!$object->validate()) {
$results = $validator->getResults();
$messages = $validator->getMessages();
// do what you want with the specific results and/or messages validate() generated...
}
?>
Also, xPDO allows you to control validation when objects are saved via the XPDO_OPT_VALIDATE_ON_SAVE option. If this option is passed as true as in the following example, then validate() will be run on the object when save() is called and if rules fail, save will not continue:
<?php
$myModel = new xPDO(
$database_dsn,
$database_user,
$database_password,
array (
XPDO_OPT_CACHE_PATH => '/path/to/cache/dir/',
XPDO_OPT_TABLE_PREFIX => 'mymodel_',
XPDO_OPT_HYDRATE_FIELDS => true,
XPDO_OPT_HYDRATE_RELATED_OBJECTS => true,
XPDO_OPT_HYDRATE_ADHOC_FIELDS => true,
XPDO_OPT_VALIDATE_ON_SAVE => true,
),
array (
PDO_ATTR_ERRMODE => PDO_ERRMODE_SILENT,
PDO_ATTR_PERSISTENT => false,
PDO_MYSQL_ATTR_USE_BUFFERED_QUERY => true
)
);
$this->setPackage('mymodel', '/path/to/mymodel/root/dir/');
?>
You can also override the xPDOValidator class, but I’ll save that for the documentation. You can see the modValidator class for an example though.
Validation rule types
Also, validation rules can be one of three basic types:
- callable - Any callable PHP function.
- preg_match - Define a simple preg_match pattern.
- xPDOValidationRule - Use the pre-defined derivatives or extend xPDOValidationRule yourself to create custom rule classes which handle their own parameters and can easily interact with the xPDOValidator class that is responsible for executing the validation process.