0

I am trying to get the value of group_name field once data entered by user before submitting the form. Then only I can access the db, where the entered data is exist in db or not using query. If exists, it shows the data of that id (selected group name id) on the below fields dynamically. Here is my code:

   public function buildForm(array $form, FormStateInterface $form_state) {
     $form['group_name'] = array(
       '#type' => 'textfield',
       '#title' => $this->t('Name of Group (e.g. name of school, organization, business, etc.)'),
       '#validated' => TRUE,
    );

   /******get the entered group name here********/

    $form['gi_additions_changes'] = array(
       '#type' => 'textfield',
       '#title' => $this->t('If any additions or changes need to be made to your group info above, please let us know'),
       '#validated' => TRUE,
    );

    $form['add_group_name'] = array(
       '#type' => 'textfield',
       '#title' => $this->t('Name of Group (e.g. name of school, organization, business, etc.)'),
       '#validated' => TRUE,
       //'#default_value' => ($get_form_data) ? $get_form_data->group_name : ''
    );
  return $form;
 }
1

If I'm not wrong, you want to update the other fields based on group_name entered by user. To achieve this you can use AJAX API provided by Drupal 8 Core.

If you don't know how to use/work with AJAX API, you can have a look at Examples for Developers module which provides working examples for many of the functionality. To see AJAX API Example, you can install "Form API Example" Sub-Module from Examples Module. Feel free to go through Examples Module code for any help you need.

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.