Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
By XandieL on
As the title says
How can I re-attach drupal behavior to the form that was loaded via ajax, i've tried different approach on the forum.
Here's how i call my ajax menu
$.ajax({
type: 'GET',
url: baseUrl + '/' + lang + 'blocks/remote/my_form',
success: function (data) {
$('.myclass').html(data.form);
// console.log(data[0]['settings']);
//Drupal.attachBehaviors(document, Drupal.settings);
Drupal.attachBehaviors();
}
});
and here's my php code
$form = drupal_get_form($formid);
$output = render($form);
// Generate the settings
$settings = false;
$javascript = drupal_add_js(NULL, NULL);
if (isset($javascript['settings'], $javascript['settings']['data'])) {
$merge = drupal_json_encode(call_user_func_array(
'array_merge_recursive',
$javascript['settings']['data']
));
$settings = "
<script type='text/javascript'>
jQuery.extend(Drupal.settings, $merge);
</script>"
;
}
$data['form'] = $output . $settings;
drupal_json_output($data);
drupal_exit();
The form is submitting correctly FYI, but the clientside validation is not kicking in.
Im stuck.
Comments
You will either need to
You will either need to attach the JS validation script on page load, or dynamically before you insert the form into the page.
yeah, but how to re attach
yeah, but how to re attach them?
via drupal attachbevior?
I like the universe, but she messes with my words
I'm not talking planets or galaxies and the distance just makes it worse.
I know what you're thinking, this probably sounds rehearsed.
Either send it to the browser
Either send it to the browser on page load, or dynamically insert a script tag into the page from your Ajax success function.