Tuesday, 7 July 2015

Drupal form API usage tips

How to set default value for date_popup

In New Zealand we often use 'd/m/Y H:i:s' as our time format, so I set it as the format of my date_popup field. But when I tried to set the default value using the same format as below, it wouldn't work for me:
'#default_value' =>date('d/m/Y H:i:s', strtotime('+1 hour')

The reason is that date_popup requires the default value to use the format of 'Y-m-d H:i:s' no matter what we are using for the date_popup format. Therefore it works well after I changed the default value using the required format as the following:
'#default_value' =>date('Y-m-d H:i:s', strtotime('+1 hour')

Always remember to set default value for the fields

For a simple form(one step form), it's better to pre-populate the fields to make it more user-friendly.
For a wizard form(multiple step form), to make it more convenient for users we need to both pre-populate the fields and keep user-input information when user go back and forth.
Based on the aforementioned reasons, it's required to always remember to set default value for the fields. Otherwise, we might have to set the default values for all the fields all at once which is a quite boring work.


 

No comments:

Post a Comment