Skip to content

Commit

Permalink
[TASK] Provide custom validation error messages for empty fields
Browse files Browse the repository at this point in the history
Refs #1293
  • Loading branch information
derhansen committed Oct 20, 2024
1 parent 10dd1b2 commit 4c2c7fb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
11 changes: 7 additions & 4 deletions Classes/Validation/Validator/RegistrationFieldValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 14 additions & 5 deletions Classes/Validation/Validator/RegistrationValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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();
Expand All @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions Resources/Private/Language/locallang.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,9 @@
<trans-unit id="validation.missing_captcha">
<source>Captcha field is missing or invalid.</source>
</trans-unit>
<trans-unit id="validation.required_field">
<source>This field is required.</source>
</trans-unit>
<trans-unit id="validation.invalid_priceoption">
<source>Invalid price option selected.</source>
</trans-unit>
Expand Down

0 comments on commit 4c2c7fb

Please sign in to comment.