public function PostalCodeWidget::validate in Postal Code 8
File
- src/
Plugin/ Field/ FieldWidget/ PostalCodeWidget.php, line 93
Class
- PostalCodeWidget
- Plugin implementation of the 'postal_code' widget.
Namespace
Drupal\postal_code\Plugin\Field\FieldWidgetCode
public function validate($element, FormStateInterface $form_state) {
$field_settings = $this
->getFieldSettings();
$validator = $this->postalCodeValidation;
$config = $this->config;
$value = trim($element['#value']);
if (!empty($value) && $config
->get('validate')) {
// Locate 'postal_type' in the form.
$country_code = $field_settings['country_select'];
if (!empty($country_code)) {
if ($country_code != 'any') {
$error_array = $validator
->validate($country_code, $value);
}
else {
$validatable_countries = $config
->get('valid_countries');
foreach ($validatable_countries as $key => $country) {
$err_array[] = $validator
->validate($country, $value);
}
foreach ($err_array as $k => $v) {
$error_array[] = $v[0];
}
}
}
else {
$form_state
->setError($element, $this
->t('This form has been altered in a way in which Postal Code validation will not work, but the validation option remains enabled. Please correct the changes to the form or disable the validation option.'));
}
if (!in_array(TRUE, $error_array)) {
$form_state
->setError($element, $this
->t('Invalid Postal Code Provided.'));
}
}
}