From ba8b78706aa9d06a3620224fa5786fa31b78c6bd Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Mon, 12 Jul 2021 17:57:07 +0200 Subject: [PATCH] jsonCompositeValue & supannCompositeValue: fix checking value method Use LSformRule::validate_values() to check value instead of reimplementing incomplete checking method. --- ...s.LSformElement_jsonCompositeAttribute.php | 91 +++++++++---------- ...LSformElement_supannCompositeAttribute.php | 23 +++-- 2 files changed, 52 insertions(+), 62 deletions(-) diff --git a/src/includes/class/class.LSformElement_jsonCompositeAttribute.php b/src/includes/class/class.LSformElement_jsonCompositeAttribute.php index 4d2f9cde..ca8c98d7 100644 --- a/src/includes/class/class.LSformElement_jsonCompositeAttribute.php +++ b/src/includes/class/class.LSformElement_jsonCompositeAttribute.php @@ -363,68 +363,59 @@ class LSformElement_jsonCompositeAttribute extends LSformElement { * @retval void **/ private function checkComponentValues($c, $value) { - if ($this -> getComponentConfig($c, 'multiple', false, 'bool')) { - foreach ($value as $val) { - $this -> checkComponentValue($c, $val); + // Check select_list component's values + if ($this -> getComponentConfig($c, 'type') == 'select_list') { + if ($this -> getComponentConfig($c, 'multiple', false, 'bool')) { + foreach ($value as $val) { + $this -> checkSelectListComponentValue($c, $val); + } + } + else + $this -> checkSelectListComponentValue($c, $value); + } + + // Apply check data rules + LSsession :: loadLSclass('LSformRule', null, true); + foreach($this -> getComponentConfig($c, 'check_data', array(), 'array') as $ruleType => $rconf) { + $errors = LSformRule :: validate_values($ruleType, $value, $rconf, $this); + if (is_array($errors)) { + $retval = false; + foreach ($errors as $error) { + $this -> form -> setElementError( + $this -> attr_html, + getFData( + __('%{label}: %{error}'), + array( + 'label' => __($this -> getComponentConfig($c, 'label')), + 'error' => $error, + ) + ) + ); + } } } - else - $this -> checkComponentValue($c, $value); } /** - * Check one component's value + * Check one select_list component's value * * @param[] $c The component name * @param[] $value The value to check * * @retval void **/ - private function checkComponentValue($c, $value) { - $label = __($this -> getComponentConfig($c, 'label')); - - // select_list components : check values - if ($this -> getComponentConfig($c, 'type') == 'select_list') { - if (!$this -> getSelectListComponentValueLabel($c, $value)) { - $this -> form -> setElementError( - $this -> attr_html, - getFData( - _('Invalid value "%{value}" for component %{component}.'), - array('value' => $value, 'component' => $label) + private function checkSelectListComponentValue($c, $value) { + if (!$this -> getSelectListComponentValueLabel($c, $value)) { + $this -> form -> setElementError( + $this -> attr_html, + getFData( + _('Invalid value "%{value}" for component %{component}.'), + array( + 'value' => $value, + 'component' => __($this -> getComponentConfig($c, 'label')) ) - ); - } - } - - // Apply check data rules - foreach($this -> getComponentConfig($c, 'check_data', array(), 'array') as $ruleType => $rconf) { - $className = 'LSformRule_'.$ruleType; - if (LSsession::loadLSclass($className)) { - $r = new $className(); - if (!$r -> validate($value, $rconf, $this)) { - if (isset($rconf['msg'])) { - $this -> form -> setElementError( - $this -> attr_html, - getFData(__($rconf['msg']), $label) - ); - } - else { - $this -> form -> setElementError( - $this -> attr_html, - getFData( - _('Invalid value "%{value}" for component %{component}.'), - array('value' => $value, 'component' => $label) - ) - ); - } - } - } - else { - $this -> form -> setElementError( - $this -> attr_html, - getFData(_("Can't validate value of component %{c}."), $label) - ); - } + ) + ); } } diff --git a/src/includes/class/class.LSformElement_supannCompositeAttribute.php b/src/includes/class/class.LSformElement_supannCompositeAttribute.php index 9e2e5392..61d363f8 100644 --- a/src/includes/class/class.LSformElement_supannCompositeAttribute.php +++ b/src/includes/class/class.LSformElement_supannCompositeAttribute.php @@ -470,19 +470,18 @@ class LSformElement_supannCompositeAttribute extends LSformElement { // Check component value (if configured) if (isset($cconf['check_data']) && is_array($cconf['check_data'])) { + LSsession :: loadLSclass('LSformRule', null, true); foreach($cconf['check_data'] as $ruleType => $rconf) { - $className = 'LSformRule_'.$ruleType; - if (!LSsession::loadLSclass($className)) { - $errors[] = getFData(__("Can't validate value of component %{c}."),__($cconf['label'])); - continue; - } - $r = new $className(); - if (!$r -> validate($value, $rconf, $this)) { - $errors[] = getFData( - __(LSconfig :: get('msg', 'Invalid value for component %{c}.', 'string', $rconf)), - __($cconf['label']) - ); - } + $cerrors = LSformRule :: validate_values($ruleType, $value, $rconf, $this); + if (is_array($cerrors)) + foreach ($cerrors as $cerror) + $errors[] = getFData( + __('%{label}: %{error}'), + array( + 'label' => __($cconf['label']), + 'error' => $cerror, + ) + ); } } }