public static function MultiValue::validateMultiValue in SAML Authentication 4.x
Same name and namespace in other branches
- 8.3 src/Element/MultiValue.php \Drupal\samlauth\Element\MultiValue::validateMultiValue()
Validates a multi-value form element.
Used to clean and sort the submitted values in the form state.
Parameters
array $element: The element being processed.
\Drupal\Core\Form\FormStateInterface $form_state: The current form state.
array $complete_form: The complete form.
File
- src/
Element/ MultiValue.php, line 276
Class
- MultiValue
- Provides a multi-value form element.
Namespace
Drupal\samlauth\ElementCode
public static function validateMultiValue(array &$element, FormStateInterface $form_state, array &$complete_form) : void {
$input_exists = FALSE;
$values = NestedArray::getValue($form_state
->getValues(), $element['#parents'], $input_exists);
if (!$input_exists) {
return;
}
// Remove the 'value' of the 'add more' button.
unset($values['add_more']);
// Sort the values based on the weight.
usort($values, function ($a, $b) {
return SortArray::sortByKeyInt($a, $b, '_weight');
});
foreach ($values as $delta => &$delta_values) {
// Remove the weight element value from the submitted data.
unset($delta_values['_weight']);
// Determine if all the elements of this delta are empty.
$is_empty_delta = array_reduce($delta_values, function (bool $carry, $value) : bool {
if (is_array($value)) {
return $carry && empty(array_filter($value));
}
else {
return $carry && ($value === NULL || $value === '');
}
}, TRUE);
// If all the elements are empty, drop this delta.
if ($is_empty_delta) {
unset($values[$delta]);
}
}
// Re-key the elements so that deltas are consecutive.
$values = array_values($values);
// Set the value back to the form state.
$form_state
->setValueForElement($element, $values);
}