Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wolffc/00f3b7a2f6394949de63 to your computer and use it in GitHub Desktop.
Save wolffc/00f3b7a2f6394949de63 to your computer and use it in GitHub Desktop.
This is an TYPO3 / Extbase Example on how to Validate some Properties in the Controller - this is usefull for conditonal Validation
<?php
class foo {
/**
* keep trac if we have created a custom error.
* @var boolean
*/
protected $hasCustomError = false;
/**
* Dies beispiel zeigt wie die validierung manuell durchführt.
* @param namespace\package\Domain\Model\MyModelWithoutValidation $myObjectWithoutValidation dieses model besitzt keine Validierung annotations
* wir geben auch keine manuell @validate an so das die controller action argument nicht validiert wird.
*/
public function myCustomAction($myObjectWithoutValidation){
//** da wir werde validierung im moden noch im kontroller haben müssen wir nun die validierung manuell durchführen.
//** hier validieren wir eine email adresse auf "füllung" und email und das sie nicht länger als 50 zeichen ist (weil z.b unser DB feld nicht länger ist).
$emailValue = $MyModelWithoutValidation-getEmail();
$this->validatePropertyByValidator('email',$emailValue,'NotEmpty');
$this->validatePropertyByValidator('email',$emailValue,'EmailAddress');
$this->validatePropertyByValidator('email',$emailValue,'StringLength',array('maximum'=>50));
// if a custom validation Error has been triggered
if($this->hasCustomError){
$this->errorAction();
}
}
/**
* Validates property by a given validator and genereated the correct errors for handling
* usful for validating conditional properties in conroller
* @param string $propertyName the name of the property to be validated
* @param mixed $propertyValue the value to be validated
* @param string $validatorName full class name of the validator or short form for Default Extbase Validators
* @param mixed $validatorParameters the parameter array for the given Validator or NULL
* @return boolean returns the validation result
*/
protected function validatePropertyByValidator($propertyName, $propertyValue, $validatorName, $validatorParameters=NULL){
$validatorName = $this->getValidatorClassNameFromShortName($validatorName);
if(TYPO3_branch == '4.5'){
$validator = $this->objectManager->get($validatorName);
if(is_array($validatorParameters)){
$validator->setOptions($validatorParameters);
}
}else{
if(is_null($validatorParameters)){
$validatorParameters = array();
}
$validator = $this->objectManager->get($validatorName, $validatorParameters);
}
$result = $validator->isValid($propertyValue);
$errors = $validator->getErrors();
foreach($errors as $currentError){
$this->addCustomError($propertyName,$currentError->getMessage(), $currentError->getCode());
}
return $result;
}
/**
* Returns the Full Class name form a Short "validator name"
* @param string $validatorName the name of the validator
* @return string the full classname
*/
protected function getValidatorClassNameFromShortName($validatorName){
if(substr($validatorName, 0,3) === 'Tx_'){
return $validatorName;
}
if(TYPO3_branch == '4.5'){
$validatorName = 'Tx_Extbase_Validation_Validator_'.$validatorName.'Validator';
}elseif( substr($validatorName,0,1) != '\\'){
$validatorName = '\\TYPO3\CMS\\Extbase\\Validation\\Validator\\'.$validatorName.'Validator';
}
return $validatorName;
}
/**
* Creates a Custom Error for Attributes wich are Evaluated in the Controller
* @param string $propertyName name of the property with an error
* @param string $message the default message if not Translated
* @param integer $code the error code needs to be unique
*/
protected function addCustomError($propertyName,$message,$code){
$error = $this->objectManager->get('Tx_Extbase_MVC_Controller_ArgumentError', $propertyName);
$propertyErrors = array();
$propertyErrors[$propertyName] = $this->objectManager->get('Tx_Extbase_Validation_Error',$message,$code);
$error->addErrors($propertyErrors);
$this->propertyMapper->getMappingResults()->addError($error,$propertyName);
$this->hasCustomError = true;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment