jsonCompositeValue & supannCompositeValue: fix checking value method

Use LSformRule::validate_values() to check value instead of 
reimplementing incomplete checking method.
This commit is contained in:
Benjamin Renard 2021-07-12 17:57:07 +02:00
parent 988d744836
commit ba8b78706a
2 changed files with 52 additions and 62 deletions

View file

@ -363,68 +363,59 @@ class LSformElement_jsonCompositeAttribute extends LSformElement {
* @retval void * @retval void
**/ **/
private function checkComponentValues($c, $value) { private function checkComponentValues($c, $value) {
if ($this -> getComponentConfig($c, 'multiple', false, 'bool')) { // Check select_list component's values
foreach ($value as $val) { if ($this -> getComponentConfig($c, 'type') == 'select_list') {
$this -> checkComponentValue($c, $val); 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[] $c The component name
* @param[] $value The value to check * @param[] $value The value to check
* *
* @retval void * @retval void
**/ **/
private function checkComponentValue($c, $value) { private function checkSelectListComponentValue($c, $value) {
$label = __($this -> getComponentConfig($c, 'label')); if (!$this -> getSelectListComponentValueLabel($c, $value)) {
$this -> form -> setElementError(
// select_list components : check values $this -> attr_html,
if ($this -> getComponentConfig($c, 'type') == 'select_list') { getFData(
if (!$this -> getSelectListComponentValueLabel($c, $value)) { _('Invalid value "%{value}" for component %{component}.'),
$this -> form -> setElementError( array(
$this -> attr_html, 'value' => $value,
getFData( 'component' => __($this -> getComponentConfig($c, 'label'))
_('Invalid value "%{value}" for component %{component}.'),
array('value' => $value, 'component' => $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)
);
}
} }
} }

View file

@ -470,19 +470,18 @@ class LSformElement_supannCompositeAttribute extends LSformElement {
// Check component value (if configured) // Check component value (if configured)
if (isset($cconf['check_data']) && is_array($cconf['check_data'])) { if (isset($cconf['check_data']) && is_array($cconf['check_data'])) {
LSsession :: loadLSclass('LSformRule', null, true);
foreach($cconf['check_data'] as $ruleType => $rconf) { foreach($cconf['check_data'] as $ruleType => $rconf) {
$className = 'LSformRule_'.$ruleType; $cerrors = LSformRule :: validate_values($ruleType, $value, $rconf, $this);
if (!LSsession::loadLSclass($className)) { if (is_array($cerrors))
$errors[] = getFData(__("Can't validate value of component %{c}."),__($cconf['label'])); foreach ($cerrors as $cerror)
continue; $errors[] = getFData(
} __('%{label}: %{error}'),
$r = new $className(); array(
if (!$r -> validate($value, $rconf, $this)) { 'label' => __($cconf['label']),
$errors[] = getFData( 'error' => $cerror,
__(LSconfig :: get('msg', 'Invalid value for component %{c}.', 'string', $rconf)), )
__($cconf['label']) );
);
}
} }
} }
} }