-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extend booking information #63
Merged
pascal20997
merged 1 commit into
jweiland-net:master
from
DanielSiepmann:feature/7886-extend-booking-info
Nov 9, 2020
Merged
Extend booking information #63
pascal20997
merged 1 commit into
jweiland-net:master
from
DanielSiepmann:feature/7886-extend-booking-info
Nov 9, 2020
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add further (optional) fields. Add first and last name for each reservation. Allow external code to hook into validation, to add custom validation.
Merged
An example how custom validation can look like:
<?php
namespace WerkraumMedia\Sitepackage\Reserve\Validation;
/*
* Copyright (C) 2020 Daniel Siepmann <coding@daniel-siepmann.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
use JWeiland\Reserve\Domain\Model\Order as OrderEntity;
use JWeiland\Reserve\Domain\Validation\OrderValidator;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;
use TYPO3\CMS\Extbase\Validation\ValidatorResolver;
use TYPO3\CMS\Extbase\Validation\Validator\EmailAddressValidator;
use TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator;
use TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator;
use TYPO3\CMS\Extbase\Validation\Validator\NumberRangeValidator;
class Order
{
/**
* @var ValidatorResolver
*/
private $validatorResolver;
/**
* @var GenericObjectValidator
*/
private $genericObjectValidator;
public function __construct(
ValidatorResolver $validatorResolver,
GenericObjectValidator $genericObjectValidator
) {
$this->validatorResolver = $validatorResolver;
$this->genericObjectValidator = $genericObjectValidator;
}
public function validate(
OrderEntity $order,
ObjectStorage $errorResults
) {
$this->buildValidationForOrder();
$this->buildValidationForParticipants($order);
$errorResults->attach($this->genericObjectValidator->validate($order));
}
private function buildValidationForOrder()
{
$rules = [
'firstName' => [
NotEmptyValidator::class => [],
],
'lastName' => [
NotEmptyValidator::class => [],
],
'email' => [
NotEmptyValidator::class => [],
EmailAddressValidator::class => [],
],
'address' => [
NotEmptyValidator::class => [],
],
'zip' => [
NotEmptyValidator::class => [],
],
'city' => [
NotEmptyValidator::class => [],
],
];
$this->attachValidatorsForRules($rules);
}
private function buildValidationForParticipants(OrderEntity $order): void
{
$rules = [];
foreach (array_keys($order->getParticipants()->toArray()) as $position) {
$rules['participants[' . $position . '].firstName'] = [
NotEmptyValidator::class => [],
];
$rules['participants[' . $position . '].lastName'] = [
NotEmptyValidator::class => [],
];
}
$this->attachValidatorsForRules($rules);
}
private function attachValidatorsForRules(array $rules)
{
foreach ($rules as $propertyName => $validators) {
foreach ($validators as $validatorClassName => $validatorOptions) {
$this->genericObjectValidator->addPropertyValidator(
$propertyName,
$this->validatorResolver->createValidator($validatorClassName, $validatorOptions)
);
}
}
}
public static function register(): void
{
/** @var Dispatcher $dispatcher */
$dispatcher = GeneralUtility::makeInstance(Dispatcher::class);
$dispatcher->connect(OrderValidator::class, 'validateOrder', static::class, 'validate');
}
}
\WerkraumMedia\Sitepackage\Reserve\Validation\Order::register(); |
Love it, looks very nice. Thank you |
sfroemkenjw
pushed a commit
that referenced
this pull request
Nov 17, 2023
…g-info Extend booking information
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add further (optional) fields.
Add first and last name for each reservation.
Allow external code to hook into validation, to add custom validation.