I've got a bunch of forms that are opening inside a modal so that the user is not redirected off the page. I've then got a few Ajax calls on form items inside the form. They all work when using the form in a page instead of a modal, however when in a modal it just gets an Ajax error.
The simplest Ajax functionality is the following:
function hook_form_alter(&$form, $form_state, $form_id) {
$form['field_rate']['widget']['#ajax'] = array(
'callback' => 'changeActualRate',
'event' => 'change',
'wrapper' => 'rates-wrapper'
);
}
function changeActualRate(array &$form, FormStateInterface $form_state) {
// Get rate from taxonomy term
$rate = $form_state->getValue('field_rate');
if (!empty($rate)) {
// Instantiate an AjaxResponse Object to return.
$ajax_response = new AjaxResponse();
$rate = taxonomy_term_load($rate[0]['target_id']);
$cost = $rate->get('field_cost')->getValue();
$cost = $cost[0]['value'];
$ajax_response->addCommand(new InvokeCommand('#edit-field-actual-rate-0-value', 'val' , array($cost)));
// Return the AjaxResponse Object.
return $ajax_response;
}
}
It's a pretty simple bit of functionality but it seems no Ajax gets initialised from inside the modal. Anyone have any ideas on how to initialise Ajax once the modal is open. Modals are being opened via links using class="use-ajax" data-dialog-type="modal"
.