feat(form-field): backfill form_fields.options to form_field_options

Atomic data migration. Every options datum in the database — in
form_fields and form_field_library, their translations bags, and the
form_submissions.schema_snapshot + form_templates.schema_snapshot JSON
blobs — is converted to the new relational rich-shape representation.

Strict dispatch per §17.4.4 / §8.7 convention:
  - Fail on field_type ∉ {RADIO, SELECT, MULTISELECT, CHECKBOX_LIST}
    carrying non-null options (post-WS-5b TAG_PICKER seed-bug indicator)
  - Fail on non-flat-string-array options shape
  - Fail on translations.{locale}.options[] length mismatch
  - Fail on non-string / >255-char translated labels
  - Fail on any residual translations.{locale}.options key after
    step C migration

Snapshot rewrite in-place: both form_submissions.schema_snapshot and
form_templates.schema_snapshot walk fields[*] and rewrite options to
the new rich-shape, strip per-locale options[] from the parallel
translations bag. Zero-compromise directive — no reader tolerance for
pre-WS-5d shape in commit 3 onwards.

Rollback reconstructs JSON column shapes plus translations bags.
Forward+back pair safe as a unit; partial rollback unsupported.

FormFieldService::insertFromLibrary switches from JSON-copy to
FormFieldOptionService::copyOptions row-clone per addendum Q3 row-copy
mandate. The field's own translations bag no longer carries
{locale}.options keys — those live on option rows now.

Seeders and factories switch to service-level option creation:
  - FormBuilderDevSeeder.canonicalFields keeps flat-string options as
    its data shape; FormField::create no longer receives an options
    key, the post-create FormFieldOptionService::replaceOptions call
    inserts the rich rows. The same applies to
    seedEventRegistrationShowcaseSchema. The vergoedingstype field's
    legacy {label, description} object shape (a pre-WS-5d seed-bug
    that the strict backfill would reject) is normalised to flat
    strings; the descriptions are dropped.
  - seedSystemTemplates embeds rich-shape options in the template
    snapshot — no flat-array snapshot data remains in newly-seeded
    rows.
  - FormFieldFactory + FormFieldLibraryFactory drop the options
    default; new ::withOptions() helper accepts either flat strings
    (each becomes value+label) or full spec arrays and routes through
    the service.

JSON columns (form_fields.options, form_field_library.options) remain
present and writable via fillable; column-drop lands in commit 5.
Reads from the JSON column still exist in resources, snapshot writer,
FormRequests, FormValueService, and FilterRegistryController — commit
3 switches those all atomically.

Migration step-count tests in WS-5a/b/c bumped by 1 to account for
the new backfill_form_field_options migration on the migration stack.

Tests: 1182 → 1193 green (+11 tests / +56 assertions).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-25 02:21:26 +02:00
parent 11588623c5
commit 15e4e49d8c
10 changed files with 1216 additions and 38 deletions

View File

