I am calling external API in form build function. While submitting form gets rebuild and call external API again.
How can I avoid repetitive API calls in the form's build function? I used form_state->set()
but it's doesn't seem to be working.
I am calling external API in form build function. While submitting form gets rebuild and call external API again.
How can I avoid repetitive API calls in the form's build function? I used form_state->set()
but it's doesn't seem to be working.
You can test whether the form is currently rebuilding using the FormStateInterface::isRebuilding
method:
if (!$form_state->isRebuilding()) {
// Contact the API
}
if (empty($form_state->getUserInput())) {}
buildForm
rather than submitForm
?