[TYPO3-mvc] Re: Manual validation od domain objects

Philipp philippwrann at gmx.at
Thu Apr 25 18:34:40 CEST 2013


Did it.

wrote an own object validator, that maps the given array using the propertymapper on a specific model. Than does a validation...

Looks like this:

###

<?php

namespace Pixelpoint\Contact\Domain\Validator;

/**
 * @author Philipp Wrann
 * @vendor Pixelpoint
 */
class FormValidator extends \TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator {
	/**
	 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
	 * @inject
	 */
	protected $objectManager;
	
	/**
	 * @var \Pixelpoint\Contact\Domain\Service\RequestService
	 * @inject
	 */
	protected $requestService;
	
	/**
	 * @param \mixed $form
	 * @return \bool
	 */
	public function isValid($form) {
		$className = $this->requestService->getModelName($form['key']);
		$this->requestService->convert($form);
		$validator = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver')->getBaseValidatorConjunction($className);
		/* @var $validator \TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator */
		if (!$validator->isValid($form)) {
			$this->errors = $validator->getErrors();
		}
		return count($this->errors) == 0;
	}
}

?>

###

The create action

###


	
	/**
	 * Save the new request and send it
	 * @param \mixed $form
	 * @validate $form \Pixelpoint\Contact\Domain\Validator\FormValidator
	 */
	public function createAction($form) {
		
		try {
			$this->requestService->convert($form);
			$request = $this->requestService->processRequest($key,$identifier,$form);
			$this->requestLogRepository->add($request);
			$this->redirect('success',NULL,NULL,array('form'=>$form),NULL,NULL,302);
		} catch (\Pixelpoint\Contact\Exception\KeyNotFoundException $error) {
			$this->flashMessageContainer->add('Der übermittelte Schlüssel ist nicht registriert', 1366879495);
		} catch (\Pixelpoint\Contact\Exception\AddressNotRegisteredException $error) {
			$this->flashMessageContainer->add('Die Zieladresse konnte nicht ermittelt werden', 1366879504);
		} catch (\Pixelpoint\Contact\Exception\FormDoesNotImplementRequestInterfaceException $error) {
			$this->flashMessageContainer->add('Das Form-Model ist keine Instanz von RequestInterface', 1366879513);
		} catch (\ValidationException $error) {
			$this->forward('new',NULL,NULL,array('form'=> $form));
		}
		
		$this->view->assignMultiple(array(
			'form' => $form,
			'flashMessages' => $this->flashMessageContainer->getAllMessages()
		));
	}


More information about the TYPO3-project-typo3v4mvc mailing list