From 8117cfb4f48e9f2d6f7ab0f627f37515be3b2308 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Sun, 14 Apr 2024 11:47:48 +0200 Subject: [PATCH] Add number field --- app/Form/Fields/NumberField.php | 84 +++++++++++++++++++ packages/adrema-form | 2 +- .../js/views/formtemplate/FormBuilder.vue | 68 ++++++++------- .../js/views/formtemplate/NumberField.vue | 22 +++++ tests/Feature/Form/FormRegisterActionTest.php | 24 ++++++ tests/Lib/CreatesFormFields.php | 6 ++ 6 files changed, 170 insertions(+), 36 deletions(-) create mode 100644 app/Form/Fields/NumberField.php create mode 100644 resources/js/views/formtemplate/NumberField.vue diff --git a/app/Form/Fields/NumberField.php b/app/Form/Fields/NumberField.php new file mode 100644 index 00000000..5ae05430 --- /dev/null +++ b/app/Form/Fields/NumberField.php @@ -0,0 +1,84 @@ + 'required', 'default' => true, 'rules' => ['required' => 'present|boolean'], 'label' => 'Erforderlich'], + ['key' => 'min', 'default' => null, 'rules' => ['min' => 'present|nullable|numeric'], 'label' => 'minimaler Wert'], + ['key' => 'max', 'default' => null, 'rules' => ['min' => 'present|nullable|numeric'], 'label' => 'maximaler Wert'], + ]; + } + + public static function default(): ?int + { + return null; + } + + public static function fake(Generator $faker): array + { + return [ + 'required' => $faker->boolean(), + 'min' => $faker->numberBetween(0, 100), + 'max' => $faker->numberBetween(0, 100), + ]; + } + + /** + * @inheritdoc + */ + public function getRegistrationRules(Form $form): array + { + $minmax = []; + + if ($this->min !== null) { + $minmax[] = 'gte:' . $this->min; + } + + if ($this->max !== null) { + $minmax[] = 'lte:' . $this->max; + } + + return [$this->key => $this->required ? ['required', 'integer', ...$minmax] : ['nullable', 'integer', ...$minmax]]; + } + + /** + * @inheritdoc + */ + public function getRegistrationAttributes(Form $form): array + { + return [$this->key => $this->name]; + } + + /** + * @inheritdoc + */ + public function getRegistrationMessages(Form $form): array + { + return []; + } + + /** + * @inheritdoc + */ + public function afterRegistration(Form $form, Participant $participant, array $input): void + { + } +} diff --git a/packages/adrema-form b/packages/adrema-form index 57d6236a..ecf008f1 160000 --- a/packages/adrema-form +++ b/packages/adrema-form @@ -1 +1 @@ -Subproject commit 57d6236a930039b56b64da84b2ecca00601485bb +Subproject commit ecf008f12130c8ba18fced50f0a6fe1378d88755 diff --git a/resources/js/views/formtemplate/FormBuilder.vue b/resources/js/views/formtemplate/FormBuilder.vue index d9bee05d..61298f13 100644 --- a/resources/js/views/formtemplate/FormBuilder.vue +++ b/resources/js/views/formtemplate/FormBuilder.vue @@ -2,59 +2,55 @@
- - - + + + - + - + - - - + + + - +
- + @deleteSection="deleteSection($event.detail[0])" @active="updateActive($event.detail[0])">
diff --git a/tests/Feature/Form/FormRegisterActionTest.php b/tests/Feature/Form/FormRegisterActionTest.php index e7113597..c7aec9f9 100644 --- a/tests/Feature/Form/FormRegisterActionTest.php +++ b/tests/Feature/Form/FormRegisterActionTest.php @@ -266,6 +266,30 @@ class FormRegisterActionTest extends FormTestCase ['email' => 'alaaa'], ['email' => 'Mail muss eine gültige E-Mail-Adresse sein.'] ]; + + yield [ + $this->numberField('numb')->name('Nummer')->required(false)->min(10)->max(20), + ['numb' => 21], + ['numb' => 'Nummer muss kleiner oder gleich 20 sein.'] + ]; + + yield [ + $this->numberField('numb')->name('Nummer')->required(false)->min(10)->max(20), + ['numb' => 9], + ['numb' => 'Nummer muss größer oder gleich 10 sein.'] + ]; + + yield [ + $this->numberField('numb')->name('Nummer')->required(false)->min(10)->max(20), + ['numb' => 'asss'], + ['numb' => 'Nummer muss eine ganze Zahl sein.'] + ]; + + yield [ + $this->numberField('numb')->name('Nummer')->required(true), + ['numb' => ''], + ['numb' => 'Nummer ist erforderlich.'] + ]; } public function testItValidatesGroupFieldWithParentGroupField(): void diff --git a/tests/Lib/CreatesFormFields.php b/tests/Lib/CreatesFormFields.php index b932105e..3b676347 100644 --- a/tests/Lib/CreatesFormFields.php +++ b/tests/Lib/CreatesFormFields.php @@ -9,6 +9,7 @@ use App\Form\Fields\DropdownField; use App\Form\Fields\EmailField; use App\Form\Fields\GroupField; use App\Form\Fields\NamiField; +use App\Form\Fields\NumberField; use App\Form\Fields\RadioField; use App\Form\Fields\TextareaField; use App\Form\Fields\TextField; @@ -27,6 +28,11 @@ trait CreatesFormFields return FormtemplateFieldRequest::type(TextField::class)->key($key ?? $this->randomKey()); } + protected function numberField(?string $key = null): FormtemplateFieldRequest + { + return FormtemplateFieldRequest::type(NumberField::class)->key($key ?? $this->randomKey()); + } + protected function emailField(?string $key = null): FormtemplateFieldRequest { return FormtemplateFieldRequest::type(EmailField::class)->key($key ?? $this->randomKey());