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
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
<?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 'Поле должно быть уникальным';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user