From 59ec6a2145f3012fc80cfdb4c57479c01902e986 Mon Sep 17 00:00:00 2001 From: Tom Udding Date: Fri, 8 Sep 2023 21:03:43 +0200 Subject: [PATCH] Add validator to `Text` type sign-up list field Adds `NotEmpty` validator and a `StringTrim` filter to make sure the field value is never the empty string. --- .../src/Form/Element/ValidatedText.php | 55 +++++++++++++++++++ module/Activity/src/Form/Signup.php | 3 +- 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 module/Activity/src/Form/Element/ValidatedText.php diff --git a/module/Activity/src/Form/Element/ValidatedText.php b/module/Activity/src/Form/Element/ValidatedText.php new file mode 100644 index 0000000000..6880e894b6 --- /dev/null +++ b/module/Activity/src/Form/Element/ValidatedText.php @@ -0,0 +1,55 @@ +validator) { + $this->validator = new NotEmpty(NotEmpty::STRING); + } + + return $this->validator; + } + + /** + * Provide default input rules for this element + * + * Attaches a not empty validator and a string trim filter. + * + * @inheritDoc + */ + public function getInputSpecification(): array + { + $spec = [ + 'required' => true, + 'filters' => [ + ['name' => StringTrim::class], + ], + 'validators' => [ + $this->getValidator(), + ], + ]; + + $name = $this->getName(); + if (null !== $name) { + $spec['name'] = $name; + } + + return $spec; + } +} diff --git a/module/Activity/src/Form/Signup.php b/module/Activity/src/Form/Signup.php index 52aa1e1bdf..aa8305714b 100644 --- a/module/Activity/src/Form/Signup.php +++ b/module/Activity/src/Form/Signup.php @@ -4,6 +4,7 @@ namespace Activity\Form; +use Activity\Form\Element\ValidatedText; use Activity\Model\SignupField as SignupFieldModel; use Activity\Model\SignupList as SignupListModel; use Laminas\Captcha\Image as ImageCaptcha; @@ -135,7 +136,7 @@ protected function createSignupFieldElementArray(SignupFieldModel $field): array switch ($field->getType()) { case 0: //'Text' - $result['type'] = 'Text'; + $result['type'] = ValidatedText::class; break; case 1: //'Yes/No' $result['type'] = Radio::class;