Is there a way to send errors back to the set password form?

I am listening to the users.onBeforeSetPassword event in which I want to confirm that the string in a confirm password field is the same as in the newPassword field.

craft()->on('users.onBeforeSetPassword', function(Event $event) {
       // Only do anything if it is a front end submission
           $newPassword = craft()->request->getPost('newPassword');
           $passwordConfirm = craft()->request->getPost('passwordConfirm');
           if(isset($passwordConfirm) && strcmp($newPassword, $passwordConfirm) !== 0)
               $event->params['user']->addErrors(array('newPassword' => Craft::t('Passwords do not match')));
               $event->performAction = false;

This does prevent the user from resetting their password but doesn't send a error message back to the template. How can I pass a variable back to the template so I can show the user their error?

  • This is only possible when you copy and paste the controller action in your custom controller. It takes usually about 2-3 minutes to do this. If you don't want this for any reasons you can include Javascript after setting your performAction and include the html by yourself. You just have to append an unsorted list with the message. – Robin Schambach Oct 24 '17 at 5:50

You could use the craft()->userSession->setFlash('your message here'); functionality.

How it works is explained in this or this answer for example but it boils down to setting the message in your event handler and then checking in your template if there's a message and if there's one, display it.


User version must not be shown and must be impossible

New contributor
Connor is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • Welcome @Connor! Would you mind elaborating on what you mean here? I'm not sure if I'm missing your point or if this isn't a valid answer. – Matt Stein 11 hours ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.