AnswerBun.com

submitForm() isn't called

Drupal Answers Asked by dylhof on December 7, 2020

Goal: Have a custom page that shows various elements including links, logos, images and a custom form to help the users reset their passwords (we have a custom reset password interface which sends the user a code so we are over-riding the default reset password interface).

Problem: The submit button is not firing the submitForm function on the reset password form.

Here is my code now:

In my module:

function example_theme($existing, $type, $theme, $path) {
  return array(
    'reset_password' => array(
            'variables' => array(
                'form' => NULL,
                'url'=> '',
                'main_logo_url' => '',
                'foot_logo_url' => '',
            )
        )
  );
}

In my routing:

example.reset_password:
  path: '/reset-password'
  defaults:
    _controller: 'DrupalexampleControllerResetPasswordController::resetPassword'
    _title: 'Reset password'
  requirements:
    _permission: 'access content'

In my Controller:

namespace DrupalexampleController;

use DrupalCoreControllerControllerBase;
use DrupalCoreFormFormBuilderInterface;
use DrupalCoreFormFormState;

class ResetPasswordController extends ControllerBase {
  public function resetPassword() {

    //code here that sets the variables in the return array

    $form_state = new FormState();
    $reset_password_form = Drupal::formBuilder()->getForm('DrupalexampleFormResetPasswordForm', $form_state);

    return array(
      '#theme' => 'reset_password',
      '#form' => $reset_password_form,
      '#url' => $url,
      '#main_logo_url' => $main_logo_url,
      '#foot_logo_url' => $foot_logo_url,
    );
  }
}

in my form:

namespace DrupalexampleForm;

use DrupalCoreFormFormBase;
use DrupalCoreFormFormStateInterface;

class ResetPasswordForm extends FormBase {
  public function getFormId() {
    return 'reset_password';
  }

  public function buildForm (array $form, FormStateInterface $form_state) {
    Drupal::logger('rp-form')->notice('hello from build Form');
    
    $form['#theme'] = 'reset_password';

    $form['step_one_user_exists'] = [
      '#type' => 'textfield',
      '#required' => TRUE,
      '#attributes' => array(
        'placeholder' => t('Enter here')
      )
    ];

    $form['actions'] = ['#type' => 'actions'];

    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Send Code'),
    ];

    return $form;
  }

  public function validateForm (array &$form, FormStateInterface $form_state) {
    Drupal::logger('rp-form')->notice('hello from validate');
  }

  public function submitForm (array &$form, FormStateInterface $form_state) {
   Drupal::logger('rp-form')->notice('hello from submit');
  }
}

and in my twig template:

// I have other elements on the page here
<div>
 {{ form.step_one_user_exists }}
</div>
<div>
 {{ form.actions.submit }}
</div>

As I said above, the problem is that the submitForm function from the form class is not firing when I click the submit button. The form shows up on the page, you can type in the text field, you can click the button, but then nothing happens. I have loggers in the buildForm (which logs), and in validateForm and submitForm (which do not log).

I want to use a controller because there are other variables needed on the page that change based on language and other factors, but am open to other methods of doing this (although I’d prefer to keep most of the logic out of the template).

Thanks!

One Answer

The code in the question is mixing three different types of templates:

  1. Custom twig template in a controller

    https://www.drupal.org/docs/theming-drupal/twig-in-drupal/create-custom-twig-templates-for-custom-module

  2. Overriding form.html.twig

    How to Create twig template files in drupal 8 for customizing user login,profile edit and forget password forms?

  3. Adding a custom form template

    How to render specific form elements from the *.theme file

The confusing part of your question is that you are using the same template reset-pasword.html.twig for 1. and 3.

The issue discussed in the comments about the special form elements applies only to 2.

You can use all three template levels at the same time. Then they are nested inside of each other which you see when you enable twig debugging.

More info Custom registration twig template.

Answered by 4k4 on December 7, 2020

Add your own answers!

Related Questions

” a field on Manage display

1  Asked on February 6, 2021 by carrotandme

   

Display Entity Reference field twice, with different display modes

1  Asked on February 3, 2021 by paul-canning

     

Media html not rendering in blocks

1  Asked on February 3, 2021 by kristoffer-rom

   

How can I programmatically change the view mode?

4  Asked on February 3, 2021 by jmzea

   

How do I avoid API calls are cached?

2  Asked on February 2, 2021 by user1648228

 

Custom JSON api normalizer is not initiated

0  Asked on February 1, 2021 by kevinvhengst

   

Suddenly unable to add a field to a new content type

1  Asked on January 31, 2021 by leif

     

an error when changing the date form

1  Asked on January 30, 2021 by mahdi-alikhasi

 

Display content based on taxonomy with view

2  Asked on January 30, 2021

 

Why is my code called again written on build form?

0  Asked on January 29, 2021 by namit-garg

     

How to change path of taxonomy facet

1  Asked on January 24, 2021 by rahulkumar-it

   

Form field not show on Ajax Callback even the callback is called

1  Asked on January 23, 2021 by emad-karhely

     

Features to import content types

2  Asked on January 18, 2021 by webmaster-pf

   

Redirect user to previous page after registration

2  Asked on January 15, 2021 by lbogdan

     

field_info_field: how to define the content type name?

1  Asked on January 14, 2021 by bruno-von-paris

   

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP