Skip to content

Instantly share code, notes, and snippets.

@naag
Created March 1, 2011 15:04
Show Gist options
  • Save naag/849247 to your computer and use it in GitHub Desktop.
Save naag/849247 to your computer and use it in GitHub Desktop.
Multiple argument validation in Extbase
<?php
class Tx_MyExt_Controller_BaseController extends Tx_Extbase_MVC_Controller_ActionController {
/**
* @see Tx_Extbase_MVC_Controller_ActionController::callActionMethod()
*/
protected function callActionMethod() {
$this->validateAll();
parent::callActionMethod();
}
/**
* 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) {
$split = preg_split('/\s+/', $validateAll);
$arguments = array();
foreach ($split as $part) {
if (substr($part, 0, 1) === '$') {
$arguments[] = $this->arguments->getArgument(substr($part, 1));
} else if (substr($part, 0, 3) === 'Tx_') {
$validatorClass = $part;
}
}
$argumentValues = array();
foreach ($arguments as $argument) {
$argumentValues[] = $argument->getValue();
}
$validator = $this->objectManager->get($validatorClass);
call_user_func_array(array($validator, 'isValidAll'), $argumentValues);
foreach ($validator->getErrors() as $error) {
$this->argumentsMappingResults->addError($error, $error->getPropertyName());
}
}
}
}
}
<?php
class Tx_MyExt_Validation_Validator_EqualsValidator extends Tx_Extbase_MVC_Controller_ArgumentsValidator {
/**
* Checks if $a equals $b
*
* @param mixed $a
* @param mixed $b
*/
public function isValidAll($a, $b) {
if ($a !== $b) {
$error = t3lib_div::makeInstance('Tx_Extbase_MVC_Controller_ArgumentError', 'foo');
$error->addErrors(array(t3lib_div::makeInstance('Tx_Extbase_Validation_Error', 'A and B are not equal.', 'notEqual')));
$this->errors[] = $error;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment