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

François Suter fsu-lists at cobweb.ch
Tue May 17 14:35:31 CEST 2011


Hi again,

> 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?

I'm coming back to this thread after digging some more into this issue. 
The above was finally submitted as a patch [1], but it does not really 
solve the problem.

Indeed when an instance model object containing a DateTime property is 
created, that DateTime object will contain the current date and time if 
the underlying value (i.e. the database value) is 0 or NULL. This makes 
it impossible to have an empty date field when using the DateTime object.

It seems like the real solution was indeed the one mentioned by 
SteffenR: ignore the DateTime object (at least for non-mandatory fields) 
and do your own validation in such a case.

Cheers

-- 

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

[1] https://review.typo3.org/#change,2147


More information about the TYPO3-project-typo3v4mvc mailing list