0

Shouldn't the form generating function get executed every time the form is rendered? Because I need to update some field suffixes based on the $form_state values and I don't know how to do this.

I have something like this:

function xxxx_some_form($form, &$form_state){
  $form["some_field"] = array(
    "#type" => "textfield",
    "#required" => true,
    // bla bla bla
  );
  drupal_set_message("whatever");
  $form["actions"]["do_action"] = array(
    "#type" => "submit",
    "#value" => t("Do action"),
  );
  return $form;
}

When I submit the form, the validation messages are displayed ( Eg: X field is required ) but the message from drupal_set_message("whatever") is not displayed.

Also, no other logic is executed there.

1

When a form has a validation error, the form is rebuilt from cache, meaning that the form function isn't invoked.

I could move the drupal_set_message call over to a #process or #preprocess function on the form itself or on one of the methods. These should be called again.

If the form is displayed with hook_menu you could also make a callback function which calls drupal_set_message and then fetches the form. There are a lot of ways to get around this.

  • Thanks, never thought about a #process callback. It seems promising but it looks like the $form_state["values"] is missing all #type => 'value' fields. – Marius Ilie Mar 23 '16 at 15:31
  • @MariusIlie $form_state['values'] is not availble if the form has validation errors. This is by design and you should not use this outside submit handlers. – googletorp Mar 23 '16 at 15:38
  • That's weird. All other fields have values in $form_state, just the fields of type value are missing. I guess I'll have to try something else then... – Marius Ilie Mar 23 '16 at 15:48
0

Are you trying to get the form to be interactive? Have you looked at the #states ability of the form which lets you set up conditional display of other fields, markdown elements, etc?

Field suffixes sounds like a very weird thing to modify on form submission, since the expectation with a submission is that the user is now done with the form. And as for the messages, it is possible the template for the page you are working with does not have the message print lines.

  • Thanks but @googletorp's answer is closer to what I'm trying to achieve. – Marius Ilie Mar 23 '16 at 15:36

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.