feat(form-builder): form_field_validation_rules table + polymorphic owner + scope + cascade
This commit is contained in:
@@ -7,8 +7,10 @@ namespace Database\Factories\FormBuilder;
|
||||
use App\Enums\FormBuilder\FormFieldBindingMode;
|
||||
use App\Enums\FormBuilder\FormFieldDisplayWidth;
|
||||
use App\Enums\FormBuilder\FormFieldType;
|
||||
use App\Enums\FormBuilder\FormFieldValidationRuleType;
|
||||
use App\Models\FormBuilder\FormField;
|
||||
use App\Models\FormBuilder\FormFieldBinding;
|
||||
use App\Models\FormBuilder\FormFieldValidationRule;
|
||||
use App\Models\FormBuilder\FormSchema;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Illuminate\Support\Str;
|
||||
@@ -100,4 +102,21 @@ final class FormFieldFactory extends Factory
|
||||
->create();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Attach a validation-rule row in `form_field_validation_rules` after
|
||||
* the field is persisted. Replaces populating the legacy
|
||||
* `validation_rules` JSON column — which WS-5b commit 5 drops.
|
||||
*
|
||||
* @param array<string, mixed> $parameters
|
||||
*/
|
||||
public function withValidationRule(FormFieldValidationRuleType $type, array $parameters): static
|
||||
{
|
||||
return $this->afterCreating(function (FormField $field) use ($type, $parameters): void {
|
||||
FormFieldValidationRule::factory()
|
||||
->forField($field)
|
||||
->ofType($type, $parameters)
|
||||
->create();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,8 +6,10 @@ namespace Database\Factories\FormBuilder;
|
||||
|
||||
use App\Enums\FormBuilder\FormFieldBindingMode;
|
||||
use App\Enums\FormBuilder\FormFieldType;
|
||||
use App\Enums\FormBuilder\FormFieldValidationRuleType;
|
||||
use App\Models\FormBuilder\FormFieldBinding;
|
||||
use App\Models\FormBuilder\FormFieldLibrary;
|
||||
use App\Models\FormBuilder\FormFieldValidationRule;
|
||||
use App\Models\Organisation;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Illuminate\Support\Str;
|
||||
@@ -71,4 +73,21 @@ final class FormFieldLibraryFactory extends Factory
|
||||
->create();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Attach a validation-rule row in `form_field_validation_rules` after
|
||||
* the library entry is persisted. Replaces populating the legacy
|
||||
* `validation_rules` JSON column — which WS-5b commit 5 drops.
|
||||
*
|
||||
* @param array<string, mixed> $parameters
|
||||
*/
|
||||
public function withValidationRule(FormFieldValidationRuleType $type, array $parameters): static
|
||||
{
|
||||
return $this->afterCreating(function (FormFieldLibrary $library) use ($type, $parameters): void {
|
||||
FormFieldValidationRule::factory()
|
||||
->forLibrary($library)
|
||||
->ofType($type, $parameters)
|
||||
->create();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Database\Factories\FormBuilder;
|
||||
|
||||
use App\Enums\FormBuilder\FormFieldValidationRuleType;
|
||||
use App\Models\FormBuilder\FormField;
|
||||
use App\Models\FormBuilder\FormFieldLibrary;
|
||||
use App\Models\FormBuilder\FormFieldValidationRule;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/** @extends Factory<FormFieldValidationRule> */
|
||||
final class FormFieldValidationRuleFactory extends Factory
|
||||
{
|
||||
protected $model = FormFieldValidationRule::class;
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'owner_type' => 'form_field',
|
||||
'owner_id' => FormField::factory(),
|
||||
'rule_type' => FormFieldValidationRuleType::MinLength->value,
|
||||
'parameters' => ['value' => 3],
|
||||
'error_message_key' => null,
|
||||
];
|
||||
}
|
||||
|
||||
public function forField(FormField $field): static
|
||||
{
|
||||
return $this->state(fn () => [
|
||||
'owner_type' => 'form_field',
|
||||
'owner_id' => $field->id,
|
||||
]);
|
||||
}
|
||||
|
||||
public function forLibrary(FormFieldLibrary $library): static
|
||||
{
|
||||
return $this->state(fn () => [
|
||||
'owner_type' => 'form_field_library',
|
||||
'owner_id' => $library->id,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $parameters
|
||||
*/
|
||||
public function ofType(FormFieldValidationRuleType $type, array $parameters): static
|
||||
{
|
||||
return $this->state(fn () => [
|
||||
'rule_type' => $type->value,
|
||||
'parameters' => $parameters,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user