Implementing Email Verification in Frappe 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 = () => {
            // Attach a function to execute after the web form loads
    
            // Listen for click event on the "Next" button
    
            var nextButton = frappe.web_form.page.wrapper.find('.btn-next');
    
            nextButton.on('click', function() {
                // Get the user's email from the web form field
                var email = frappe.web_form.get_value('email');
    
                // Check if the email is valid
                if (email) {
                    // Call a server method to send the verification code via email
                    frappe.call({
                        method: 'send_verification_code',
                        args: {
                            email: email
                        },
                        callback: function(response) {
                            if (response.message === 'success') {
                                // Verification code sent successfully
                                frappe.msgprint('A verification code has been sent to your email address.');
                            } else {
                                // Error occurred while sending verification code
                                frappe.msgprint('An error occurred while sending the verification code.');
                            }
                        }
                    });
                } else {
                    // Show a message if email is not valid
                    frappe.msgprint('Please provide a valid email address.');
                }
            });
        }
});

companay_registration.py

@frappe.whitelist()
def send_verification_email(self, email, verification_code):
    try:
        # Create and send the verification email
        subject = _("Verification Code for Your Registration")
        message = _("Your verification code is: {0}").format(verification_code)
        frappe.sendmail(recipients=email, subject=subject, message=message)
        return "success"
    except Exception as e:
        frappe.log_error(e)
        return "error"