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: '\Drupal\example\Controller\ResetPasswordController::resetPassword'
_title: 'Reset password'
requirements:
_permission: 'access content'
In my Controller:
namespace Drupal\example\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormState;
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('Drupal\example\Form\ResetPasswordForm', $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 Drupal\example\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
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!
form.html.twig
and the one defined inside of the form $form['#theme']. Those three would be nested inside of each other. The first and the last one need their own theme hook, the middle one is a core template.