[TYPO3-mvc] Complex validation scenario

Jacob Rasmussen jacob at lightbulb.dk
Wed Jun 22 09:43:05 CEST 2011


Hi Francois,

Den 22/06/11 09.00, François Suter skrev:
> Now - as far as I could see - a validator receives only the value it is
> supposed to validate and thus can't refer to the other submitted values.
> So 2 questions:
>
> - is there any way to refer to others submitted values from within a
> validator?

It is possible to create a custom validator which accepts several 
values, as far as I remember there is an example in the wiki.

I did the following:
- Overloaded callActionMethod() in my controllers to be able to call a 
custom method before calling the default callActionMethod
- In my custom method:
/**
  * Implement controller action annotation @validateAll.
  *
  * Example: @validateAll $foo $bar 
Tx_MyExt_Validation_Validator_FooBarValidator
  *
  * This will call 
Tx_MyExt_Validation_Validator_FooBarValidator->isValidAll($foo, $bar)
  *
  * @see Tx_Extbase_MVC_Controller_ActionController::callActionMethod()
  */
protected function validateAll() {
   $methodTagsValues = 
$this->reflectionService->getMethodTagsValues(get_class($this), 
$this->actionMethodName);
		
   if (isset($methodTagsValues['validateAll'])) {
     foreach ($methodTagsValues['validateAll'] as $validateAll) {
       $parsedAnnotation = $this->parseValidatorAnnotation($validateAll);

       $validator = 
$this->objectManager->get($parsedAnnotation['validator']['validatorName']);
$validator->setOptions($parsedAnnotation['validator']['validatorOptions']);
       call_user_func_array(array($validator, 'isValidAll'), 
$parsedAnnotation['arguments']);
       foreach ($validator->getErrors() as $error) {
         $this->argumentsMappingResults->addError($error, 
$error->getPropertyName());
       }
     }
   }
}

Since I was in a bit of a hurry, I took some of the code from 
Tx_Extbase_Validation_ValidatorResolver instead of instantiating - this 
part could probably needs some restructuring at some point.

With this method it is possible to use the new annotation, in my example:
* @validateAll $delivery $alternate 
Tx_Blshop_Domain_Validator_OptionalAddressValidator(mandatory="name,address,zip,city", 
optional="company")

And in my validator I use the arguments as params:
	/**
	 * @param Tx_Extbase_MVC_Controller_Argument $addressArg The address 
that should be validated
	 * @param Tx_Extbase_MVC_Controller_Argument $flagArg
	 *
	 * @return boolean TRUE if the address is valid, FALSE if an error occured
	 */
	public function isValidAll(Tx_Extbase_MVC_Controller_Argument 
$addressArg, Tx_Extbase_MVC_Controller_Argument $flagArg = NULL) {


-- 
Regards

Jacob Rasmussen
Certified TYPO3 Integrator


More information about the TYPO3-project-typo3v4mvc mailing list