Michi
Michi

Reputation: 2520

Set default display value for Symfony2 form field

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".

EDIT:
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

Upvotes: 0

Views: 503

Answers (1)

fsenart
fsenart

Reputation: 5891

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.

Upvotes: 1

Related Questions