I am using headless Drupal with .net in middleware. I have a Drupal 9 form. This form contains two dropdowns which is filled up depending on a value selected in a 3rd dropdown. I have a custom validation function just after those ajax dropdowns. In this function, I am storing some data in a table in .net db.
This is my code for form_alter:
$form[‘technology_type’] = [ ‘#type’ => ‘select’, ‘#title’ => t(‘Technology Type’), ‘#options’ => $tech_type_options, ‘#default_value’ => $t_selected_option, ‘#ajax’ => [ ‘callback’ => ‘techlistDropdownCallback’, ‘wrapper’ => ‘techlist-fieldset-container’, ‘event’ => ‘change’, ],
]; if($t_selected_option != ”) { $tech_options = array(0 => ‘- None -‘); $tech_options = custom_authorization_tech_options($t_selected_option); } else { $tech_options = array(0 => ‘- None -‘); }
$form['techlist-select-fieldset-container']= [ '#type' => 'container', '#attributes' => ['id' => 'techlist-fieldset-container'], ]; $form['techlist-select-fieldset-container']['source_tech'] = [ '#type' => 'select', '#title' => t('Source Tech'), '#options' => $tech_options, '#default_value' => !empty($source_tech_value) ? $source_tech_value : $form_state->getValue('source_tech'), '#multiple' => true, ]; $form['techlist-select-fieldset-container']['target_tech'] = [ '#type' => 'select', '#title' => t('Target Tech'), '#options' => $tech_options, '#default_value' => !empty($target_tech_value) ? $target_tech_value : $form_state->getValue('target_tech'), '#multiple' => true, ]; if($t_selected_option == 0) { $form['techlist-select-fieldset-container']['source_tech']['#title'] = t('Source Tech (You must choose tech type first)'); $form['techlist-select-fieldset-container']['source_tech']['#disabled'] = TRUE; $form['techlist-select-fieldset-container']['target_tech']['#title'] = t('Target Tech (You must choose tech type first)'); $form['techlist-select-fieldset-container']['target_tech']['#disabled'] = TRUE; } array_unshift($form['#validate'],'custom_authorization_mak_form_validate');
But whenever these dependent dropdowns get filled up with the values, the validation function gets called somehow and incomplete data gets automatically stored in .net db even without pressing the save button of the form.
How to avoid this weird issue?
I just want to fill up the dropdowns using ajax functionality, then call the validation function to store data in database.