HTML_Quickform adding a date

I've been mucking with html_quickform, to get dates nicely.

I hope this helps others.

Create the usual group of input fields then rules to validate them.

$dateform = array();
$dateform[] = &HTML_QuickForm::createElement('text', 'date_purchase_day', '', array('size' => 2, 'maxlength' => 2));
$dateform[] = &HTML_QuickForm::createElement('text', 'date_purchase_month', '', array('size' => 2, 'maxlength' => 2));
$dateform[] = &HTML_QuickForm::createElement('text', 'date_purchase_year', '', array('size' => 4, 'maxlength' => 4));
$form->addGroup($dateform,'date_purchase','Purchase Date:<div style="font-weight: normal; font-size: .8em;">DD/MM/YYYY</div>', '', false);

// adding the rules for the date fields
$form->addGroupRule('date_purchase', array(
'date_purchase_day' => array(
array('Date must be numeric, in the form DDMMYYYY', 'numeric'),
array('Date must be numeric, in the form DDMMYYYY', 'rangelength', array(2, 2))
),
'date_purchase_month' => array(
array('Date must be numeric, in the form DDMMYYYY', 'numeric'),
array('Date must be numeric, in the form DDMMYYYY', 'rangelength', array(2, 2))
),
'date_purchase_year' => array(
array('Date must be numeric, in the form DDMMYYYY', 'numeric'),
array('Date must be numeric, in the form DDMMYYYY', 'rangelength', array(4, 4))
)
));
// then we need to register a rule to check all the values
// so we know it is a valid date
$form->registerRule('local_checkdate', 'callback', 'local_checkdate');
$form->addRule('date_purchase', 'Must be a valid date, in for DDMMYYYY', 'local_checkdate');

function local_checkdate ( $date_array ) {

// bool checkdate ( int month, int day, int year )
return checkdate ( $date_array['date_purchase_month'], $date_array['date_purchase_day'], $date_array['date_purchase_year']);
}




Nice.

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options