diff --git a/app/Form/Fields/CheckboxField.php b/app/Form/Fields/CheckboxField.php index 591ed80c..488eeb97 100644 --- a/app/Form/Fields/CheckboxField.php +++ b/app/Form/Fields/CheckboxField.php @@ -41,7 +41,7 @@ class CheckboxField extends Field /** * @inheritdoc */ - public function getRegistrationRules(): array + public function getRegistrationRules(Form $form): array { return [ $this->key => $this->required ? ['boolean', 'accepted'] : ['present', 'boolean'], @@ -51,7 +51,7 @@ class CheckboxField extends Field /** * @inheritdoc */ - public function getRegistrationAttributes(): array + public function getRegistrationAttributes(Form $form): array { return [ $this->key => $this->name, @@ -61,7 +61,7 @@ class CheckboxField extends Field /** * @inheritdoc */ - public function getRegistrationMessages(): array + public function getRegistrationMessages(Form $form): array { return []; } diff --git a/app/Form/Fields/CheckboxesField.php b/app/Form/Fields/CheckboxesField.php index 87f46a52..6934b2bf 100644 --- a/app/Form/Fields/CheckboxesField.php +++ b/app/Form/Fields/CheckboxesField.php @@ -41,7 +41,7 @@ class CheckboxesField extends Field /** * @inheritdoc */ - public function getRegistrationRules(): array + public function getRegistrationRules(Form $form): array { return [ $this->key => 'array', @@ -52,7 +52,7 @@ class CheckboxesField extends Field /** * @inheritdoc */ - public function getRegistrationAttributes(): array + public function getRegistrationAttributes(Form $form): array { return [ ...collect($this->options)->mapWithKeys(fn ($option, $key) => [$this->key . '.' . $key => $this->name])->toArray(), @@ -63,7 +63,7 @@ class CheckboxesField extends Field /** * @inheritdoc */ - public function getRegistrationMessages(): array + public function getRegistrationMessages(Form $form): array { return []; } diff --git a/app/Form/Fields/DateField.php b/app/Form/Fields/DateField.php index ce10a771..4689f518 100644 --- a/app/Form/Fields/DateField.php +++ b/app/Form/Fields/DateField.php @@ -45,7 +45,7 @@ class DateField extends Field /** * @inheritdoc */ - public function getRegistrationRules(): array + public function getRegistrationRules(Form $form): array { $rules = [$this->required ? 'required' : 'nullable']; @@ -61,7 +61,7 @@ class DateField extends Field /** * @inheritdoc */ - public function getRegistrationAttributes(): array + public function getRegistrationAttributes(Form $form): array { return [ $this->key => $this->name, @@ -71,7 +71,7 @@ class DateField extends Field /** * @inheritdoc */ - public function getRegistrationMessages(): array + public function getRegistrationMessages(Form $form): array { return [ $this->key . '.before_or_equal' => $this->name . ' muss ein Datum vor oder gleich dem ' . now()->format('d.m.Y') . ' sein.', diff --git a/app/Form/Fields/DropdownField.php b/app/Form/Fields/DropdownField.php index f3ad3775..4bf67f32 100644 --- a/app/Form/Fields/DropdownField.php +++ b/app/Form/Fields/DropdownField.php @@ -42,7 +42,7 @@ class DropdownField extends Field /** * @inheritdoc */ - public function getRegistrationRules(): array + public function getRegistrationRules(Form $form): array { return [ $this->key => $this->required ? ['required', 'string', Rule::in($this->options)] : ['nullable', 'string', Rule::in($this->options)], @@ -52,7 +52,7 @@ class DropdownField extends Field /** * @inheritdoc */ - public function getRegistrationAttributes(): array + public function getRegistrationAttributes(Form $form): array { return [$this->key => $this->name]; } @@ -60,7 +60,7 @@ class DropdownField extends Field /** * @inheritdoc */ - public function getRegistrationMessages(): array + public function getRegistrationMessages(Form $form): array { return []; } diff --git a/app/Form/Fields/Field.php b/app/Form/Fields/Field.php index c6f270a9..219b2fe5 100644 --- a/app/Form/Fields/Field.php +++ b/app/Form/Fields/Field.php @@ -34,13 +34,13 @@ abstract class Field extends Data abstract public static function default(); /** @return array */ - abstract public function getRegistrationRules(): array; + abstract public function getRegistrationRules(Form $form): array; /** @return array */ - abstract public function getRegistrationAttributes(): array; + abstract public function getRegistrationAttributes(Form $form): array; /** @return array */ - abstract public function getRegistrationMessages(): array; + abstract public function getRegistrationMessages(Form $form): array; /** @return array */ abstract public static function fake(Generator $faker): array; diff --git a/app/Form/Fields/GroupField.php b/app/Form/Fields/GroupField.php index 3b6784c7..714fb26e 100644 --- a/app/Form/Fields/GroupField.php +++ b/app/Form/Fields/GroupField.php @@ -48,7 +48,7 @@ class GroupField extends Field /** * @inheritdoc */ - public function getRegistrationRules(): array + public function getRegistrationRules(Form $form): array { $rules = [$this->required ? 'required' : 'nullable']; @@ -69,7 +69,7 @@ class GroupField extends Field /** * @inheritdoc */ - public function getRegistrationAttributes(): array + public function getRegistrationAttributes(Form $form): array { return [$this->key => $this->name]; } @@ -77,7 +77,7 @@ class GroupField extends Field /** * @inheritdoc */ - public function getRegistrationMessages(): array + public function getRegistrationMessages(Form $form): array { return []; } diff --git a/app/Form/Fields/NamiField.php b/app/Form/Fields/NamiField.php index 87c2dece..a3204a7b 100644 --- a/app/Form/Fields/NamiField.php +++ b/app/Form/Fields/NamiField.php @@ -37,16 +37,15 @@ class NamiField extends Field /** * @inheritdoc */ - public function getRegistrationRules(): array + public function getRegistrationRules(Form $form): array { $rules = []; - $fields = request()->route('form')->getFields(); - $c = collect($fields) + $c = collect($form->getFields()) ->filter(fn ($field) => $field['for_members'] === true) ->filter(fn ($field) => $field['nami_type'] === null) ->filter(fn ($field) => $field['type'] !== class_basename(static::class)) - ->map(fn ($field) => Field::fromConfig($field)->getRegistrationRules()); + ->map(fn ($field) => Field::fromConfig($field)->getRegistrationRules($form)); foreach ($c as $field) { foreach ($field as $ruleKey => $rule) { @@ -63,20 +62,20 @@ class NamiField extends Field /** * @inheritdoc */ - public function getRegistrationAttributes(): array + public function getRegistrationAttributes(Form $form): array { $rules = []; - $fields = request()->route('form')->getFields(); $inputMembers = request($this->key); - $c = collect($fields) + $c = collect($form->getFields()) ->filter(fn ($field) => $field['type'] !== class_basename(static::class)) ->filter(fn ($field) => $field['for_members'] === true) ->map(fn ($field) => Field::fromConfig($field)); foreach ($c as $field) { - foreach ($field->getRegistrationRules() as $ruleKey => $rule) { + foreach ($field->getRegistrationRules($form) as $ruleKey => $rule) { foreach ($inputMembers as $memberIndex => $inputMember) { + $message = $field->name . ' für ein Mitglied'; $rules = array_merge( $rules, @@ -103,7 +102,7 @@ class NamiField extends Field /** * @inheritdoc */ - public function getRegistrationMessages(): array + public function getRegistrationMessages(Form $form): array { return []; } diff --git a/app/Form/Fields/RadioField.php b/app/Form/Fields/RadioField.php index ff39b5bd..4f24a016 100644 --- a/app/Form/Fields/RadioField.php +++ b/app/Form/Fields/RadioField.php @@ -43,7 +43,7 @@ class RadioField extends Field /** * @inheritdoc */ - public function getRegistrationRules(): array + public function getRegistrationRules(Form $form): array { return [ $this->key => $this->required ? ['required', 'string', Rule::in($this->options)] : ['nullable', 'string', Rule::in($this->options)], @@ -53,7 +53,7 @@ class RadioField extends Field /** * @inheritdoc */ - public function getRegistrationAttributes(): array + public function getRegistrationAttributes(Form $form): array { return [$this->key => $this->name]; } @@ -61,7 +61,7 @@ class RadioField extends Field /** * @inheritdoc */ - public function getRegistrationMessages(): array + public function getRegistrationMessages(Form $form): array { return []; } diff --git a/app/Form/Fields/TextField.php b/app/Form/Fields/TextField.php index 5f87a9d2..46d08e7f 100644 --- a/app/Form/Fields/TextField.php +++ b/app/Form/Fields/TextField.php @@ -38,7 +38,7 @@ class TextField extends Field /** * @inheritdoc */ - public function getRegistrationRules(): array + public function getRegistrationRules(Form $form): array { return [$this->key => $this->required ? ['required', 'string'] : ['nullable', 'string']]; } @@ -46,7 +46,7 @@ class TextField extends Field /** * @inheritdoc */ - public function getRegistrationAttributes(): array + public function getRegistrationAttributes(Form $form): array { return [$this->key => $this->name]; } @@ -54,7 +54,7 @@ class TextField extends Field /** * @inheritdoc */ - public function getRegistrationMessages(): array + public function getRegistrationMessages(Form $form): array { return []; } diff --git a/app/Form/Fields/TextareaField.php b/app/Form/Fields/TextareaField.php index 1b2f0760..cceee2b9 100644 --- a/app/Form/Fields/TextareaField.php +++ b/app/Form/Fields/TextareaField.php @@ -39,7 +39,7 @@ class TextareaField extends Field /** * @inheritdoc */ - public function getRegistrationRules(): array + public function getRegistrationRules(Form $form): array { return [$this->key => $this->required ? ['required', 'string'] : ['nullable', 'string']]; } @@ -47,7 +47,7 @@ class TextareaField extends Field /** * @inheritdoc */ - public function getRegistrationAttributes(): array + public function getRegistrationAttributes(Form $form): array { return [$this->key => $this->name]; } @@ -55,7 +55,7 @@ class TextareaField extends Field /** * @inheritdoc */ - public function getRegistrationMessages(): array + public function getRegistrationMessages(Form $form): array { return []; } diff --git a/app/Form/Models/Form.php b/app/Form/Models/Form.php index 2fa25e9c..85f038bb 100644 --- a/app/Form/Models/Form.php +++ b/app/Form/Models/Form.php @@ -74,7 +74,7 @@ class Form extends Model implements HasMedia return [ ...$carry, - ...$field->getRegistrationRules(), + ...$field->getRegistrationRules($this), ]; }, []); } @@ -89,7 +89,7 @@ class Form extends Model implements HasMedia return [ ...$carry, - ...$field->getRegistrationMessages(), + ...$field->getRegistrationMessages($this), ]; }, []); } @@ -104,7 +104,7 @@ class Form extends Model implements HasMedia return [ ...$carry, - ...$field->getRegistrationAttributes(), + ...$field->getRegistrationAttributes($this), ]; }, []); } diff --git a/phpstan.neon b/phpstan.neon index c5a8f0b1..99ad4acc 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -547,3 +547,13 @@ parameters: message: "#^Unable to resolve the template type TValue in call to function collect$#" count: 1 path: app/Form/Models/Form.php + + - + message: "#^Unable to resolve the template type TKey in call to function collect$#" + count: 1 + path: app/Form/Fields/NamiField.php + + - + message: "#^Unable to resolve the template type TValue in call to function collect$#" + count: 1 + path: app/Form/Fields/NamiField.php diff --git a/tests/Feature/Form/FormtemplateFieldRequest.php b/tests/Feature/Form/FormtemplateFieldRequest.php index 11b045d9..56ca47b9 100644 --- a/tests/Feature/Form/FormtemplateFieldRequest.php +++ b/tests/Feature/Form/FormtemplateFieldRequest.php @@ -18,6 +18,7 @@ use Worksome\RequestFactories\RequestFactory; * @method self parentGroup(int $groupId) * @method self parentField(string $fieldKey) * @method self namiType(?NamiType $type) + * @method self forMembers(bool $forMembers) */ class FormtemplateFieldRequest extends RequestFactory {