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>
105 lines
2.7 KiB
PHP
105 lines
2.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Models\FormBuilder;
|
|
|
|
use App\Models\Organisation;
|
|
use App\Models\Scopes\OrganisationScope;
|
|
use Illuminate\Database\Eloquent\Concerns\HasUlids;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
|
|
|
final class FormFieldLibrary extends Model
|
|
{
|
|
use HasFactory;
|
|
use HasUlids;
|
|
|
|
protected $table = 'form_field_library';
|
|
|
|
public string $organisationScopeColumn = 'organisation_id';
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::addGlobalScope(new OrganisationScope());
|
|
}
|
|
|
|
protected $fillable = [
|
|
'organisation_id',
|
|
'name',
|
|
'slug',
|
|
'field_type',
|
|
'label',
|
|
'help_text',
|
|
'default_is_required',
|
|
'default_is_filterable',
|
|
'translations',
|
|
'description',
|
|
'is_active',
|
|
];
|
|
|
|
/** @var array<string, string> */
|
|
protected $casts = [
|
|
'translations' => 'array',
|
|
'default_is_required' => 'bool',
|
|
'default_is_filterable' => 'bool',
|
|
'is_system' => 'bool',
|
|
'is_active' => 'bool',
|
|
'usage_count' => 'int',
|
|
];
|
|
|
|
public function organisation(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Organisation::class);
|
|
}
|
|
|
|
public function fields(): HasMany
|
|
{
|
|
return $this->hasMany(FormField::class, 'library_field_id');
|
|
}
|
|
|
|
public function bindings(): MorphMany
|
|
{
|
|
return $this->morphMany(FormFieldBinding::class, 'owner');
|
|
}
|
|
|
|
public function validationRules(): MorphMany
|
|
{
|
|
return $this->morphMany(FormFieldValidationRule::class, 'owner');
|
|
}
|
|
|
|
public function configs(): MorphMany
|
|
{
|
|
return $this->morphMany(FormFieldConfig::class, 'owner');
|
|
}
|
|
|
|
public function options(): MorphMany
|
|
{
|
|
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;
|
|
}
|
|
}
|