User login

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

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.