[TYPO3-mvc] Extbase 1.3 Model Validation In Controller

chris Wolff chris at connye.com
Wed Aug 14 13:17:28 CEST 2013


Hi Jan,
i have rewritten my Object Validation to be a Little bit more general

here is my new code:

/**
* Validates an Object against its Model Descriptions @validate Notations
* @param  object  $object          the Domain Object to be Validated
* @param  array  $errorArray       reference to an Array Holding the
Object Errors
* @param  boolean $addCustomErrors should the addCustomError()
function be called if errors exists
* @return boolean                  if the object validates
*/
protected function validateObject($object, &$errorArray = array(),
$addCustomErrors = true){
$errorArray = array();
$className = get_class($object);

$validatorResolver =
$this->objectManager->get('Tx_Extbase_Validation_ValidatorResolver');
$validatorConjunction =
$validatorResolver->getBaseValidatorConjunction($className);
$result = $validatorConjunction->isValid($object);
if($result === true){
return true;
}
$errorArray = $validatorConjunction->getErrors();
if($addCustomErrors){
foreach ($errorArray as $property => $error) {
$this->addCustomError($property,$error->getMessage(),$error->getCode());
}
}
return false;
}

/**
* 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;
}

regards chris

2013/8/14 Jan Kornblum <jan.kornblum at gmx.de>:
> Danke :)
>
>
>
> _______________________________________________
> TYPO3-project-typo3v4mvc mailing list
> TYPO3-project-typo3v4mvc at lists.typo3.org
> http://lists.typo3.org/cgi-bin/mailman/listinfo/typo3-project-typo3v4mvc



-- 
christian wolff - webdeveloper, tech-loving geek and typo3 enthusiast
telefon: +49 30 347 244 88
mobil:  +49 179 49 44 758
email: chris at connye.com
adresse: friedelstraße 31 - 12047 berlin

xing-profil: http://www.xing.com/profile/Christian_Wolff43
google+: https://plus.google.com/u/0/115669673917212236875/posts?hl=de
facebook: https://www.facebook.com/1stMachine
flattr: https://flattr.com/profile/1stmachine


More information about the TYPO3-project-typo3v4mvc mailing list