[TYPO3-mvc] Validate form field depending on another passed objects property
Jan Kornblum
jan.kornblum at gmx.de
Thu Nov 13 16:54:09 CET 2014
Hi Matthew,
> you have two options:
>
> Validate in the controller action:
>
> * @validate $objectName Validator
>
> E.g.
>
> * @validate $booking
> \NAMESPACE\Extname\Validation\Validator\BookingValidator
>
> or
>
> * @validate $stringVar String, StringLength(minimum=3,maximum=20)
>
>
> Or you can validate in the Controller method
> "initializeActionMethodValidators", this one of the "well documented"
> features of Extbase...
> ***************************************************
> protected function initializeActionMethodValidators() {
> if ($this->actionMethodName == 'insertAction') {
> parent::initializeActionMethodValidators();
> $booking = $this->arguments['booking'];
> $validator = $booking->getValidator();
> $extTermsAndConditionsRequiredValidator =
> \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
>
> 'NAMESPACE\Extname\Validation\Validator\Validator\ExtTermsAndConditionsRequiredValidator',
> array('someOptionToBeHandedOverToTheValidator' =>
> $someOptionToBeHandedOverToTheValidator)
> );
> $validator->addValidator($enoughTicketsAvailableValidator);
> }
> }
> ***************************************************
>
> And the validator class must be something like this:
> ***************************************************
> class ExtTermsAndConditionsRequiredValidator extends
> \TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator {
>
> /**
> * Booking repository
> *
> * @var \NAMESPACE\Extname\Domain\Repository\BookingRepository
> * @inject
> */
> protected $bookingRepository;
>
> /**
> * Event repository
> *
> * @var \NAMESPACE\Extname\Domain\Repository\EventRepository
> * @inject
> */
> protected $eventRepository;
>
>
> /**#
> * This contains the supported options, their default values,
> * descriptions and types.
> *
> * @var array
> */
> protected $supportedOptions = array(
> 'someOptionToBeHandedOverToTheValidator' =>
> array(
> 5,
> 'The maximum amount of reservations',
> 'integer'),
> 'event' =>
> array(
> NULL,
> 'The event object',
> '\NAMESPACE\Extname\Domain\Model\Event')
> );
>
> /**
> *
> *
> * @param \NAMESPACE\Extname\Domain\Repository\Booking $booking
> * @return boolean TRUE if the value is valid
> * @api
> */
> public function isValid($booking) {
> // Need your own validation checking here
> if ($thisCrapDoesntValidate) {
> $error = new \TYPO3\CMS\Extbase\Validation\Error('', time());
> $this->result->forProperty(
> 'error_tag_free_to_define'
> )->addError($error);
> return FALSE;
> }
> return TRUE;
> }
> }
> ***************************************************
> --
> Cheers,
> Matthew
great, thanks a lot!
Kind regards, Jan
More information about the TYPO3-project-typo3v4mvc
mailing list