[TYPO3-mvc] triggering errorClass for form elements from custom validators

Franz Koch typo3.RemoveForMessage at elements-net.de
Fri Feb 26 20:24:39 CET 2010


Hi,

> i they fail, an errormessage is added an the action is redirected to the
> form, showing the message.
> BUT: the errorClass attributes of the not validated field does not
> react! with standard validators they work.
>
> this is my custom validator:
>
> 	public function isValid($group)
> 	{		
> 		// Format. Muss wie erster Teil einer E-Mail sein
> 		if(!preg_match('/^[\w.+-]{2,}$/',$group->getEmail()))
> 		{
> 			return FALSE;
> 		}	
> 	}

just have a look at your validator. How should extbase know that the 
email address failed validation? Your object validator is getting the 
object passed and you return FALSE, so for extbase the object is 
invalid, but it still doesn't know what part of it. There are two 
solutions for this I think:

a) add a custom validator to the property itself and not a global one 
for the model. This can be done by simply adding @validate 
Tx_EbYourExt_Your_Validator_ObjectClass

b) explicitly set a error message for the property by doing this:
if (!isset($this->errors[$propertyName])) {
	$this->errors[$propertyName] = 
t3lib_div::makeInstance('Tx_Extbase_Validation_PropertyError', 
$propertyName);
}
$this->errors[$propertyName]->addErrors(
	array(t3lib_div::makeInstance('Tx_Extbase_Validation_Error', 'here is 
your message', $code))
);


But actually I'm not entirely sure if the later is working - but IIRC it 
does.

-- 
kind regards,
Franz Koch


More information about the TYPO3-project-typo3v4mvc mailing list