Commit 3bf487f4 authored by alexpott's avatar alexpott

Issue #2633678 by mikeker, dagmar, geertvd, Lendude: Improve grouped filter...

Issue #2633678 by mikeker, dagmar, geertvd, Lendude: Improve grouped filter form and fix validation problems
parent 57432ad4
......@@ -98,32 +98,23 @@ public function validateValidTime(&$form, FormStateInterface $form_state, $opera
}
/**
* Validate the build group options form.
* {@inheritdoc}
*/
protected function buildGroupValidate($form, FormStateInterface $form_state) {
// Special case to validate grouped date filters, this is because the
// $group['value'] array contains the type of filter (date or offset)
// and therefore the number of items the comparison has to be done
// against 'one' instead of 'zero'.
foreach ($form_state->getValue(array('options', 'group_info', 'group_items')) as $id => $group) {
if (empty($group['remove'])) {
// Check if the title is defined but value wasn't defined.
if (!empty($group['title'])) {
if ((!is_array($group['value']) && empty($group['value'])) || (is_array($group['value']) && count(array_filter($group['value'])) == 1)) {
$form_state->setError($form['group_info']['group_items'][$id]['value'], $this->t('The value is required if title for this item is defined.'));
}
}
// Check if the value is defined but title wasn't defined.
if ((!is_array($group['value']) && !empty($group['value'])) || (is_array($group['value']) && count(array_filter($group['value'])) > 1)) {
if (empty($group['title'])) {
$form_state->setError($form['group_info']['group_items'][$id]['title'], $this->t('The title is required if value for this item is defined.'));
}
}
}
protected function hasValidGroupedValue(array $group) {
if (!is_array($group['value']) || empty($group['value'])) {
return FALSE;
}
}
// Special case when validating grouped date filters because the
// $group['value'] array contains the type of filter (date or offset) and
// therefore the number of items the comparison has to be done against is
// one greater.
$operators = $this->operators();
$expected = $operators[$group['operator']]['values'] + 1;
$actual = count(array_filter($group['value'], 'static::arrayFilterZero'));
return $actual == $expected;
}
public function acceptExposedInput($input) {
if (empty($this->options['exposed'])) {
......
......@@ -618,6 +618,38 @@ public function validateExposeForm($form, FormStateInterface $form_state) {
$this->validateIdentifier($identifier, $form_state, $form['expose']['identifier']);
}
/**
* Determines if the given grouped filter entry has a valid value.
*
* @param array $group
* A group entry as defined by buildGroupForm().
*
* @return bool
*/
protected function hasValidGroupedValue(array $group) {
$operators = $this->operators();
if ($operators[$group['operator']]['values'] == 0) {
// Some filters, such as "is empty," do not require a value to be
// specified in order to be valid entries.
return TRUE;
}
else {
if (is_string($group['value'])) {
return trim($group['value']) != '';
}
elseif (is_array($group['value'])) {
// Some filters allow multiple options to be selected (for example, node
// types). Ensure at least the minimum number of values is present for
// this entry to be considered valid.
$min_values = $operators[$group['operator']]['values'];
$actual_values = count(array_filter($group['value'], 'static::arrayFilterZero'));
return $actual_values >= $min_values;
}
}
return FALSE;
}
/**
* Validate the build group options form.
*/
......@@ -628,26 +660,21 @@ protected function buildGroupValidate($form, FormStateInterface $form_state) {
}
if ($group_items = $form_state->getValue(array('options', 'group_info', 'group_items'))) {
$operators = $this->operators();
foreach ($group_items as $id => $group) {
if (empty($group['remove'])) {
// Check if the title is defined but value wasn't defined.
if (!empty($group['title']) && $operators[$group['operator']]['values'] > 0) {
if ((!is_array($group['value']) && trim($group['value']) == "") ||
(is_array($group['value']) && count(array_filter($group['value'], 'static::arrayFilterZero')) == 0)) {
$form_state->setError($form['group_info']['group_items'][$id]['value'], $this->t('The value is required if title for this item is defined.'));
$has_valid_value = $this->hasValidGroupedValue($group);
if ($has_valid_value && $group['title'] == '') {
$operators = $this->operators();
if ($operators[$group['operator']]['values'] == 0) {
$form_state->setError($form['group_info']['group_items'][$id]['title'], $this->t('A label is required for the specified operator.'));
}
}
// Check if the value is defined but title wasn't defined.
if ((!is_array($group['value']) && trim($group['value']) != "") ||
(is_array($group['value']) && count(array_filter($group['value'], 'static::arrayFilterZero')) > 0)) {
if (empty($group['title'])) {
$form_state->setError($form['group_info']['group_items'][$id]['title'], $this->t('The title is required if value for this item is defined.'));
else {
$form_state->setError($form['group_info']['group_items'][$id]['title'], $this->t('A label is required if the value for this item is defined.'));
}
}
if (!$has_valid_value && $group['title'] != '') {
$form_state->setError($form['group_info']['group_items'][$id]['value'], $this->t('A value is required if the label for this item is defined.'));
}
}
}
}
......@@ -1468,19 +1495,6 @@ public function canGroup() {
return TRUE;
}
/**
* Filter by no empty values, though allow to use "0".
*
* @param string $var
* The variable to evaluate.
*
* @return bool
* TRUE if the value is equal to an empty string, FALSE otherwise.
*/
protected static function arrayFilterZero($var) {
return trim($var) != '';
}
/**
* {@inheritdoc}
*/
......@@ -1518,6 +1532,22 @@ public function validate() {
}
}
/**
* Filter by no empty values, though allow the use of (string) "0".
*
* @param string $var
* The variable to evaluate.
*
* @return bool
* TRUE if the value is equal to an empty string, FALSE otherwise.
*/
protected static function arrayFilterZero($var) {
if (is_int($var)) {
return $var != 0;
}
return trim($var) != '';
}
}
/**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment