As I was having date validation problems using Mike Vranckx answer above, I needed to implement a custom validation method to get around the The specified date is invalid.
error.
First add a custom #element_validate function to the form element:
$form['dob'] = array(
'#type' => 'date',
'#title' => t('Date Of Birth'),
'#weight' => 9,
'#pre_render' => array(
'_form_date_empty',
),
'#element_validate' => array('_form_date_empty_validate'),
);
However it seems that setting this value will prevent the default date_validate()
function from running, which still has some value, we just want to make sure it doesn't prevent blank dates:
function _form_date_empty_validate($element, &$form_state, $form) {
$date = $element['#value'];
if (($date['year'] || $date['month'] || $date['day'])) {
date_validate($element, $form_state, $form);
}
}