Skip to content

Instantly share code, notes, and snippets.

@basz
Last active December 13, 2015 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save basz/4986430 to your computer and use it in GitHub Desktop.
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?
'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