-
-
Save dreadwarrior/1633059 to your computer and use it in GitHub Desktop.
Multiple argument validation in Extbase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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()); | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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