Score:0

How do I skip calling a custom validation function in a form with AJAX dropdowns?

in flag

I am using headless Drupal with .NET middleware. I have a Drupal 9 form. This form contains two drop-down elements which are filled up depending on a value selected in a third drop-down. I have a custom validation handler just after those AJAX drop-down elements, where I store some data in a .NET database.

This is my code for hook_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');

Whenever these dependent drop-downs get filled up with values, the validation function gets called somehow and incomplete data gets automatically stored in database even without pressing the save button of the form.

How do I avoid this weird issue?

I just want to fill up the drop-down elements using AJAX, then call the validation handler to store data in database.

apaderno avatar
us flag
*Headless Drupal* means Drupal isn't used to show pages nor forms to the users. Your code is altering a form Drupal should not show to users. Also, *headless Drupal* doesn't write to database tables created by other software.
sonfd avatar
in flag
Shouldn't you be writing to table once the form has been successfully submitted? Not during validation?
taggartJ avatar
cn flag
In reality validation is called each time there is an ajax change you should change your custom validation code to check if values exist if do run validation if not dont
Raja Chakraborty avatar
in flag
Hi @apaderno, Please find my clarifications- 1. Headless Drupal means Drupal isn't used to show pages nor forms to the users - The front end of my application contains a menu which navigates users to drupal forms. 2. headless Drupal doesn't write to database tables created by other software - In validation function, I have one api which posts form data to .net database
Raja Chakraborty avatar
in flag
Hi @taggartJ, is the validation function called everytime when an ajax call happens?
apaderno avatar
us flag
If users are exposed to Drupal pages or forms, then it's not headless Drupal. Yes, validation and submission handlers are called also when AJAX is involved; even the form builder method is called after an AJAX event.
taggartJ avatar
cn flag
yep it should do yes
Score:0
cn flag

To prevent the form submission when the form is submitted with AJAX (when you change the state of the form with AJAX), in the validation handler use $form_state->setRebuild();

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.