Posted by noobizness on February 18, 2010 at 4:35pm
I know this is simple but .. trying to learn custom node modules. How do you deal with date type data? both: how do you create a date widget in the form definition AND how do you validate date data after the form submits? I am actually unsure of how in general you validate form data - what is the hook for that?
thanks
Comments
For the benefit of others ...
if you choose the 'date' type you get select items for each date element ... not bad as far as it goes, but the best way is to use the Date API module and the 'date_select' type to enable jQuery date popups.
The Drupal date field type returns dates as ARRAYS of date parts which require reassembly before submitting them to a database. (you'd need to keep the data as int or timestamp probably, if you use the below function as a solution, or encase the mktime() function with a date_format() call to pound it back to a string if you want to store it in a date or datetime field in the DB.
function _drupaldate2unixtime($drupal_date) { $year = $drupal_date["year"]; $month = $drupal_date["month"]; $day = $drupal_date["day"]; $hour = $drupal_date['hour']; $minute = $drupal_date['minute']; $second = 0; return mktime($hour, $minute, $second, (int)$month, (int)$day, (int)$year); }the jquery/date_select method on the other hand returns dates as strings suitable for a datetime storage in SQL without any post-processing.