diff --git a/README.md b/README.md index 7e2fd250..5567ff9b 100644 --- a/README.md +++ b/README.md @@ -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