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:
2026-04-25 02:33:21 +02:00
parent 15e4e49d8c
commit bb9242fd6e
19 changed files with 728 additions and 51 deletions

View File

@@ -42,6 +42,7 @@ final class FormFieldService
$bindingSpec = $this->extractBindingSpec($data);
$validationRuleSpecs = $this->extractValidationRuleSpecs($data);
$optionSpecs = $this->extractOptionSpecs($data);
[$conditionalTree, $conditionalProvided] = $this->extractConditionalLogicTree($data);
/** @var FormField $field */
@@ -55,6 +56,10 @@ final class FormFieldService
$this->validationRuleService->replaceRules($field, $validationRuleSpecs);
}
if ($optionSpecs !== null) {
$this->optionService->replaceOptions($field, $optionSpecs);
}
if ($conditionalProvided) {
// Cycle check runs inside the service — reads the schema's
// relational adjacency and throws on a back-edge.
@@ -86,10 +91,14 @@ final class FormFieldService
$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;
[$conditionalTree, $conditionalProvided] = $this->extractConditionalLogicTree($data);
$currentBindingShape = $this->bindingService->toJsonShape($field->bindings()->first());
$currentConditionalShape = $this->conditionalLogicService->toJsonShape($field->rootConditionalLogicGroup());
$currentOptionsShape = $this->optionService->toJsonShape($field->options()->get());
if ($bindingProvided && $this->bindingChanged($currentBindingShape, $rawBinding)) {
$this->assertBindingChangeAllowed($field, $forceBindingChange);
@@ -117,6 +126,10 @@ final class FormFieldService
$this->validationRuleService->replaceRules($field, $validationRuleSpecs ?? []);
}
if ($optionsProvided) {
$this->optionService->replaceOptions($field, $optionSpecs ?? []);
}
if ($conditionalProvided) {
$this->conditionalLogicService->replaceLogic($field, $conditionalTree);
}
@@ -140,6 +153,16 @@ final class FormFieldService
$new['conditional_logic'] = $newConditionalShape;
}
// §17.6.3 dual emit pattern: include options in the field.updated diff
// only when the option set actually changed (byte-equal JSON compare).
// The semantic field.options_replaced event from
// FormFieldOptionService::replaceOptions stays in addition to this.
$newOptionsShape = $this->optionService->toJsonShape($field->fresh()?->options()->get() ?? collect());
if (json_encode($currentOptionsShape) !== json_encode($newOptionsShape)) {
$before['options'] = $currentOptionsShape;
$new['options'] = $newOptionsShape;
}
$field->logFieldChange('field.updated', [
'old' => $before,
'new' => $new,
@@ -182,6 +205,31 @@ final class FormFieldService
return array_values($raw);
}
/**
* Extract the `options` key from the request data array and return
* it as the service-layer spec list. The JSON column is gone post
* WS-5d commit 3 writes go through
* `FormFieldOptionService::replaceOptions` after the FormField row
* is created/updated.
*
* @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