From 4c2c7fbab2ad56c0d51f81a793c309b962bf0289 Mon Sep 17 00:00:00 2001 From: Torben Hansen Date: Sun, 20 Oct 2024 13:39:57 +0200 Subject: [PATCH] [TASK] Provide custom validation error messages for empty fields Refs #1293 --- .../Validator/RegistrationFieldValidator.php | 11 +++++++---- .../Validator/RegistrationValidator.php | 19 ++++++++++++++----- Resources/Private/Language/locallang.xlf | 3 +++ 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/Classes/Validation/Validator/RegistrationFieldValidator.php b/Classes/Validation/Validator/RegistrationFieldValidator.php index 9e13b6560..5764dfa13 100644 --- a/Classes/Validation/Validator/RegistrationFieldValidator.php +++ b/Classes/Validation/Validator/RegistrationFieldValidator.php @@ -12,7 +12,6 @@ namespace DERHANSEN\SfEventMgt\Validation\Validator; use DERHANSEN\SfEventMgt\Domain\Model\Registration; -use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Persistence\ObjectStorage; use TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator; use TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator; @@ -64,14 +63,18 @@ protected function validateField(Registration\Field $registrationField, ObjectSt protected function getNotEmptyValidator(): NotEmptyValidator { - return GeneralUtility::makeInstance(NotEmptyValidator::class); + $validator = new NotEmptyValidator(); + $validator->setOptions([ + 'nullMessage' => 'LLL:EXT:sf_event_mgt/Resources/Private/Language/locallang.xlf:validation.required_field', + 'emptyMessage' => 'LLL:EXT:sf_event_mgt/Resources/Private/Language/locallang.xlf:validation.required_field', + ]); + + return $validator; } /** * Returns the value for the given registrationField from the given fieldValues * - * @param Registration\Field $registrationField - * @param ObjectStorage $fieldValues * @return string|array */ protected function getFieldValue(Registration\Field $registrationField, ObjectStorage $fieldValues) diff --git a/Classes/Validation/Validator/RegistrationValidator.php b/Classes/Validation/Validator/RegistrationValidator.php index 131515e36..31ddbfe30 100644 --- a/Classes/Validation/Validator/RegistrationValidator.php +++ b/Classes/Validation/Validator/RegistrationValidator.php @@ -97,10 +97,9 @@ protected function validateDefaultFields(Registration $value): void { $defaultFields = ['firstname', 'lastname', 'email']; foreach ($defaultFields as $defaultField) { - $validator = new NotEmptyValidator(); + $validator = $this->getNotEmptyValidator(); $validationResult = $validator->validate($value->_getProperty($defaultField)); if ($validationResult->hasErrors()) { - $result = false; foreach ($validationResult->getErrors() as $error) { $this->result->forProperty($defaultField)->addError($error); } @@ -110,7 +109,6 @@ protected function validateDefaultFields(Registration $value): void $validator = new EmailAddressValidator(); $validationResult = $validator->validate($value->_getProperty('email')); if ($validationResult->hasErrors()) { - $result = false; foreach ($validationResult->getErrors() as $error) { $this->result->forProperty('email')->addError($error); } @@ -152,13 +150,24 @@ protected function getValidator(string $type, string $field): AbstractValidator $validator = new CaptchaValidator($this->configurationManager); $validator->setRequest($this->getRequest()); } else { - $validator = new NotEmptyValidator(); + $validator = $this->getNotEmptyValidator(); } } return $validator; } + protected function getNotEmptyValidator(): NotEmptyValidator + { + $validator = new NotEmptyValidator(); + $validator->setOptions([ + 'nullMessage' => 'LLL:EXT:sf_event_mgt/Resources/Private/Language/locallang.xlf:validation.required_field', + 'emptyMessage' => 'LLL:EXT:sf_event_mgt/Resources/Private/Language/locallang.xlf:validation.required_field', + ]); + + return $validator; + } + protected function validatePriceOption(Registration $registration): void { $event = $registration->getEvent(); @@ -167,7 +176,7 @@ protected function validatePriceOption(Registration $registration): void return; } - $validator = new NotEmptyValidator(); + $validator = $this->getNotEmptyValidator(); $validationResult = $validator->validate($registration->getPriceOption()); if ($validationResult->hasErrors()) { foreach ($validationResult->getErrors() as $error) { diff --git a/Resources/Private/Language/locallang.xlf b/Resources/Private/Language/locallang.xlf index ac5cde18f..ad014b43c 100644 --- a/Resources/Private/Language/locallang.xlf +++ b/Resources/Private/Language/locallang.xlf @@ -369,6 +369,9 @@ Captcha field is missing or invalid. + + This field is required. + Invalid price option selected.