From 63583b9b25ab8d579117d4a58184e9a99899b53a Mon Sep 17 00:00:00 2001 From: philipp lang Date: Fri, 12 Apr 2024 15:42:43 +0200 Subject: [PATCH] Add Boolean presenter for checkbox --- app/Form/Data/FieldCollection.php | 5 ++++- app/Form/Fields/CheckboxField.php | 7 +++++++ app/Form/Presenters/BooleanPresenter.php | 15 +++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 app/Form/Presenters/BooleanPresenter.php diff --git a/app/Form/Data/FieldCollection.php b/app/Form/Data/FieldCollection.php index 5ec04a75..3c6c6d7b 100644 --- a/app/Form/Data/FieldCollection.php +++ b/app/Form/Data/FieldCollection.php @@ -62,7 +62,10 @@ class FieldCollection extends Collection */ public static function fromRequest(Form $form, array $input): self { - return $form->getFields()->each(fn ($field) => $field->value = array_key_exists($field->key, $input) ? $input[$field->key] : $field->default()); + return $form->getFields()->map(function ($field) use ($input) { + $field->value = array_key_exists($field->key, $input) ? $input[$field->key] : $field->default(); + return $field; + }); } public function find(Field $givenField): ?Field diff --git a/app/Form/Fields/CheckboxField.php b/app/Form/Fields/CheckboxField.php index 35ada00e..511c039a 100644 --- a/app/Form/Fields/CheckboxField.php +++ b/app/Form/Fields/CheckboxField.php @@ -4,6 +4,8 @@ namespace App\Form\Fields; use App\Form\Models\Form; use App\Form\Models\Participant; +use App\Form\Presenters\BooleanPresenter; +use App\Form\Presenters\Presenter; use Faker\Generator; use Illuminate\Validation\Rule; @@ -72,4 +74,9 @@ class CheckboxField extends Field public function afterRegistration(Form $form, Participant $participant, array $input): void { } + + public function getPresenter(): Presenter + { + return app(BooleanPresenter::class); + } } diff --git a/app/Form/Presenters/BooleanPresenter.php b/app/Form/Presenters/BooleanPresenter.php new file mode 100644 index 00000000..24541b43 --- /dev/null +++ b/app/Form/Presenters/BooleanPresenter.php @@ -0,0 +1,15 @@ +