[TYPO3-mvc] Validation of non-mandatory DateTime fields

François Suter fsu-lists at cobweb.ch
Sun May 1 18:46:51 CEST 2011


Hi all,

> If the date is wrong upon input (say somebody entered "xxxx"), I'll get
> a validation error and the input form gets reloaded with an empty value
> in the Date field. That's fine.
>
> However if I leave the field empty (which is supposed to be fine,
> because it's not mandatory) and the form gets reloaded because of some
> other input error, the field is now filled with the current date, which
> is not desirable. At which point in the process was the current date
> "injected" into my property? Is there any way to prevent this?

I've gotten back to this topic and have found what seems to be a 
reasonable solution. I wanted to check it here before submitting a 
possible patch.

The problem - as I see it - comes from the following test inside 
Tx_Extbase_Validation_Validator_DateTimeValidator:

if ($value instanceof DateTime) return TRUE;

If the field has been left empty, $value is NULL and thus the test 
fails. But this is wrong, because if your field is not mandatory, a NULL 
value is acceptable. All the more so, because if you define your 
property to be "NotEmpty", the NULL will be caught there. So IMO, it's 
ok to change the test to:

if ($value == NULL || $value instanceof DateTime) return TRUE;

What do you think?

Cheers

-- 

Francois Suter
Cobweb Development Sarl - http://www.cobweb.ch


More information about the TYPO3-project-typo3v4mvc mailing list