feat(form-builder): form_field_configs relational table + non-validation key split + drop validation_rules JSON columns

This commit is contained in:
2026-04-24 22:42:35 +02:00
parent 9d2758a42c
commit d494478c08
31 changed files with 1233 additions and 60 deletions

View File

@@ -51,7 +51,6 @@ final class FormField extends Model
'help_text',
'section',
'options',
'validation_rules',
'is_required',
'is_filterable',
'is_portal_visible',
@@ -70,7 +69,6 @@ final class FormField extends Model
/** @var array<string, string> */
protected $casts = [
'options' => 'array',
'validation_rules' => 'array',
'conditional_logic' => 'array',
'role_restrictions' => 'array',
'translations' => 'array',
@@ -116,6 +114,11 @@ final class FormField extends Model
return $this->morphMany(FormFieldValidationRule::class, 'owner');
}
public function configs(): MorphMany
{
return $this->morphMany(FormFieldConfig::class, 'owner');
}
/**
* Nuanced activity log (ARCH §17.1; S1 Phase 4b). Callers choose which
* events are worth logging e.g. created/deleted/restored, field_type

View File

@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
namespace App\Models\FormBuilder;
use App\Enums\FormBuilder\FormFieldConfigType;
use App\Models\Scopes\FormFieldConfigScope;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
/**
* Relational home for non-validation field configuration. See
* ARCH-FORM-BUILDER §17.5 and ARCH-CONSOLIDATION-ADDENDUM-2026-04-24
* §Q3 WS-5b Uitvoering.
*
* Polymorphic owner (`form_field` / `form_field_library` aliases, reused
* from WS-5a). One row per (owner, config_type). Parameter shape
* validated at the service layer.
*/
final class FormFieldConfig extends Model
{
use HasFactory;
use HasUlids;
protected $table = 'form_field_configs';
protected static function booted(): void
{
static::addGlobalScope(new FormFieldConfigScope());
}
protected $fillable = [
'owner_type',
'owner_id',
'config_type',
'parameters',
];
/** @var array<string, string> */
protected $casts = [
'config_type' => FormFieldConfigType::class,
'parameters' => 'array',
];
public function owner(): MorphTo
{
return $this->morphTo('owner', 'owner_type', 'owner_id');
}
}

View File

@@ -35,7 +35,6 @@ final class FormFieldLibrary extends Model
'label',
'help_text',
'options',
'validation_rules',
'default_is_required',
'default_is_filterable',
'translations',
@@ -46,7 +45,6 @@ final class FormFieldLibrary extends Model
/** @var array<string, string> */
protected $casts = [
'options' => 'array',
'validation_rules' => 'array',
'translations' => 'array',
'default_is_required' => 'bool',
'default_is_filterable' => 'bool',
@@ -74,4 +72,9 @@ final class FormFieldLibrary extends Model
{
return $this->morphMany(FormFieldValidationRule::class, 'owner');
}
public function configs(): MorphMany
{
return $this->morphMany(FormFieldConfig::class, 'owner');
}
}

View File

@@ -0,0 +1,102 @@
<?php
declare(strict_types=1);
namespace App\Models\Scopes;
use App\Models\FormBuilder\FormField;
use App\Models\FormBuilder\FormFieldLibrary;
use App\Models\FormBuilder\FormSchema;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
/**
* Third sibling in the form-field-child-table scope family, after
* `FormFieldBindingScope` (WS-5a) and `FormFieldValidationRuleScope`
* (WS-5b commit 1). Identical UNION-over-two-owner-chains shape:
*
* owner_id (
* SELECT id FROM form_fields
* WHERE form_schema_id (SELECT id FROM form_schemas WHERE organisation_id = ?)
* UNION
* SELECT id FROM form_field_library
* WHERE organisation_id = ?
* )
*
* Base-class extraction between the three siblings is deliberately
* deferred to WS-5d (where `form_field_options` lands and the fourth
* concrete implementation may clarify what truly varies). Premature
* abstraction from three is still premature.
*/
final class FormFieldConfigScope implements Scope
{
public function __construct(
private readonly ?string $organisationId = null,
) {}
public function apply(Builder $builder, Model $model): void
{
$orgId = $this->resolveOrganisationId();
if ($orgId === null) {
return;
}
$fieldIds = FormField::query()
->withoutGlobalScope(OrganisationScope::class)
->whereIn(
'form_schema_id',
FormSchema::query()
->withoutGlobalScope(OrganisationScope::class)
->where('organisation_id', $orgId)
->select('id'),
)
->select('id');
$libraryIds = FormFieldLibrary::query()
->withoutGlobalScope(OrganisationScope::class)
->where('organisation_id', $orgId)
->select('id');
$table = $model->getTable();
$builder->where(function (Builder $outer) use ($table, $fieldIds, $libraryIds): void {
$outer->where(function (Builder $q) use ($table, $fieldIds): void {
$q->where("$table.owner_type", 'form_field')
->whereIn("$table.owner_id", $fieldIds);
})->orWhere(function (Builder $q) use ($table, $libraryIds): void {
$q->where("$table.owner_type", 'form_field_library')
->whereIn("$table.owner_id", $libraryIds);
});
});
}
private function resolveOrganisationId(): ?string
{
if ($this->organisationId !== null) {
return $this->organisationId;
}
$route = request()->route();
if ($route === null) {
return null;
}
$org = $route->parameter('organisation');
if ($org instanceof \App\Models\Organisation) {
return $org->id;
}
if (is_string($org) && $org !== '') {
return $org;
}
$event = $route->parameter('event');
if ($event instanceof \App\Models\Event) {
return $event->organisation_id;
}
return null;
}
}