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:
@@ -37,6 +37,7 @@ final class FormSubmissionService
|
||||
private readonly FormFieldValidationRuleService $validationRuleService,
|
||||
private readonly FormFieldConfigService $configService,
|
||||
private readonly FormFieldConditionalLogicService $conditionalLogicService,
|
||||
private readonly FormFieldOptionService $optionService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -203,7 +204,7 @@ final class FormSubmissionService
|
||||
*/
|
||||
private function buildSnapshot(FormSchema $schema): array
|
||||
{
|
||||
$schema->loadMissing(['fields.bindings', 'fields.validationRules', 'fields.configs', 'sections']);
|
||||
$schema->loadMissing(['fields.bindings', 'fields.validationRules', 'fields.configs', 'fields.options', 'sections']);
|
||||
|
||||
return [
|
||||
'schema_version' => $schema->version,
|
||||
@@ -234,7 +235,9 @@ final class FormSubmissionService
|
||||
'label' => $f->label,
|
||||
'help_text' => $f->help_text,
|
||||
'section_slug' => $this->sectionSlug($schema, $f->form_schema_section_id),
|
||||
'options' => $f->options,
|
||||
'options' => $f->options->isNotEmpty()
|
||||
? $this->optionService->toJsonShape($f->options)
|
||||
: null,
|
||||
'validation_rules' => $this->validationRuleService->toJsonShape($f->validationRules),
|
||||
'configs' => $this->configService->toJsonShape($f->configs),
|
||||
'is_required' => (bool) $f->is_required,
|
||||
@@ -242,13 +245,43 @@ final class FormSubmissionService
|
||||
'is_pii' => (bool) $f->is_pii,
|
||||
'binding' => $this->bindingService->toJsonShape($f->bindings->first()),
|
||||
'conditional_logic' => $this->conditionalLogicService->toJsonShape($f->rootConditionalLogicGroup()),
|
||||
'translations' => $f->translations,
|
||||
'translations' => $this->stripOptionsFromTranslations($f->translations),
|
||||
'value_storage_hint' => $f->value_storage_hint instanceof \BackedEnum ? $f->value_storage_hint->value : $f->value_storage_hint,
|
||||
'sort_order' => $f->sort_order,
|
||||
])->toArray(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Per-locale `options[]` parallel arrays moved onto each
|
||||
* form_field_options.translations row in WS-5d. The field's own
|
||||
* translations bag retains only label/help_text per locale; strip
|
||||
* any residual options key defensively (commit 2 backfill should
|
||||
* already have done so on existing rows).
|
||||
*
|
||||
* @param mixed $translations
|
||||
* @return array<string, mixed>|null
|
||||
*/
|
||||
private function stripOptionsFromTranslations(mixed $translations): ?array
|
||||
{
|
||||
if (! is_array($translations) || $translations === []) {
|
||||
return null;
|
||||
}
|
||||
$clean = [];
|
||||
foreach ($translations as $locale => $bag) {
|
||||
if (is_array($bag)) {
|
||||
unset($bag['options']);
|
||||
if ($bag !== []) {
|
||||
$clean[$locale] = $bag;
|
||||
}
|
||||
} else {
|
||||
$clean[$locale] = $bag;
|
||||
}
|
||||
}
|
||||
|
||||
return $clean === [] ? null : $clean;
|
||||
}
|
||||
|
||||
private function sectionSlug(FormSchema $schema, ?string $sectionId): ?string
|
||||
{
|
||||
if ($sectionId === null) {
|
||||
|
||||
Reference in New Issue
Block a user