Code verification on Web form

Hello everyone,

I’d like to ask for your help with a problem I’m having with a web form I’ve created using the “Company Registration” doctype in Frappe. This form is designed in two pages: the first is for collecting the application information, while the second is reserved for code verification.

Here’s my idea of the logic: once the user has filled in the fields on the first page and clicked “Next”, a verification code should be sent to their e-mail address. Then, on the second page, the user should enter this code in a dedicated field to finalize the verification.

I’ve already written the code for the web form’s .js and .py files, but the logic doesn’t seem to work properly. Do you have any suggestions to help me implement this idea?

Thanks in advance for your help and advice!

companay_registration.js

frappe.ready(function() {
    frappe.web_form.after_load = () => {
            // Attachez une fonction à exécuter après le chargement du formulaire web
    
            // Écoutez l'événement de clic sur le bouton "Next"
    
            var nextButton = frappe.web_form.page.wrapper.find('.btn-next');
    
            nextButton.on('click', function() {
                // Récupérez l'e-mail de l'utilisateur depuis le champ du web form
                var email = frappe.web_form.get_value('email');
    
                // Vérifiez si l'e-mail est valide
                if (email) {
                    // Appelez une méthode serveur pour envoyer le code de vérification par e-mail
                    frappe.call({
                        method: 'send_verification_code',
                        args: {
                            email: email
                        },
                        callback: function(response) {
                            if (response.message === 'success') {
                                // Le code de vérification a été envoyé avec succès
                                frappe.msgprint('Un code de vérification a été envoyé à votre adresse e-mail.');
                            } else {
                                // Une erreur s'est produite lors de l'envoi du code de vérification
                                frappe.msgprint('Une erreur s\'est produite lors de l\'envoi du code de vérification.');
                            }
                        }
                    });
                } else {
                    // Affichez un message si l'e-mail n'est pas valide
                    frappe.msgprint('Veuillez fournir une adresse e-mail valide.');
                }
            });
        }
}

companay_registration.py

  @frappe.whitelist()
  def send_verification_email(self, email, verification_code):
      try:
          # Créer et envoyer l'e-mail de vérification
          subject = _("Code de vérification pour votre inscription")
          message = _("Votre code de vérification est : {0}").format(verification_code)
          frappe.sendmail(recipients=email, subject=subject, message=message)
          return "success"
      except Exception as e:
          frappe.log_error(e)
          return "error"