[TYPO3-mvc] Validate form field depending on another passed objects property

Matthew Colton mat.colton at web-xs.de
Thu Nov 13 15:22:33 CET 2014


Hi Jan,
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

-- 
Mit freundlichen Grüßen
Matthew Colton

Am 13.11.2014 um 12:58 schrieb Jan Kornblum:
> Hi newsgroup,
> 
> i have to validate a form property depending on the value of another
> passed objects property. The two objects passed to the form
> (createAction) are:
> 
> - $booking (the object to be created)
> - $event.
> 
> The property "$booking->extTermsAndConditions" should be validated as
> boolean (and must be TRUE) when "$event->extTermsAndConditonsRequired"
> is TRUE.
> 
> In case of "$event->extTermsAndConditionsRequired" is FALSE the property
> "$booking->extTermsAndConditions" should not be validated at all! So, i
> cannot set the validation directly in the model.
> 
> I want the validation results to be available in the form
> ("f:form.validationResults"), too.
> 
> How can i accomplish this?
> 
> Kind regards, Jan
> 
> 
> _______________________________________________
> TYPO3-project-typo3v4mvc mailing list
> TYPO3-project-typo3v4mvc at lists.typo3.org
> http://lists.typo3.org/cgi-bin/mailman/listinfo/typo3-project-typo3v4mvc
> 


More information about the TYPO3-project-typo3v4mvc mailing list