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

François Suter fsu-lists at cobweb.ch
Wed May 18 14:45:49 CEST 2011


Hi all,

> I stumbled on a weird behavior of the DateTime data type. Assuming I
> declare a property as having to validate as a DateTime object, i.e.
> [snip]
> 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 would like to sum up my eventual solution in case it helps someone 
stumbling on the same issue.

Instead of using a DateTime object for a non-mandatory date, I'm using 
an integer (the timestamp itself directly). I made my own custom 
validator for the date/time input, which accepts an empty string as a 
valid value. When the value is not empty, it tries to validate by 
instantiating a DateTime object.

On the model side I fell back to a somewhat hacky solution, but which 
works fine: I changed the get method so that it interprets the timestamp 
stored in the corresponding property and returns a DateTime object if 
the timestamp is valid or NULL if not.

This is not really nice, because it means the model is not entirely 
consistent (the setter uses an integer, the getter a DateTime object), 
but didn't come across any issue using this and it's convenient to have 
a DateTime object instead of a timestamp, because I have other fields 
defined as DateTime objects.

Of course, I could also have worked entirely with integers/timestamps. 
Maybe time will prove me wrong ;-)

Cheers

-- 

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


More information about the TYPO3-project-typo3v4mvc mailing list