2

In my form building function I need to render some things basing on user's actions. But if the form building function is called for the very first time I want to put a form deliberately in slightly inconsistent state (I have my reasons, long story).

How can I reliably detect if the form building function was called for the first time, or if the form was already presented to the user and now it's built due to AJAX call or to allow user to fix input errors?

Currently I'm using empty($form_state['input']) but I have this feeling there is a better, more reliable way and I forgot it.

5

You can add our own value to $form_state in a hook_form_alter(). If it doesn't already exist (eg., !isset($form_state['my_var'])), then you know it is the first build.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.