Files
ntspi-app/app/Containers/Widget/Data/Rules/UniqueJsonField.php
F4ilji c01016a154 Migrate backend to Porto architecture and fix minor bugs
- Fully transitioned the backend to the Porto architectural pattern
- Improved code organization and maintainability
- Fixed minor bugs and inconsistencies
2025-05-12 08:47:43 +05:00

42 lines
1.2 KiB
PHP

<?php
namespace App\Containers\Widget\Data\Rules;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Support\Facades\DB;
class UniqueJsonField implements Rule
{
protected int $formId;
protected string $fieldName;
public function __construct($formId, $fieldName)
{
$this->formId = $formId;
$this->fieldName = $fieldName;
}
public function passes($attribute, $value)
{
// Получаем все записи для данного custom_form_id
$responses = DB::table('custom_form_responses')
->where('custom_form_id', $this->formId)
->pluck('answers');
// Проверяем, есть ли значение в JSON
foreach ($responses as $response) {
$answers = json_decode($response, true);
if (isset($answers[$this->fieldName]) && $answers[$this->fieldName] === $value) {
return false; // Значение не уникально
}
}
return true; // Значение уникально
}
public function message()
{
return 'Поле должно быть уникальным';
}
}