I’ve recently changed my form to use Ajax to submit the data. My form accepts dates from the user in (mm-dd-yyyy) format, and prior to this change my hook_submit seen below as would convert it to a (yyyy-mm-dd) format on my behalf before it sent it to the DB. After the change, it skips that step and sends the date value to the DB as is.
I don’t mind finding or writing my own function that will do the conversion, but I’d first like to understand the nature of this before I do. Perhaps I’ll learn something that will help me later on.
Any help would be appreciated.
My question is, why is my date value converted with $form_data and not with $form_state?
Using Drupal 7.
Modules: Date API, CTools,
//Original submit handler
//Format sent to DB: yyyy-mm-dd <--This is what I want for the Ajax version
function fsa_roster_form_submit($form_id, &$form_data){
$dcc_table = 'dcc_'.$form_data['values']['sid'];
$insertDaycare = db_insert($dcc_table)
->fields(array(
'entered' => $form_data['values']['date_entered'],
'exited' => $form_data['values']['date_exited'],
))
->execute();
}
//Ajaxed submit handler:
//Format sent to DB: mm-dd-yyyy
function fsa_roster_form_ajax_submit($form, $form_state) {
$dcc_table = 'dcc_'.$form_state['input']['sid'];
$insertRoster = db_insert($dcc_table)
->fields(array(
'entered' => $form_state['input']['date_entered'],
'exited' => $form_state['input']['date_exited'],
))
->execute();
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save Entry'),
'#prefix' => '<div id="modalSubmit">',
'#ajax' => array(//Remove this from your imagination for original submit handler
'callback' => 'fsa_daycare_roster_form_ajax_submit',
'wrapper' => 'msgBox',
'method' => 'replace',
'effect' => 'fade',
),
'#suffix' => '</div><script>addValidationEvents();</script>'
);
The reason is that you are using
$form_state['input']in the AJAX submit callback, and$form_state['values']in the regular submit callback. Theinputarray always contains the form data before it is processed and validated, while thevaluesarray contains the values after processing and validation have been applied.So to achieve the same effect in your AJAX submit callback, use the following, just like you did in your regular submit callback:
Also, for what it’s worth, your regular submit callback and the AJAX submit callback both accept the same parameters:
$formand$form_state. Though you called them by different variable names in the two functions, they are actually (more or less) identical.