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

@@ -50,7 +50,6 @@ final class FormField extends Model
'label',
'help_text',
'section',
'options',
'is_required',
'is_filterable',
'is_portal_visible',
@@ -67,7 +66,6 @@ final class FormField extends Model
/** @var array<string, string> */
protected $casts = [
'options' => 'array',
'role_restrictions' => 'array',
'translations' => 'array',
'is_required' => 'bool',
@@ -123,6 +121,28 @@ final class FormField extends Model
->orderBy('sort_order');
}
/**
* Force `$field->options` (no parens) to resolve to the morphMany
* relation rather than the legacy JSON column attribute that lives
* on the table until WS-5d commit 5 drops it. Required because
* Eloquent's getAttribute() prefers the underlying column over a
* relation method when the column still exists. Removed at commit 5
* along with the column.
*
* @return \Illuminate\Database\Eloquent\Collection<int, FormFieldOption>
*/
public function getOptionsAttribute(): \Illuminate\Database\Eloquent\Collection
{
if (! $this->relationLoaded('options')) {
$this->load('options');
}
/** @var \Illuminate\Database\Eloquent\Collection<int, FormFieldOption> $relation */
$relation = $this->getRelation('options');
return $relation;
}
public function conditionalLogicGroups(): HasMany
{
return $this->hasMany(FormFieldConditionalLogicGroup::class, 'form_field_id');

View File

@@ -34,7 +34,6 @@ final class FormFieldLibrary extends Model
'field_type',
'label',
'help_text',
'options',
'default_is_required',
'default_is_filterable',
'translations',
@@ -44,7 +43,6 @@ final class FormFieldLibrary extends Model
/** @var array<string, string> */
protected $casts = [
'options' => 'array',
'translations' => 'array',
'default_is_required' => 'bool',
'default_is_filterable' => 'bool',
@@ -83,4 +81,24 @@ final class FormFieldLibrary extends Model
return $this->morphMany(FormFieldOption::class, 'owner')
->orderBy('sort_order');
}
/**
* Force `$library->options` (no parens) to resolve to the morphMany
* relation rather than the legacy JSON column attribute that lives
* on the table until WS-5d commit 5 drops it. Removed at commit 5
* along with the column.
*
* @return \Illuminate\Database\Eloquent\Collection<int, FormFieldOption>
*/
public function getOptionsAttribute(): \Illuminate\Database\Eloquent\Collection
{
if (! $this->relationLoaded('options')) {
$this->load('options');
}
/** @var \Illuminate\Database\Eloquent\Collection<int, FormFieldOption> $relation */
$relation = $this->getRelation('options');
return $relation;
}
}