I'm trying to figure out how to rebuild a form without using a button click trigger. Instead, I'm looking to rebuild whenever a blur event occurs on a textfield. All non-button form types lack the #submit value. It doesn't seem possible to trigger a form rebuild with $form_state['rebuild'] = TRUE;
unless the submit callback is triggered. Demo code below.
function test_form($form, &$form_state) {
$form['form_wrapper'] = array(
'#prefix' => '<div id="form_wrapper">',
'#suffix' => '</div>',
);
if (isset($form_state['textfield_vale'])) {
$textfield_vale = $form_state['textfield_vale'];
}
else {
$textfield_vale = 'not set';
}
//will not trigger page rebuild
$form['form_wrapper']['test_textfield'] = array(
'#type' => 'textfield',
'#value' => 'Value = '.$textfield_vale,
'#ajax' => array(
'callback' => 'test_result',
'wrapper' => 'form_wrapper',
'event' => 'blur',
),
);
//will trigger page rebuild, changing the textfield value
$form['form_wrapper']['trigger_rebuild'] = array(
'#type' => 'submit',
'#value' => 'I don\'t want to press it',
'#submit' => array('submit_rebuild'), //has #submit value unique to buttons
'#ajax' => array(
'callback' => 'test_result_with_button',
'wrapper' => 'form_wrapper',
),
);
return $form;
}
function test_result($form, &$form_state) { //unable to set $form_state['rebuild'] = TRUE
$form_state['textfield_vale'] = 'textfield trigger'; //TRUE;
$form_state['rebuild'] = TRUE;
return $form['form_wrapper'];
}
function submit_rebuild($form, &$form_state) {
$form_state['textfield_vale'] = 'button trigger'; //TRUE;
$form_state['rebuild'] = TRUE;
}
function test_result_with_button($form, $form_state) {
return $form['form_wrapper'];
}
Is there a way to trigger a form rebuild without clicking a button (perhaps with an ajax command)? Alternatively, how can I change my code to allow an ajax callback to change $form_state values?