Last active
December 13, 2015 22:39
-
-
Save basz/4986430 to your computer and use it in GitHub Desktop.
Postcode validator won't work when the postcode is unknown to it. (eg. Suriname does not have postcodes) and is not listed in the validator internal regularExpression array. So I would like to disable the requirement on it when such an exception is thrown... Is that possible?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'postcode' => array( | |
'filters' => array( | |
array('name' => 'StringTrim'), | |
array('name' => 'StripTags') | |
), | |
'validators' => array( | |
array( | |
'name' => 'Callback', | |
'options' => array( | |
'callback' => function ($value, $context = null) use ($postcodeValidator) { | |
if (!isset($context['country']) || empty($context['country'])) { | |
return true; // return true if no validation is necessary | |
} | |
$postcodeValidator->setLocale('xx_' . strtoupper($context['country'])); | |
// throws InvalidArgumentException("A postcode-format string has to be given for validation") | |
// I can catch and return true, but that leaves the fiels open for any input but nothing | |
$result = $postcodeValidator->isValid($value); | |
return $result; | |
}, | |
'messages' => array( | |
'callbackValue' => translate('Does not appear to be a valid postal code for this country'), | |
) | |
), | |
), | |
), | |
), | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment