I have 3 fields. On blur on any of them I want to look for empty ones - if blurred one has value and any of the others does not, I want to copy value.

Obtaining value is easy, simply $form_state['triggering_element']['#value'], but setting it elsewhere gets tricky.

Changing $form['item']['#default_value'] does not work if field is empty after user's edit. Changing $form['item']['#value'] makes Drupal ignore any further user input. Changing $form_state['values'] seems to do nothing. What is the correct approach that should work? When I'll know that, I'll be able to debug.

  • 1
    Have you tried $form_state['input']['item'] = '...'?
    – Clive
    Commented Jun 12, 2013 at 12:51
  • @Clive - right. Case of a hole in memory it seems. Do you think it's better to delete it or format it properly and leave it?
    – Mołot
    Commented Jun 12, 2013 at 12:59
  • Leave it open I'd say. I didn't put that in as an answers as I've never taken the time to find out why that method works and others don't. If you could a bit of example code to the question and answe it yourself with exactly what you did to make it work this could very useful to future visitors
    – Clive
    Commented Jun 12, 2013 at 13:12
  • My code now looks like $form_state['input']["properties"][$property_machine_name]['parameters']['parameters'][$language->idLanguage]['choices'] - it'll take some time to clear it ;) But I guess I should, at evening. Note - 'parameters' are doubled becouse of you, @Clive, and one of your answers ;)
    – Mołot
    Commented Jun 12, 2013 at 13:16
  • 1
    @Clive no nonsense, it's just a quick and apparently dirty implementation of this
    – Mołot
    Commented Jun 12, 2013 at 13:46

2 Answers 2


I had the same issue and this workaround did the trick. Basically you clear up $form_state['input'] value and rebuild the form.

Form builder:

$form['test'] = array(
  '#type' => 'textfield', 
  '#title' => t('Test'),
  '#ajax' => array(
    'callback' => 'my_test_callback_func',
    'wrapper' => 'edit-test2',
    'progress' => array(
      'message' => '',
      'type' => 'throbber',

$form['test2'] = array(
  '#type' => 'textfield', 
  '#prefix' => '<div id="edit-test2">',
  '#suffix' => '</div>', 
  '#title' => t('Test2'),

Ajax callback function: This is the trickier part.

function my_test_callback_func($form, &$form_state) {
  unset($form_state['input']['test2'], $form_state['values']['test2']);
  $form['test2']['#default_value'] = 'Auto Filled!';
  $form_state['input']['test2'] = 'Auto Filled!';
  return form_builder($form['#id'], $form['test2'], $form_state);

We unset the user input (or check if it's empty), set a default value and then rebuild the form.


The solution was, as Clive suggested, to simply use:

$form_state['input']['item'] = '...';

For elements that got replaced by AJAX, works like a charm.

If that's not enough, using AJAX commands might be needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.