Add 'exists_in_submissions' and 'does_not_exist_in_submissions' valid… (#725)
* Add 'exists_in_submissions' and 'does_not_exist_in_submissions' validation conditions - Implement new validation conditions in FormLogicConditionChecker to check for existing submissions. - Update open_filters.json and client-side filters to include the new conditions. - Enhance FormLogicTest with test cases for the new validation conditions. - Modify UI components to support the new logic conditions in form validation. * Fix FormLogicConditionChecker * Improve custom_validation_only * fix test --------- Co-authored-by: Julien Nahum <julien@nahum.net>
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace App\Service\Forms;
|
||||
|
||||
use App\Models\Forms\FormSubmission;
|
||||
|
||||
class FormLogicConditionChecker
|
||||
{
|
||||
public function __construct(private ?array $conditions, private ?array $formData)
|
||||
@@ -304,6 +306,48 @@ class FormLogicConditionChecker
|
||||
return false;
|
||||
}
|
||||
|
||||
private function checkExistsInSubmissions($condition, $fieldValue): bool
|
||||
{
|
||||
if (!$fieldValue || !isset($condition['property_meta']['id'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$formId = $this->formData['form']['id'] ?? null;
|
||||
if (!$formId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return FormSubmission::where('form_id', $formId)
|
||||
->where(function ($query) use ($condition, $fieldValue) {
|
||||
$fieldId = $condition['property_meta']['id'];
|
||||
|
||||
if (env('DB_CONNECTION') == 'mysql') {
|
||||
// For scalar values
|
||||
$query->where(function ($q) use ($fieldId, $fieldValue) {
|
||||
$q->whereRaw("JSON_UNQUOTE(JSON_EXTRACT(data, '$.\"$fieldId\"')) = ?", [$fieldValue]);
|
||||
|
||||
// For array values
|
||||
if (is_array($fieldValue)) {
|
||||
$q->orWhereRaw("JSON_CONTAINS(JSON_EXTRACT(data, '$.\"$fieldId\"'), ?)", [json_encode($fieldValue)]);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
$query->where(function ($q) use ($fieldId, $fieldValue) {
|
||||
// For scalar values
|
||||
$q->whereRaw("data->? = ?::jsonb", [$fieldId, json_encode($fieldValue)]);
|
||||
|
||||
// For array values
|
||||
if (is_array($fieldValue)) {
|
||||
$q->orWhereRaw("data->? @> ?::jsonb", [
|
||||
$fieldId,
|
||||
json_encode($fieldValue)
|
||||
]);
|
||||
}
|
||||
});
|
||||
}
|
||||
})->exists();
|
||||
}
|
||||
|
||||
private function textConditionMet(array $propertyCondition, $value): bool
|
||||
{
|
||||
switch ($propertyCondition['operator']) {
|
||||
@@ -348,6 +392,10 @@ class FormLogicConditionChecker
|
||||
} catch (\Exception $e) {
|
||||
return true;
|
||||
}
|
||||
case 'exists_in_submissions':
|
||||
return $this->checkExistsInSubmissions($propertyCondition, $value);
|
||||
case 'does_not_exist_in_submissions':
|
||||
return !$this->checkExistsInSubmissions($propertyCondition, $value);
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -384,6 +432,10 @@ class FormLogicConditionChecker
|
||||
return $this->checkLength($propertyCondition, $value, '<');
|
||||
case 'content_length_less_than_or_equal_to':
|
||||
return $this->checkLength($propertyCondition, $value, '<=');
|
||||
case 'exists_in_submissions':
|
||||
return $this->checkExistsInSubmissions($propertyCondition, $value);
|
||||
case 'does_not_exist_in_submissions':
|
||||
return !$this->checkExistsInSubmissions($propertyCondition, $value);
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user