opnform-host-nginx/api/app/Rules/CustomFieldValidationRule.php

64 lines
1.4 KiB
PHP
Raw Normal View History

<?php
namespace App\Rules;
use App\Service\Forms\FormLogicConditionChecker;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
class CustomFieldValidationRule implements ValidationRule
{
/**
* Indicates whether the rule should be implicit.
*
* @var bool
*/
public $implicit = true;
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct(public array $validation, public array $formData)
{
}
/**
* Determine if the validation rule passes.
*
2024-05-29 16:54:26 +02:00
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
2024-06-17 14:56:36 +02:00
$logicConditions = $this->validation['error_conditions']['conditions'] ?? null;
if (empty($logicConditions) || empty($logicConditions['children'] ?? [])) {
2024-05-30 06:04:55 +02:00
return true;
}
2024-06-17 14:56:36 +02:00
2024-05-29 16:54:26 +02:00
return FormLogicConditionChecker::conditionsMet(
2024-06-17 15:07:33 +02:00
$logicConditions,
2024-05-29 16:54:26 +02:00
$this->formData
);
}
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (!$this->passes($attribute, $value)) {
$fail($this->message());
}
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return isset($this->validation['error_message']) ? $this->validation['error_message'] : 'Invalid input';
}
}