2011-11-24 18 views
6

我有一個從hook_form實現的窗體,名爲simplequiz_form()我想在下面提交之後訪問它的數據是我寫的代碼,但我似乎無法訪問它數據一旦提交。我究竟做錯了什麼 ?如何訪問drupal中的hook_form_validate()中的表單數據7

function simplequiz_form_validate($form, &$form_state) { 
// here is where we will validate the data and save it in the db. 
$thid = db_insert('simplequiz') 
->fields(array(

'questions' => &$form_state['question'], 
**I can't seem to access the value of a field questions** 

)) 
->execute(); 

return $thid; 
} 

下面是我實現hook_form的()

function simplequiz_form($form, &$form_submit) 
{ 

$form['question'] = array(
'#title' => t('Please input your question'), 
'#type' => 'text_format', 
'#required' => FALSE, 
'#description' => t('Here is where you can enter your questions'),  
); 

$form['submit'] = array(
'#type' => 'submit', 
'#value' => 'Submit', 
); 
return $form; 

} 

如果我使用$ form_state [ '值'] [ '問題']

我得到下面的錯誤:

PDOException:SQLSTATE [21S01]:插入值列表與列表列表不匹配:1136列計數不匹配值計數a第1行:INSERT INTO {simplequiz}(questions)VALUES(:db_insert_placeholder_0_value,:db_insert_placeholder_0_format);在simplequiz_form_submit()(/home/vishal/Dropbox/sites/dev/sites/all/modules/simplequiz/simplequiz.module的第245行)中的Array([:db_insert_placeholder_0_value] => [:db_insert_placeholder_0_format] => filtered_html)。

它的工作使用$ form_state [「值」] [「問題」] [「值」]

回答

9

它使用hook_form_validate,只是爲了進行驗證的最佳實踐,任何超過驗證其他應該做的在hook_form_submit

無論哪種方式他們都功能幾乎相同的方式。

所有表單數據都存儲在$form_state['values']中,因此要訪問$form['questions']值,只需使用$form_state['values']['questions']

+0

它的工作我不得不改變一下,雖然$ form_state ['values'] ['question'] ['value'] –

+1

只是一句話:沒有hook_form_validate()。這被稱爲「表單驗證處理程序」。 – pfrenssen