Skip to content

Commit

Permalink
An example on how do dynamically forward
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Scherer committed May 19, 2015
1 parent 8d3fbdb commit a2eb367
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions README.md
Expand Up @@ -56,9 +56,31 @@ For further information on validation rules see the [cakephp documentation on va
You can also access the localized validators any time you would call `Validation` methods. After importing the validation class.

```php
if (Validation::postal($value, null, 'cz')) {
// Do something with valid postal code
}
if (Validation::postal($value, null, 'cz')) {
// Do something with valid postal code
}
```

## Dynamic forwarding of validation
Using a snippet like the following could reduce the manual include/setup part when using multiple localizations:
```php
// Inside your custom validation rule validatePostal()
// $country (de, at, ...) and $value (12345, 1234, ...) given
$className = ucfirst($country) . 'Validation';
App::uses($className, 'Localized.Validation');

// Skip if we don't have that country, then only check for existence
if (!class_exists($className) || !method_exists($className, 'postal')) {
return !empty($value);
}

try {
$result = Validation::postal($value, null, $country);
} catch (NotImplementedException $e) {
$result = !empty($value);
}

return $result;
```

## PO files
Expand Down

0 comments on commit a2eb367

Please sign in to comment.