Score:0

setError or setRebuilld methods breaks validation form

vn flag
<?php

namespace Drupal\my_custom_module\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Class DefaultForm.
 */
class DefaultForm extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'default_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['test'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Test'),
      '#maxlength' => 64,
      '#size' => 64,
      '#weight' => '0',
    ];
    $form['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Submit'),
    ];

    $form_state->setErrorByName('test', 'error');
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $form_state->setErrorByName('test', 'error');
    parent::validateForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $form_state->setRebuild(TRUE);
  }

}

When using setError, setErrorByName or setRebuild methods, wether it be in validateForm or submitForm, it breaks my form by giving me a Allowed memory size of XXX bytes exhausted error when I submit the form.

Problem is, I don't see anything in the logs except thousands of "404" errors, as if I've something that trigger an infinite loop.

In the example, I have both setErrorByName and setRebuild, but it happens even with only one of them.

Can you think of anything that can trigger this error ?

cn flag
If this is the same as your previous question (where I think you suspected it was a server issue?), you'll probably need to provide more details to get any help. These same methods are used in many, many websites without issue, and trying to guess what might be leaking memory outside of the code you've shown is difficult to say the least. Have you let a proper debugger/profiler loose on it yet? Do you know where the leak is happening?
Corentin Le Fur avatar
vn flag
I don't actually. I just checked that the bug doesn't appears on a similar project (same Drupal version, same Docker server). I'll test to see what's the differences between these two projects, and come back.
Score:0
vn flag

Ok, I just found out what was triggering the bug.

Turns out that it was from an other custom module, where one of my Controller was returning a 404 exception. I still don't know how or why, but when (and only when) submitting a form, the 404 exception was triggered.

Sorry to have made you waste your time if you have tried to understand this issue.

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.