Drupal AJAX caught rendering (parts of) the form rather than just inserting returned HTML
Seriously, Drupal, what the hell?
This code is the callback for an #ajax property on a form element. The HTML returned replaces the HTML provided. That all works. What gets tricky is the form validation function being called and getting the error.
The proper, pretty-printed message is shown via AJAX in addition to the plain text message that we get here. Indeed, the code below, here, blanks out the message we receive entirely to be absolutely sure form_get_error() isn't returning both the plain text and the formatted error div. The formatted error div still prints.
<?php
/**
* Test callback.
*/
function formmsgs_image_style_name($form, $form_state) {
image_style_name_validate($form['name']);
$message = form_get_error($form['name']);
if (!$message) {
$message = "Default message.";
}
$message = '';
$message = '<div id="formmsgs-image-style-name">' . $message . '</div>';
form_clear_error();
return $message;
}
?>
Somehow, somewhere, Drupal is rendering that piece of form element and sticking it inside the formmsgs-image-style-name div.
Drupal, when i tell you to stick in some HTML, i don't want you rendering the form in the background, ok?
Even the form error clearing function doesn't help, which is particularly disturbing.
For completeness, here's the form_alter that makes the AJAX callback happen:
<?php
/**
* Implements hook_form_alter().
*/
function formmsgs_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'image_style_add_form') {
$form['name']['#ajax'] = array(
'callback' => 'formmsgs_image_style_name',
'event' => 'keyup',
'wrapper' => 'formmsgs-image-style-name',
);
$form['name']['#suffix'] = '<div id="formmsgs-image-style-name">Default message.</div>';
}
}
?>
Comments
Post new comment