Hello,
In Drupal 7's Form API, when using #AJAX to refresh fields, error messages from validation are not displayed until the entire page is refreshed. I see the field I refreshed highlighted in the error state, but the user does not see the associated message until it's too late (they've reloaded the page, or gone to another page).
Note: I'm using multi-commands with a callback (mandatory in my module to accomplish the desired behavior):
/**
* Form ajax callback
*/
function berp_product_mgr_add_new_item_ajax_callback($form, $form_state) {
$commands[] = ajax_command_replace(NULL, render($form['berp_product_mgr_item_list']));
$commands[] = ajax_command_invoke('#' . $form['berp_product_mgr_item_list']['new_item_code']['#id'], 'focus');
return array('#type' => 'ajax', '#commands' => $commands);
}
To solve my problem I have also tried to add to my commands array the following:
// Remove the old messages div, clearing existing messages.
$commands[] = ajax_command_remove('#messages');
// Append a new messages div with our latest errors and messages.
$commands[] = ajax_command_after('#header-region', '
');
But unfortunately, this is still not displaying the error message when refreshing the form with AJAX.
Any suggestion would be very appreciate to solve my problem!
Thanks in advance!
Benoit
Comments
I have found a way to finally
I have found a way to finally display the message when refreshing with AJAX.
I have replaced the following line in my callback:
$commands[] = ajax_command_after('#header-region', '' . theme('status_messages') . '');
WITH the following line
$commands[] = ajax_command_after(NULL, 'smaller than div id="messages" greater than' . theme('status_messages') . 'smaller than /div greater than');
* where smaller than = <
* where greater than = >
With this modification, the error message are displayed at the bottom of my form instead of at the top.
Any suggestion would be very appreciate to be able to display the messages at the top of my form.
Thanks in advance!
Benoit
This works for me
Adding this to the callback works for me.
$form['#prefix'] .= theme('status_messages');
Display the messages at the top
To display the messages at the top of the form use ajax_command_prepend instead ajax_command_after
AJAX commands and Drupal Messages
Also see https://www.drupal.org/node/1271004 (AJAX commands and Drupal Messages).
-----------------------------------------------
The Future is Open!