

How to ensure only future (or past) dates can be submitted to a date component of a Drupal Webform.
This is where we need to get a little funky with the 'Additional Validation' field under 'Webform Advanced Settings'.
First of all, find out the name of the date field you are wanting to validate. You can do this via the database (look for it in the webform_component table and note it's form_key field), or, the preferred route is to use the Firefox Web Developer toolbar, enable 'Display Form Details' and note down the name(s) of the inputs.
Go to the form, click on 'edit', scroll down to the 'Webform Advanced Settings' and expand.
Examine, modify and then insert this little bit of code:-
<?php
$preferred_date = mktime("00", "00", "00",
$form_values['submitted_tree']['destination']['preferred_travel_date']['month'],
$form_values['submitted_tree']['destination']['preferred_travel_date']['day'],
$form_values['submitted_tree']['destination']['preferred_travel_date']['year']);
if ($preferred_date < time())
{
form_set_error('submitted][preferred_travel_date', t('Your date is in the past,
please select a valid travel date'));
}
?>
Note, in this instance, the date picker is the three field type with day, month, year drop downs. Also, in this instance, the date picker is embedded in a fieldset 'destination', so remove or rename this according to your installation.
Finally, note the "$preferred_date < time()" clause, reverse the < to > if you're wanting dates only in the past.
Comments
Less and Equal
How do you make the date to include the today date. I am adding the equal sign (=) to the right of the more sigh (<) and it does not work for me. It works otherwise just fine, but I need the today's day as well. Please help. Thanks you very much for the help.
Nice! Just what I was looking
Nice! Just what I was looking for! Thanks!
Post new comment