How to get form data in hook_form_validate () in drupal 7

I have a form implemented from hook_form called simplequiz_form () I want to access my data after submitting below, this is the code I wrote, but I can not access its data after submitting it. What am I doing wrong?

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; } 

Below is my implementation of 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; } 

if i use $ form_state ['values'] ['question']

I get the following error:

PDOException: SQLSTATE [21S01]: the list of insertion values ​​does not match the list of columns: 1136 The number of columns does not match the number of values ​​in row 1: INSERT INTO {simplequiz} (questions) VALUES (: db_insert_placeholder_0_value ,: db_insert_placeholder_0_format); Array ([: db_insert_placeholder_0_value] => [: db_insert_placeholder_0_format] => filter_html) in simplequiz_form_submit () (line 245 from /home/vishal/Dropbox/sites/dev/sites/all/modules/simplequiz/simple.

it worked using $ form_state ['values'] ['question'] ['value']

+6
source share
1 answer

It is best to use hook_form_validate , only for verification purposes, something other than verification should be done in hook_form_submit .

In any case, both of them work almost the same.

All form data is stored in $form_state['values'] , so just use $form_state['values']['questions'] to access the values ​​of $form['questions'] $form_state['values']['questions'] .

+9
source

Source: https://habr.com/ru/post/902277/


All Articles