1

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
       if(craft()->request->isSiteRequest())
       {
           $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
1

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.

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.