I have a form using Entity forms and attaching an AJAX callback to an existing field in hook_FORM_form_id_alter(). So far I have no luck with any ajax callbacks.

<?php

function hook_FORM_form_id_alter(&$form, &$form_state, $form_id) {
 
				. . .

	// An existing field.
	$form['existing_field']['#ajax'] = array(
		'callback' => 'test_callback',
		'wrapper' => 'test-wrapper',
	);

	// My container.
	$form['container'] = array(
		'#prefix' => '<div id="test-wrapper">',
		'#suffix' => '</div>',
	);

				. . .

}

test_callback($form, &$form_state) {
	
	dpm($form_state);

}

I left out a bulk of my code but this is the basic idea. Any suggestions?

Comments

Walmart_Shoes created an issue.

jfurnas’s picture

You need to target a specific delta for the fields in order get it to work. In example, your code should look like this:


function hook_FORM_form_id_alter(&$form, &$form_state, $form_id) {
 
				. . .

	// An existing field.
	$form['existing_field']['und'][0]['#ajax'] = array(
		'callback' => 'test_callback',
		'wrapper' => 'test-wrapper',
	);

	// My container.
	$form['container'] = array(
		'#prefix' => '<div id="test-wrapper">',
		'#suffix' => '</div>',
	);

				. . .

}