[TYPO3-mvc] yes, nested objects validation is a missing feature at current extbase version (1.3)

Steffen Ritter info at rs-websystems.de
Thu Aug 11 10:04:38 CEST 2011


> Does anybody can suggest a proven approach to workaround this?
YES :) write your own Model-Validator, which calls the validators to the 
subojbects, and add theire errors as propertyErrors....

class Tx_Rs***_Domain_Validator_OrderValidator extends 
Tx_Extbase_Validation_Validator_AbstractValidator {

	/**
	 * @var $objectManager Tx_Extbase_Object_ObjectManager
	 */
	protected $objectManager;

	public function injectObjectManager(Tx_Extbase_Object_ObjectManager 
$objMgr) {
		$this->objectManager = $objMgr;
	}
	/**
	 * Checks if the given value is valid according to the validator.
	 *
	 * If at least one error occurred, the result is FALSE and any errors can
	 * be retrieved through the getErrors() method.
	 *
	 * Note that all implementations of this method should set 
$this->errors() to an
	 * empty array before validating.
	 *
	 * @param mixed $value The value that should be validated
	 * @return boolean TRUE if the value is valid, FALSE if an error occured
	 */
	public function isValid($value) {
		if(!is_object($value) || $value instanceof Tx_Rs***_Domain_Model_Order) {
			return FALSE;
		}

		$addressValidator = 
$this->objectManager->get('Tx_Extbase_Validation_ValidatorResolver')->getBaseValidatorConjunction('Tx_Rs***_Domain_Model_OrderAddress');
		if (!$addressValidator->isValid($value->getBillAddress())) {
			$this->errors[] = $this->createPropertyError('billAddress', 
$addressValidator->getErrors());
		}

		return count($this->errors) == 0;
	}

	/**
	 * @param string $propertyName
	 * @param array $errors
	 * @return Tx_Extbase_Validation_PropertyError
	 */
	protected function createPropertyError($propertyName, array $errors) {
		$error = new Tx_Extbase_Validation_PropertyError($propertyName);
		$error->addErrors($errors);
		return $error;
	}
}


More information about the TYPO3-project-typo3v4mvc mailing list