My buildForm()
method queries the database and returns data which is used to calculate default values for the form. How can I use these values in submitForm()
without querying the database again?
1 Answer
Typically, the $form_state
object can be used to stash that data. $form_state->getStorage()
references a simple array that you can put whatever you like inside, which you can then retrieve in submitForm()
the same way.
You can store values in buildForm
function as follows
$form_state->set('variable_name', $value);
Inside submitForm
function, you can retrieve this as
$value = $form_state->get('variable_name');
variable_name
can be any name
-
1Specifically, use $form_state->get()/$form_state->set() to set and access these values.– BerdirJan 1, 2016 at 8:11