0

I made custom module that implements hook_form_alter that marks the field_text_field_1 textfield as Ajax-enabled. When a key is pressed inside the field_text_field_1, it should change the value in a second textfield: field_text_field_2.

$form['#prefix'] = '<div id="my_form_submit_wrapper">';
$form['#suffix'] = '</div>';


// $form['field_village']['#attributes']['id'] = 'edit-field-cities-wrapper';

$form['field_text_field_1']['widget']['#ajax'] =array(
    "callback" => "ajax_change1",
    'wrapper' => 'my_form_submit_wrapper',
    "event" => "change",
    "effect" => "fade",
);

The callback function:

function ajax_change1(array &$form, FormStateInterface $form_state){
      $form['field_text_field_2']['widget'][0]['value']['#value']='hello';  
      return $form;
}

But it's not changing the value of `field_text_field_2 to "hello".

7
  • prob instead of #value it needs to be #default_value – No Sssweat Nov 22 '19 at 10:16
  • Yea i also used ` $form['field_text_field_2']['widget']['0']['value']['#default_value'] ` but not work ..if above same thing i doing in select field it's work but not on text field. – Roshan Deuba Nov 22 '19 at 10:46
  • Then field_text_field_2 must be wrong machine name. – No Sssweat Nov 22 '19 at 11:04
  • field_text_field_2 is machine name and it's give output value when i used select field machine name . I think for text field any other way to call ajax. – Roshan Deuba Nov 22 '19 at 11:11
  • 2
    Don't change fields in the AJAX callback, the results won't be saved. Always change it in the form build based on a form state variable. – Clive Nov 22 '19 at 11:22
1

You need to declare on what object (or class if static) to call the callback, like

'callback' => [get_called_class(), 'settingsAjax']

or

'#ajax' => [
            'callback' => [get_class($this), 'addMoreAjax'],
            'wrapper' => $wrapper_id,
            'effect' => 'fade',
          ],

or at least

'callback' => '::settingsAjax'

depending on your code.

0

What's wrong on my code i don't know. I have done this:

     $form['field_full_name']['#prefix']="<div id='email-id-validation'>";
     $form['field_full_name']['#suffix']='</div>';

      $form['field_email']['widget']['#ajax'] =array(
      "callback" => "ajax_change",
      'wrapper' => 'email-id-validation',
      "event" => "change",
      "effect" => "fade",
      );

And on callback i have done this code:

  function ajax_change(array &$form, FormStateInterface $form_state){
           $form['field_full_name']['widget'][0]['value']['#value']='hello';    
       return $form;  }

Simply first i want to check if email field focus out than hello value is print on $form['field_full_name'] or not.

0

Don't change fields in the AJAX callback, the results won't be saved. Always change it in the form build based on a form state variable. - Clive

I think this is what he is referring to

$form['#prefix'] = '<div id="my_form_submit_wrapper">';
$form['#suffix'] = '</div>';


// $form['field_village']['#attributes']['id'] = 'edit-field-cities-wrapper';

$form['field_text_field_1']['widget']['#ajax'] =array(
    "callback" => '::ajax_change1',
    'wrapper' => 'my_form_submit_wrapper',
    "event" => "change",
    "effect" => "fade",
);

if (!empty($form_state->get('field_text_field_2')) {
    $form['field_text_field_2']['widget'][0]['value']['#default_value'] = $form_state->get('field_text_field_2');
}

function ajax_change1(array &$form, FormStateInterface $form_state){
    $form_state->set('field_text_field_2', 'hello');
    return $form;
}
1
  • Actually I have a email field and i want to validate that by ajax call. If user already exist than focus out time give message if already exist – Roshan Deuba Nov 22 '19 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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