Take the 2-minute tour ×

I've got a form with some display only fields in it. These fields are usually DateTime values... but when empty/null I would like to display the string "never".

To be more explicit: The field should show the DateTime value from the database and if null the string 'never' should be displayed.

How should I do that?

Thanks in advance

share|improve this question

1 Answer 1

up vote 1 down vote accepted

You can use Symfony2 Data Transformers :

  • In the transform() function you can check if your date is null and then return the 'never' string. Otherwise return a string representation of your date.
  • In the reverseTransform() function you can check if the string is 'never' and then construct a null DateTime object. Otherwise, you transform the given string into a valid DateTime object with something like 'strtotime()` PHP function.
share|improve this answer

Your Answer


