public static function WebformElementBase::validateUniqueMultiple in Webform 8.5
Same name and namespace in other branches
- 6.x src/Plugin/WebformElementBase.php \Drupal\webform\Plugin\WebformElementBase::validateUniqueMultiple()
Form API callback. Validate element #unique multiple values.
File
- src/
Plugin/ WebformElementBase.php, line 2111
Class
- WebformElementBase
- Provides a base class for a webform element.
Namespace
Drupal\webform\PluginCode
public static function validateUniqueMultiple(array &$element, FormStateInterface $form_state) {
if (!isset($element['#unique'])) {
return;
}
$name = $element['#name'];
$value = NestedArray::getValue($form_state
->getValues(), $element['#parents']);
if (empty($value)) {
return;
}
// Compare number of values to unique number of values.
if (count($value) !== count(array_unique($value))) {
$duplicates = WebformArrayHelper::getDuplicates($value);
if (isset($element['#unique_error'])) {
$form_state
->setError($element, WebformHtmlHelper::toHtmlMarkup($element['#unique_error']));
}
elseif (isset($element['#title'])) {
$t_args = [
'%name' => empty($element['#title']) ? $name : $element['#title'],
'%value' => reset($duplicates),
];
$form_state
->setError($element, t('The value %value has already been submitted once for the %name element. You may have already submitted this webform, or you need to use a different value.', $t_args));
}
else {
$form_state
->setError($element);
}
}
}