function _webform_edit_grid_unique_validate in Webform 7.4
A Form API element validate function to check that all choices are unique.
1 string reference to '_webform_edit_grid_unique_validate'
- _webform_render_grid in components/
grid.inc - Implements _webform_render_component().
File
- components/
grid.inc, line 645 - Webform module grid component.
Code
function _webform_edit_grid_unique_validate($element) {
// Grids may contain nested multiple value select components.
// Create a flat array of values.
$values = array();
$element['#value'] = (array) $element['#value'];
array_walk_recursive($element['#value'], function ($a) use (&$values) {
$values[] = $a;
});
$nr_unique = count(array_unique($values));
$nr_values = count($values);
$nr_possible = count($element['#grid_options']);
if (strlen($element['#grid_default']) && isset($element['#grid_options'][$element['#grid_default']])) {
// A default is defined and is one of the options. Don't count default values
// toward uniqueness.
$nr_defaults = count(array_keys($element['#value'], $element['#grid_default']));
if ($nr_defaults) {
$nr_values -= $nr_defaults;
$nr_unique--;
}
}
if ($nr_unique < $nr_values && $nr_unique < $nr_possible) {
// Fewer unique values than values means that at least one value is duplicated.
// Fewer unique values than possible values means that there is a possible choice
// that wasn't used.
form_error($element, t('!title is not allowed to have the same answer for more than one question.', array(
'!title' => $element['#title'],
)));
}
}