Custom form validation breaks expand/add capability on paragraphs (ajax error)

Created on 30 August 2024, 8 months ago
Updated 4 September 2024, 8 months ago

Problem/Motivation

I have a custom module that runs validation on the node add/edit forms for a custom content type. This content type contains a paragraph field, which itself contains multiple nested paragraph fields. The custom validation runs when a user tries to expand an existing paragraph, or add a new paragraph item. And once this has occurred, attempting to add a new paragraph field throws an ajax error. This latter item happens whether or not a paragraph field is being validated. 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'}

I'm able to tell whether this is a paragraphs issue or a Drupal core issue.

🐛 Bug report
Status

Active

Version

1.18

Component

Code

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

    I now have a solution for this. It won't surprise anyone who has dealt with this or similar issues that the values of a collapsed paragraph field are not available for validation. Long story short, a custom validation function that is looking for a paragraph field that is in a collapsed state will throw an error unless a conditional is applied. And once that error is thrown, no paragraph-related ajax calls will work. In my case, the name of the paragraph field is "Exhibit Chapter." Here is my custom validation function, which now works. _clean_exhibit_array() is a custom function that removes array items that are not actual content.

    function my_module_node_form_validate($form, FormStateInterface $form_state)
    {
         // Chapters validation! First let's remove items that don't have an integer as a key
          $chapters_array = $form_state->getValue('field_exhibit_chapters');
          $chapters = _clean_exhibit_array($chapters_array);
    
    // Now let's walk through the chapters!
          foreach ($chapters as $delta => $chapter) {
             if (array_key_exists('subform', $chapter)) { // This is what prevents the ajax error
                // Check for chapter title
                if (empty($chapter['subform']['field_exhibit_sect_title'][0]['value'])) {
                   $form_state->setErrorByName("field_exhibit_chapters][$delta][subform][field_exhibit_sect_title][0][value", t
                   ('The title field in exhibit chapter @delta must be completed before publishing.', ['@delta' =>
                        $delta + 1]));
                }
             }
          }
    }
    

    This function validates many more fields and subfields than are listed here, but this demonstrates the solution to my original problem. A longer term solution would be for validation to (somehow) automatically work on collapsed fields, but that one is a little above my pay grade at the moment.

Production build 0.71.5 2024