Improve form property logic validation for checkbox conditions

- Update FormPropertyLogicRule to handle operators without values
- Add support for checkbox conditions like 'is_checked' and 'is_not_checked'
- Refactor logic validation in both API and client-side implementations
- Remove unnecessary console.log statements
- Update error modal text for better user experience
This commit is contained in:
Julien Nahum
2025-02-19 15:11:27 +01:00
parent efd31133cc
commit 28248259be
7 changed files with 121 additions and 52 deletions

View File

@@ -42,73 +42,72 @@ class FormPropertyLogicRule implements DataAwareRule, ValidationRule
if (!isset($condition['value'])) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'missing condition body';
return;
}
if (!isset($condition['value']['property_meta'])) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'missing condition property';
return;
}
if (!isset($condition['value']['property_meta']['type'])) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'missing condition property type';
return;
}
if (!isset($condition['value']['operator'])) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'missing condition operator';
return;
}
if (!isset($condition['value']['value'])) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'missing condition value';
return;
}
$typeField = $condition['value']['property_meta']['type'];
$operator = $condition['value']['operator'];
$this->operator = $operator;
$value = $condition['value']['value'];
if (!isset(self::getConditionMapping()[$typeField])) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'configuration not found for condition type';
return;
}
if (!isset(self::getConditionMapping()[$typeField]['comparators'][$operator])) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'configuration not found for condition operator';
return;
}
$type = self::getConditionMapping()[$typeField]['comparators'][$operator]['expected_type'] ?? null;
$comparatorDef = self::getConditionMapping()[$typeField]['comparators'][$operator];
$needsValue = !empty((array)$comparatorDef);
if (is_array($type)) {
$foundCorrectType = false;
foreach ($type as $subtype) {
if ($this->valueHasCorrectType($subtype, $value)) {
$foundCorrectType = true;
if ($needsValue && !isset($condition['value']['value'])) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'missing condition value';
return;
}
if ($needsValue) {
$type = $comparatorDef['expected_type'] ?? null;
$value = $condition['value']['value'];
if (is_array($type)) {
$foundCorrectType = false;
foreach ($type as $subtype) {
if ($this->valueHasCorrectType($subtype, $value)) {
$foundCorrectType = true;
}
}
if (!$foundCorrectType) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'wrong type of condition value';
}
} else {
if (!$this->valueHasCorrectType($type, $value)) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'wrong type of condition value';
}
}
if (!$foundCorrectType) {
$this->isConditionCorrect = false;
}
} else {
if (!$this->valueHasCorrectType($type, $value)) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'wrong type of condition value';
}
}
}

View File

@@ -199,3 +199,57 @@ it('can validate form logic rules for conditions', function () {
$this->assertFalse($validatorObj->passes());
expect($validatorObj->errors()->messages()['properties.0.logic'][0])->toBe('The logic conditions for Name are not complete. Error detail(s): missing operator');
});
it('can validate form logic rules for operators without values', function () {
$rules = [
'properties.*.logic' => ['array', 'nullable', new FormPropertyLogicRule()],
];
// Test checkbox is_checked without value
$data = [
'properties' => [
[
'id' => 'checkbox1',
'name' => 'Checkbox Field',
'type' => 'checkbox',
'logic' => [
'conditions' => [
'operatorIdentifier' => 'and',
'children' => [
[
'identifier' => 'test-id',
'value' => [
'operator' => 'is_checked',
'property_meta' => [
'id' => 'test-id',
'type' => 'checkbox'
]
]
]
]
],
'actions' => ['show-block']
]
]
]
];
$validatorObj = $this->app['validator']->make($data, $rules);
$this->assertTrue($validatorObj->passes());
// Test checkbox is_checked with value (should still pass for backward compatibility)
$data['properties'][0]['logic']['conditions']['children'][0]['value']['value'] = true;
$validatorObj = $this->app['validator']->make($data, $rules);
$this->assertTrue($validatorObj->passes());
// Test checkbox is_not_checked without value
$data['properties'][0]['logic']['conditions']['children'][0]['value']['operator'] = 'is_not_checked';
unset($data['properties'][0]['logic']['conditions']['children'][0]['value']['value']);
$validatorObj = $this->app['validator']->make($data, $rules);
$this->assertTrue($validatorObj->passes());
// Test checkbox with operator that doesn't exist
$data['properties'][0]['logic']['conditions']['children'][0]['value']['operator'] = 'invalid_operator';
$validatorObj = $this->app['validator']->make($data, $rules);
$this->assertFalse($validatorObj->passes());
expect($validatorObj->errors()->messages()['properties.0.logic'][0])->toBe('The logic conditions for Checkbox Field are not complete. Error detail(s): configuration not found for condition operator');
});