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!
The code in the question is mixing three different types of templates:
Custom twig template in a controller
Overriding form.html.twig
Adding a custom form template
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
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP