public static function Zone::validateZone in Address 8
Validates the zone.
Parameters
array $element: The form element.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
File
- src/
Element/ Zone.php, line 195
Class
- Zone
- Provides a zone form element.
Namespace
Drupal\address\ElementCode
public static function validateZone(array $element, FormStateInterface $form_state) {
$value = $form_state
->getValue($element['#parents']);
// Remove empty territories, unneeded keys.
foreach ($value['territories'] as $index => $territory) {
if (empty($territory['country_code'])) {
unset($value['territories'][$index]);
}
unset($territory['remove']);
unset($territory['add_territory']);
}
$value['territories'] = array_filter($value['territories']);
$form_state
->setValue($element['#parents'], $value);
// Required zones must always have a territory.
// @todo Invent a nicer UX for optional zones.
if ($element['#required'] && empty($value['territories'])) {
$form_state
->setError($element['territories'], t('Please add at least one territory.'));
}
}