@@ -20,6 +20,7 @@ use App\Models\Organisation;
use App\Models\Person;
use App\Models\PersonTag;
use App\Models\UserOrganisationTag;
use App\Services\FormBuilder\FormFieldOptionService;
use App\Services\FormBuilder\FormSubmissionService;
use App\Services\FormBuilder\FormValueService;
use Illuminate\Console\Command;
@@ -50,11 +51,7 @@ final class FormBuilderDevSeeder
['type' => FormFieldType::SELECT, 'slug' => 'shirtmaat', 'label' => 'Shirtmaat', 'options' => ['XS', 'S', 'M', 'L', 'XL', 'XXL', 'XXXL'], 'is_filterable' => true, 'display_width' => 'half'],
['type' => FormFieldType::MULTISELECT, 'slug' => 'dieetwensen', 'label' => 'Dieetwensen', 'options' => ['Vegetarisch', 'Veganistisch', 'Halal', 'Glutenvrij', 'Lactosevrij', "Geen pinda's", 'Geen noten'], 'is_filterable' => true, 'display_width' => 'half'],
['type' => FormFieldType::HEADING, 'slug' => 'vergoeding', 'label' => 'Vergoeding', 'help_text' => 'Kies hoe je wilt worden bedankt voor je inzet', 'display_width' => 'full'],
['type' => FormFieldType::RADIO, 'slug' => 'vergoedingstype', 'label' => 'Vergoedingstype', 'options' => [
['label' => 'Pro Deo', 'description' => 'Je werkt als vrijwilliger zonder financiële vergoeding'],
['label' => 'Entreeticket', 'description' => 'Je ontvangt een gratis festivalticket als dank voor je inzet'],
['label' => 'Vrijwilligersvergoeding', 'description' => 'Je ontvangt een vergoeding conform de vrijwilligersregeling'],
], 'is_required' => true, 'display_width' => 'full'],
['type' => FormFieldType::RADIO, 'slug' => 'vergoedingstype', 'label' => 'Vergoedingstype', 'options' => ['Pro Deo', 'Entreeticket', 'Vrijwilligersvergoeding'], 'is_required' => true, 'display_width' => 'full'],
['type' => FormFieldType::HEADING, 'slug' => 'noodcontact', 'label' => 'Noodcontact', 'help_text' => 'Wie kunnen we bereiken bij calamiteiten?', 'display_width' => 'full'],
['type' => FormFieldType::TEXT, 'slug' => 'noodcontact-naam', 'label' => 'Noodcontact naam', 'is_pii' => true, 'display_width' => 'half'],
['type' => FormFieldType::TEXT, 'slug' => 'noodcontact-telefoon', 'label' => 'Noodcontact telefoon', 'is_pii' => true, 'display_width' => 'half'],
@@ -94,7 +91,7 @@ final class FormBuilderDevSeeder
'label' => $field['label'],
'help_text' => $field['help_text'] ?? null,
'section_slug' => null,
'options' => $field['options'] ?? null,
'options' => self::richOptionsForSnapshot($field['options'] ?? null),
'validation_rules' => null,
'is_required' => $field['is_required'] ?? false,
'is_filterable' => $field['is_filterable'] ?? false,
@@ -149,14 +146,15 @@ final class FormBuilderDevSeeder
'auto_save_enabled' => false,
]);
$optionService = app(FormFieldOptionService::class);
foreach (self::canonicalFields() as $sortOrder => $field) {
FormField::create([
$created = FormField::create([
'form_schema_id' => $schema->id,
'field_type' => $field['type']->value,
'slug' => $field['slug'],
'label' => $field['label'],
'help_text' => $field['help_text'] ?? null,
'options' => $field['options'] ?? null,
'is_required' => $field['is_required'] ?? false,
'is_filterable' => $field['is_filterable'] ?? false,
'is_portal_visible' => true,
@@ -166,11 +164,58 @@ final class FormBuilderDevSeeder
'value_storage_hint' => $field['type']->recommendedValueStorageHint(),
'sort_order' => $sortOrder + 1,
]);
$specs = self::optionSpecsFor($field['options'] ?? null);
if ($specs !== []) {
$optionService->replaceOptions($created, $specs);
}
}
return $schema;
}
/**
* Convert a flat string options array (canonical seeder shape) into
* the rich-shape spec list consumed by FormFieldOptionService.
*
* @param list<string>|null $options
* @return list<array{value:string,label:string,sort_order:int}>
*/
private static function optionSpecsFor(?array $options): array
{
if ($options === null || $options === []) {
return [];
}
$specs = [];
foreach ($options as $i => $entry) {
$entry = (string) $entry;
$specs[] = [
'value' => $entry,
'label' => $entry,
'sort_order' => $i,
];
}
return $specs;
}
/**
* Convert a flat string options array into the rich-shape JSON used
* inside form_templates / form_submissions snapshots. Returns null
* for option-less field types (preserves snapshot null contract).
*
* @param list<string>|null $options
* @return list<array{value:string,label:string,sort_order:int}>|null
*/
private static function richOptionsForSnapshot(?array $options): ?array
{
if ($options === null || $options === []) {
return null;
}
return self::optionSpecsFor($options);
}
/**
* For each person with status applied/approved/no_show on this event,
* create one FormSubmission with a realistic handful of FormValues.
@@ -359,6 +404,7 @@ final class FormBuilderDevSeeder
]);
$ruleService = app(\App\Services\FormBuilder\FormFieldValidationRuleService::class);
$optionService = app(FormFieldOptionService::class);
foreach (self::showcaseFieldDefinitions() as $sortOrder => $def) {
$field = FormField::create([
@@ -367,7 +413,6 @@ final class FormBuilderDevSeeder
'slug' => $def['slug'],
'label' => $def['label'],
'help_text' => $def['help_text'] ?? null,
'options' => $def['options'] ?? null,
'is_required' => $def['is_required'] ?? false,
'is_filterable' => $def['is_filterable'] ?? false,
'is_portal_visible' => true,
@@ -379,6 +424,11 @@ final class FormBuilderDevSeeder
'sort_order' => $sortOrder + 1,
]);
$specs = self::optionSpecsFor($def['options'] ?? null);
if ($specs !== []) {
$optionService->replaceOptions($field, $specs);
}
// Relational validation rules (WS-5b). The SECTION_PRIORITY
// field carries the UI soft cap as a `max_selected` row; other
// fields in the showcase have no rules yet.