dsaj20411 2012-07-26 05:13
浏览 110
已采纳

在hook_submit中使用&$ form_data或$ form_state时,日期值的格式会发生变化

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>'
);
  • 写回答

1条回答 默认 最新

  • doukun8670 2012-08-09 01:56
    关注

    The reason is that you are using $form_state['input'] in the AJAX submit callback, and $form_state['values'] in the regular submit callback. The input array always contains the form data before it is processed and validated, while the values array 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:

    function fsa_roster_form_ajax_submit($form, $form_state) {
      $dcc_table = 'dcc_'.$form_state['values']['sid'];
      $insertRoster = db_insert($dcc_table)
      ->fields(array(
        'entered' => $form_state['values']['date_entered'],
        'exited' => $form_state['values']['date_exited'],
      ))
      ->execute();
    }
    

    Also, for what it's worth, your regular submit callback and the AJAX submit callback both accept the same parameters: $form and $form_state. Though you called them by different variable names in the two functions, they are actually (more or less) identical.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器