Custom node form validation causes ajax calls to fail

Created on 29 August 2024, 4 months ago
Updated 5 September 2024, 4 months ago

Problem/Motivation

I am running custom validation on the node creation/edit form for a custom content type (exhibits). The presence of this custom validation causes an error to be thrown whenever ajax is called on the form, as in clicking the "edit" button on a paragraph field, or adding a new paragraph field. This might be an issue with the Paragraph module, but the problem appears to be larger than that. Here are my custom validation functions:

function my_module_form_alter(&$form, FormStateInterface $form_state, $form_id)
{
   if ($form_id == 'node_exhibit_form' || $form_id == 'node_exhibit_edit_form') {
         $form['#validate'][] = 'my_module_node_form_validate';
   }
}

function my_module_node_form_validate($form, FormStateInterface $form_state)
{

   // Only validate if the form is published
   if ($form_state->getValue('status')['value'] == 1) {

      // Check for exhibit date
      if (empty($form_state->getValue('field_exhibit_date')[0]['value'])) {
         $form_state->setErrorByName('field_exhibit_date', t('The date field must be completed before publishing.'));
      }
}

Here is the ajax error as reported by Chrome DevTools:

Drupal.AjaxError {message: '\nAn AJAX HTTP error occurred.\nHTTP Result Code: 20โ€ฆ --\\u003E\\n\\n\\u003C\\/div\\u003E","settings":null}]', name: 'AjaxError', stack: 'Error\n    at http://localhost:59002/core/misc/ajaxโ€ฆlocalhost:59002/core/misc/ajax.js?v=10.3.2:1921:3'}

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

๐Ÿ› Bug report
Status

Active

Version

11.0 ๐Ÿ”ฅ

Component
Ajaxย  โ†’

Last updated about 23 hours ago

Created by

๐Ÿ‡บ๐Ÿ‡ธUnited States joakland

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @joakland
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States joakland
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States joakland

    It turns out it was a dump() call in my custom validation function that was causing the issue. I had removed that line above just for simplicity's sake, but it turns out that was the problem! I will need to access the $form_state object some other way for development purposes.

    For the record, I don't know why dump() causes AJAX to break, but I'll leave that for another time.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States joakland

    Well, I spoke too soon. This is still an issue. It would be really great if there were a way to run the custom validation ONLY when the form is submitted. Otherwise, expand/collapse/edit buttons keep throwing errors.

  • ๐Ÿ‡ณ๐Ÿ‡ฟNew Zealand quietone

    @joakland, Thanks for the report and helping make Drupal better.

    Changes are made on on 11.x (our main development branch) first, and are then back ported as needed according to our policies.

    Also restoring template to keep track of progress and help reviewers and committers.

  • First commit to issue fork.
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia KumudB Ahmedabad

    In Drupal 11.x, I have attempted to reproduce the issue by creating a content type and adding fields such as a paragraph field and a date field (which is not required). I applied the validation function to this setup, but I am currently unable to reproduce the error. It seems that the issue might be related to an incorrect validation function name, which could lead to AJAX errors when collapsing or editing paragraph fields in the content type.

    To reproduce the issue, follow these steps:

    1. Create a Content Type: Add the necessary fields, including a paragraph field and a date field.
    2. Apply the Validation Function: Implement the custom validation function to check the fields as per the requirements.
    3. /**
       * Implements hook_form_alter().
       */
      function customTweaks_form_alter(&$form, FormStateInterface $form_state, $form_id) {
        if ($form_id == 'node_product_form' || $form_id == 'node_product_edit_form') {
           $form['#validate'][] = 'customTweaks_node_form_validate';
        }
      }
      
      /**
       * Custom validation handler for the product node forms.
       */
      function customTweaks_node_form_validate(&$form, FormStateInterface $form_state) {
          // Only validate if the form is being submitted with published status.
          if ($form_state->getValue('status')['value'] == 1) {
            // Check for the product publish date field.
            if (empty($form_state->getValue('field_prod_publish_date')[0]['value'])) {
              $form_state->setErrorByName('field_prod_publish_date', t('The publish date field must be completed before publishing.'));
            }
          }
        }
    4. Test the Form: Attempt to collapse or edit the paragraph field in the content type and observe if AJAX errors occur. in this code it does not occur.
    5. If the validation function is incorrect or not properly implemented, it may lead to errors during these actions.
Production build 0.71.5 2024