refactor(form-field): resources + snapshot + validator read form_field_options
Atomic reader switch. All call paths that previously read
form_fields.options / form_field_library.options from the JSON column
now read through FormFieldOptionService::toJsonShape() via the
morphMany relation:
- FormFieldResource + FormFieldLibraryResource +
PublicFormSchemaResource emit the rich-shape array
- FilterRegistryController emits rich shape uniformly (no flat-array
carve-out for filter-UI compatibility — preflight scan confirmed
zero portal/app consumers, S5 territory)
- FormFieldRuleBuilder plucks values from the relation for in:options
rule construction
- FormSubmissionService::buildSnapshot writes rich-shape options into
snapshots and strips translations.{locale}.options from each field's
translations bag (defensive — commit 2 backfill already did the
bulk strip)
- Four FormFieldRequest variants accept array-of-spec-objects,
validate shape in after() via FormFieldOptionService::assertSpecsValid,
and hand off to FormFieldOptionService::replaceOptions for writes
- FormFieldService::create + update extract option specs from the
request data and route through the service after the FormField row
is persisted
FormField and FormFieldLibrary $casts no longer include 'options'; the
JSON column is no longer cast. Options removed from $fillable on both
models so ::create() / ::fill() / mass assignment can no longer touch
the legacy column. Both models gain a getOptionsAttribute() accessor
that resolves $model->options to the eager-loaded morphMany collection
— required because Eloquent's getAttribute() prefers a real DB column
over a relation method, and the JSON column lives on the table until
WS-5d commit 5 drops it.
Activity log — dual emit per §6.7 / §17.4.2 / §17.6.3:
- field.updated carries old.options / new.options diff via
toJsonShape() reconstruction, byte-equal JSON compare to avoid
cosmetic false positives. Field updates that don't touch options
omit the key entirely
- field.options_replaced emits inside replaceOptions() on FormField
subject only; library subject writes silent (mirrors the WS-5b /
WS-5c convention)
JSON columns (form_fields.options, form_field_library.options) remain
present but unread — column drops land atomically in commit 5.
Two pre-existing test fixtures that seeded options via the JSON column
(FormFieldApiTest + PublicFormValidationTest) migrated to the
spec-array path: FormField::factory()->withOptions([...]) where the
options live on the field, or explicit spec-array request bodies for
HTTP tests.
Tests: 1193 → 1206 green (+13 tests / +28 assertions).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -9,6 +9,7 @@ use App\Models\Event;
|
||||
use App\Models\FormBuilder\FormField;
|
||||
use App\Models\FormBuilder\FormSchema;
|
||||
use App\Models\Organisation;
|
||||
use App\Services\FormBuilder\FormFieldOptionService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
@@ -31,7 +32,10 @@ final class FilterRegistryController extends Controller
|
||||
'field_type' => 'TAG_PICKER',
|
||||
]];
|
||||
|
||||
$optionService = app(FormFieldOptionService::class);
|
||||
|
||||
$formFieldSources = FormField::query()
|
||||
->with('options')
|
||||
->whereHas('schema', function ($q) use ($organisation, $eventId): void {
|
||||
$q->where('organisation_id', $organisation->id);
|
||||
if ($eventId !== null && $eventId !== '') {
|
||||
@@ -50,7 +54,9 @@ final class FilterRegistryController extends Controller
|
||||
'schema_slug' => $f->schema?->slug,
|
||||
'label' => $f->label,
|
||||
'field_type' => $f->field_type,
|
||||
'options' => is_array($f->options) ? array_values($f->options) : null,
|
||||
'options' => $f->options->isNotEmpty()
|
||||
? $optionService->toJsonShape($f->options)
|
||||
: null,
|
||||
])->values()->all();
|
||||
|
||||
return [
|
||||
|
||||
@@ -11,6 +11,7 @@ use App\Http\Resources\FormBuilder\FormFieldLibraryResource;
|
||||
use App\Models\FormBuilder\FormFieldLibrary;
|
||||
use App\Models\Organisation;
|
||||
use App\Services\FormBuilder\FormFieldBindingService;
|
||||
use App\Services\FormBuilder\FormFieldOptionService;
|
||||
use App\Services\FormBuilder\FormFieldValidationRuleService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||
@@ -22,6 +23,7 @@ final class FormFieldLibraryController extends Controller
|
||||
public function __construct(
|
||||
private readonly FormFieldBindingService $bindingService,
|
||||
private readonly FormFieldValidationRuleService $validationRuleService,
|
||||
private readonly FormFieldOptionService $optionService,
|
||||
) {}
|
||||
|
||||
public function index(Organisation $organisation): AnonymousResourceCollection
|
||||
@@ -51,6 +53,7 @@ final class FormFieldLibraryController extends Controller
|
||||
$data = $request->validated();
|
||||
$bindingSpec = $this->extractBindingSpec($data);
|
||||
$validationRuleSpecs = $this->extractValidationRuleSpecs($data);
|
||||
$optionSpecs = $this->extractOptionSpecs($data);
|
||||
$data['organisation_id'] = $organisation->id;
|
||||
$data['is_system'] = false;
|
||||
$data['is_active'] ??= true;
|
||||
@@ -67,6 +70,10 @@ final class FormFieldLibraryController extends Controller
|
||||
$this->validationRuleService->replaceRules($library, $validationRuleSpecs);
|
||||
}
|
||||
|
||||
if ($optionSpecs !== null) {
|
||||
$this->optionService->replaceOptions($library, $optionSpecs);
|
||||
}
|
||||
|
||||
return $this->created(new FormFieldLibraryResource($library));
|
||||
}
|
||||
|
||||
@@ -82,6 +89,9 @@ final class FormFieldLibraryController extends Controller
|
||||
$validationRulesProvided = array_key_exists('validation_rules', $data);
|
||||
$validationRuleSpecs = $validationRulesProvided ? $this->extractValidationRuleSpecs($data) : null;
|
||||
|
||||
$optionsProvided = array_key_exists('options', $data);
|
||||
$optionSpecs = $optionsProvided ? $this->extractOptionSpecs($data) : null;
|
||||
|
||||
$fieldLibrary->fill($data);
|
||||
$fieldLibrary->save();
|
||||
|
||||
@@ -99,6 +109,13 @@ final class FormFieldLibraryController extends Controller
|
||||
);
|
||||
}
|
||||
|
||||
if ($optionsProvided) {
|
||||
$this->optionService->replaceOptions(
|
||||
$fieldLibrary,
|
||||
$optionSpecs ?? [],
|
||||
);
|
||||
}
|
||||
|
||||
return $this->success(new FormFieldLibraryResource($fieldLibrary));
|
||||
}
|
||||
|
||||
@@ -126,6 +143,28 @@ final class FormFieldLibraryController extends Controller
|
||||
return array_values($raw);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract option specs from the request data array. WS-5d commit 3 —
|
||||
* writes go through `FormFieldOptionService::replaceOptions`.
|
||||
*
|
||||
* @param array<string, mixed> $data
|
||||
* @return list<array<string, mixed>>|null
|
||||
*/
|
||||
private function extractOptionSpecs(array &$data): ?array
|
||||
{
|
||||
if (! array_key_exists('options', $data)) {
|
||||
return null;
|
||||
}
|
||||
$raw = $data['options'];
|
||||
unset($data['options']);
|
||||
if (! is_array($raw)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
/** @var list<array<string, mixed>> $raw */
|
||||
return array_values($raw);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
* @return array{target_entity:string,target_attribute:string,mode:string,sync_direction?:?string}|null
|
||||
|
||||
@@ -5,7 +5,9 @@ declare(strict_types=1);
|
||||
namespace App\Http\Requests\Api\V1\FormBuilder;
|
||||
|
||||
use App\Enums\FormBuilder\FormFieldType;
|
||||
use App\Exceptions\FormBuilder\InvalidOptionSpecException;
|
||||
use App\Exceptions\FormBuilder\UnknownValidationRuleTypeException;
|
||||
use App\Services\FormBuilder\FormFieldOptionService;
|
||||
use App\Services\FormBuilder\FormFieldValidationRuleService;
|
||||
use Illuminate\Contracts\Validation\Validator;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
@@ -35,6 +37,12 @@ final class StoreFormFieldLibraryRequest extends FormRequest
|
||||
'label' => ['required', 'string', 'max:255'],
|
||||
'help_text' => ['nullable', 'string'],
|
||||
'options' => ['nullable', 'array'],
|
||||
'options.*' => ['array'],
|
||||
'options.*.value' => ['required', 'string', 'max:255'],
|
||||
'options.*.label' => ['required', 'string', 'max:255'],
|
||||
'options.*.sort_order' => ['required', 'integer', 'min:0'],
|
||||
'options.*.translations' => ['nullable', 'array'],
|
||||
'options.*.translations.*' => ['string', 'max:255'],
|
||||
'validation_rules' => ['nullable', 'array'],
|
||||
'validation_rules.*' => ['array'],
|
||||
'validation_rules.*.rule_type' => ['required', 'string', 'max:40'],
|
||||
@@ -68,6 +76,17 @@ final class StoreFormFieldLibraryRequest extends FormRequest
|
||||
$validator->errors()->add('validation_rules', $e->getMessage());
|
||||
}
|
||||
},
|
||||
function (Validator $validator): void {
|
||||
$specs = $this->input('options');
|
||||
if (! is_array($specs) || $specs === []) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
app(FormFieldOptionService::class)->assertSpecsValid($specs);
|
||||
} catch (InvalidOptionSpecException $e) {
|
||||
$validator->errors()->add('options', $e->getMessage());
|
||||
}
|
||||
},
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,9 +8,11 @@ use App\Enums\FormBuilder\FormFieldDisplayWidth;
|
||||
use App\Enums\FormBuilder\FormFieldType;
|
||||
use App\Enums\FormBuilder\FormValueStorageHint;
|
||||
use App\Exceptions\FormBuilder\InvalidConditionalLogicSpecException;
|
||||
use App\Exceptions\FormBuilder\InvalidOptionSpecException;
|
||||
use App\Exceptions\FormBuilder\UnknownValidationRuleTypeException;
|
||||
use App\Models\FormBuilder\FormSchema;
|
||||
use App\Services\FormBuilder\FormFieldConditionalLogicService;
|
||||
use App\Services\FormBuilder\FormFieldOptionService;
|
||||
use App\Services\FormBuilder\FormFieldValidationRuleService;
|
||||
use Illuminate\Contracts\Validation\Validator;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
@@ -51,6 +53,12 @@ final class StoreFormFieldRequest extends FormRequest
|
||||
Rule::exists('form_field_library', 'id'),
|
||||
],
|
||||
'options' => ['nullable', 'array'],
|
||||
'options.*' => ['array'],
|
||||
'options.*.value' => ['required', 'string', 'max:255'],
|
||||
'options.*.label' => ['required', 'string', 'max:255'],
|
||||
'options.*.sort_order' => ['required', 'integer', 'min:0'],
|
||||
'options.*.translations' => ['nullable', 'array'],
|
||||
'options.*.translations.*' => ['string', 'max:255'],
|
||||
'validation_rules' => ['nullable', 'array'],
|
||||
'validation_rules.*' => ['array'],
|
||||
'validation_rules.*.rule_type' => ['required', 'string', 'max:40'],
|
||||
@@ -91,6 +99,17 @@ final class StoreFormFieldRequest extends FormRequest
|
||||
$validator->errors()->add('validation_rules', $e->getMessage());
|
||||
}
|
||||
},
|
||||
function (Validator $validator): void {
|
||||
$specs = $this->input('options');
|
||||
if (! is_array($specs) || $specs === []) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
app(FormFieldOptionService::class)->assertSpecsValid($specs);
|
||||
} catch (InvalidOptionSpecException $e) {
|
||||
$validator->errors()->add('options', $e->getMessage());
|
||||
}
|
||||
},
|
||||
function (Validator $validator): void {
|
||||
$logic = $this->input('conditional_logic');
|
||||
if ($logic === null || $logic === [] || ! is_array($logic)) {
|
||||
|
||||
@@ -5,7 +5,9 @@ declare(strict_types=1);
|
||||
namespace App\Http\Requests\Api\V1\FormBuilder;
|
||||
|
||||
use App\Enums\FormBuilder\FormFieldType;
|
||||
use App\Exceptions\FormBuilder\InvalidOptionSpecException;
|
||||
use App\Exceptions\FormBuilder\UnknownValidationRuleTypeException;
|
||||
use App\Services\FormBuilder\FormFieldOptionService;
|
||||
use App\Services\FormBuilder\FormFieldValidationRuleService;
|
||||
use Illuminate\Contracts\Validation\Validator;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
@@ -35,6 +37,12 @@ final class UpdateFormFieldLibraryRequest extends FormRequest
|
||||
'label' => ['sometimes', 'string', 'max:255'],
|
||||
'help_text' => ['sometimes', 'nullable', 'string'],
|
||||
'options' => ['sometimes', 'nullable', 'array'],
|
||||
'options.*' => ['array'],
|
||||
'options.*.value' => ['required', 'string', 'max:255'],
|
||||
'options.*.label' => ['required', 'string', 'max:255'],
|
||||
'options.*.sort_order' => ['required', 'integer', 'min:0'],
|
||||
'options.*.translations' => ['nullable', 'array'],
|
||||
'options.*.translations.*' => ['string', 'max:255'],
|
||||
'validation_rules' => ['sometimes', 'nullable', 'array'],
|
||||
'validation_rules.*' => ['array'],
|
||||
'validation_rules.*.rule_type' => ['required', 'string', 'max:40'],
|
||||
@@ -71,6 +79,20 @@ final class UpdateFormFieldLibraryRequest extends FormRequest
|
||||
$validator->errors()->add('validation_rules', $e->getMessage());
|
||||
}
|
||||
},
|
||||
function (Validator $validator): void {
|
||||
if (! $this->has('options')) {
|
||||
return;
|
||||
}
|
||||
$specs = $this->input('options');
|
||||
if (! is_array($specs) || $specs === []) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
app(FormFieldOptionService::class)->assertSpecsValid($specs);
|
||||
} catch (InvalidOptionSpecException $e) {
|
||||
$validator->errors()->add('options', $e->getMessage());
|
||||
}
|
||||
},
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,9 +8,11 @@ use App\Enums\FormBuilder\FormFieldDisplayWidth;
|
||||
use App\Enums\FormBuilder\FormFieldType;
|
||||
use App\Enums\FormBuilder\FormValueStorageHint;
|
||||
use App\Exceptions\FormBuilder\InvalidConditionalLogicSpecException;
|
||||
use App\Exceptions\FormBuilder\InvalidOptionSpecException;
|
||||
use App\Exceptions\FormBuilder\UnknownValidationRuleTypeException;
|
||||
use App\Models\FormBuilder\FormSchema;
|
||||
use App\Services\FormBuilder\FormFieldConditionalLogicService;
|
||||
use App\Services\FormBuilder\FormFieldOptionService;
|
||||
use App\Services\FormBuilder\FormFieldValidationRuleService;
|
||||
use Illuminate\Contracts\Validation\Validator;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
@@ -47,6 +49,12 @@ final class UpdateFormFieldRequest extends FormRequest
|
||||
Rule::exists('form_schema_sections', 'id')->where('form_schema_id', $schemaId),
|
||||
],
|
||||
'options' => ['sometimes', 'nullable', 'array'],
|
||||
'options.*' => ['array'],
|
||||
'options.*.value' => ['required', 'string', 'max:255'],
|
||||
'options.*.label' => ['required', 'string', 'max:255'],
|
||||
'options.*.sort_order' => ['required', 'integer', 'min:0'],
|
||||
'options.*.translations' => ['nullable', 'array'],
|
||||
'options.*.translations.*' => ['string', 'max:255'],
|
||||
'validation_rules' => ['sometimes', 'nullable', 'array'],
|
||||
'validation_rules.*' => ['array'],
|
||||
'validation_rules.*.rule_type' => ['required', 'string', 'max:40'],
|
||||
@@ -91,6 +99,20 @@ final class UpdateFormFieldRequest extends FormRequest
|
||||
$validator->errors()->add('validation_rules', $e->getMessage());
|
||||
}
|
||||
},
|
||||
function (Validator $validator): void {
|
||||
if (! $this->has('options')) {
|
||||
return;
|
||||
}
|
||||
$specs = $this->input('options');
|
||||
if (! is_array($specs) || $specs === []) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
app(FormFieldOptionService::class)->assertSpecsValid($specs);
|
||||
} catch (InvalidOptionSpecException $e) {
|
||||
$validator->errors()->add('options', $e->getMessage());
|
||||
}
|
||||
},
|
||||
function (Validator $validator): void {
|
||||
if (! $this->has('conditional_logic')) {
|
||||
return;
|
||||
|
||||
@@ -7,6 +7,7 @@ namespace App\Http\Resources\FormBuilder;
|
||||
use App\Models\FormBuilder\FormFieldLibrary;
|
||||
use App\Services\FormBuilder\FormFieldBindingService;
|
||||
use App\Services\FormBuilder\FormFieldConfigService;
|
||||
use App\Services\FormBuilder\FormFieldOptionService;
|
||||
use App\Services\FormBuilder\FormFieldValidationRuleService;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
@@ -21,6 +22,8 @@ final class FormFieldLibraryResource extends JsonResource
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
$this->resource->loadMissing('options');
|
||||
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'organisation_id' => $this->organisation_id,
|
||||
@@ -29,7 +32,9 @@ final class FormFieldLibraryResource extends JsonResource
|
||||
'field_type' => $this->field_type,
|
||||
'label' => $this->label,
|
||||
'help_text' => $this->help_text,
|
||||
'options' => $this->options,
|
||||
'options' => $this->resource->options->isNotEmpty()
|
||||
? app(FormFieldOptionService::class)->toJsonShape($this->resource->options)
|
||||
: null,
|
||||
'validation_rules' => app(FormFieldValidationRuleService::class)->toJsonShape(
|
||||
$this->resource->validationRules,
|
||||
),
|
||||
|
||||
@@ -10,6 +10,7 @@ use App\Models\PersonTag;
|
||||
use App\Services\FormBuilder\FormFieldBindingService;
|
||||
use App\Services\FormBuilder\FormFieldConditionalLogicService;
|
||||
use App\Services\FormBuilder\FormFieldConfigService;
|
||||
use App\Services\FormBuilder\FormFieldOptionService;
|
||||
use App\Services\FormBuilder\FormFieldValidationRuleService;
|
||||
use App\Services\FormBuilder\FormLocaleResolver;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -29,6 +30,7 @@ final class FormFieldResource extends JsonResource
|
||||
$this->resource->schema,
|
||||
$request->user(),
|
||||
);
|
||||
$this->resource->loadMissing('options');
|
||||
|
||||
return [
|
||||
'id' => $this->id,
|
||||
@@ -40,7 +42,9 @@ final class FormFieldResource extends JsonResource
|
||||
'label' => $this->resolvedLabel($locale),
|
||||
'help_text' => $this->resolvedHelpText($locale),
|
||||
'section' => $this->section,
|
||||
'options' => $this->normalizedOptions($locale),
|
||||
'options' => $this->resource->options->isNotEmpty()
|
||||
? app(FormFieldOptionService::class)->toJsonShape($this->resource->options)
|
||||
: null,
|
||||
'available_tags' => $this->when(
|
||||
$this->field_type === FormFieldType::TAG_PICKER->value,
|
||||
fn () => $this->availableTags(),
|
||||
@@ -92,23 +96,6 @@ final class FormFieldResource extends JsonResource
|
||||
return $this->help_text;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, mixed>|null
|
||||
*/
|
||||
private function normalizedOptions(string $locale): ?array
|
||||
{
|
||||
$options = $this->options;
|
||||
if (! is_array($options)) {
|
||||
return null;
|
||||
}
|
||||
$translations = $this->translations ?? [];
|
||||
if (isset($translations[$locale]['options']) && is_array($translations[$locale]['options'])) {
|
||||
return array_values($translations[$locale]['options']);
|
||||
}
|
||||
|
||||
return array_values($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array<string, string>>
|
||||
*/
|
||||
|
||||
@@ -11,6 +11,7 @@ use App\Models\PersonTag;
|
||||
use App\Models\Scopes\OrganisationScope;
|
||||
use App\Services\FormBuilder\FormFieldConditionalLogicService;
|
||||
use App\Services\FormBuilder\FormFieldConfigService;
|
||||
use App\Services\FormBuilder\FormFieldOptionService;
|
||||
use App\Services\FormBuilder\FormFieldValidationRuleService;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
@@ -42,6 +43,7 @@ final class PublicFormSchemaResource extends JsonResource
|
||||
'fields' => fn ($q) => $q->withoutGlobalScope(OrganisationScope::class),
|
||||
'fields.validationRules',
|
||||
'fields.configs',
|
||||
'fields.options',
|
||||
'sections' => fn ($q) => $q->withoutGlobalScope(OrganisationScope::class),
|
||||
]);
|
||||
|
||||
@@ -80,7 +82,9 @@ final class PublicFormSchemaResource extends JsonResource
|
||||
'field_type' => $f->field_type,
|
||||
'label' => $f->label,
|
||||
'help_text' => $f->help_text,
|
||||
'options' => is_array($f->options) ? array_values($f->options) : null,
|
||||
'options' => $f->options->isNotEmpty()
|
||||
? app(FormFieldOptionService::class)->toJsonShape($f->options)
|
||||
: null,
|
||||
'available_tags' => $isTagPicker
|
||||
? $this->tagsForField($f, $availableTagsByCategory)
|
||||
: null,
|
||||
|
||||
Reference in New Issue
Block a user