1import { handleLoginCode } from './helpers.js'; 2 3export function getValidations(form) { 4 const validations = []; 5 FormValidation.validators.emailValidator = emailValidator; 6 // Init form validation rules. For more info check the FormValidation plugin's official documentation:https://formvalidation.io/ 7 8 // Build step 1 fields 9 const step1Fields = { 10 email: { 11 validators: { 12 notEmpty: { 13 message: 'Email is required', 14 }, 15 emailAddress: { 16 message: 'The value is not a valid email address', 17 }, 18 emailValidator: { 19 message: 'Test', 20 }, 21 }, 22 }, 23 }; 24 25 // Add name/phone validation when require_account_creation fields are present 26 if (form.querySelector('input[name="first_name"]')) { 27 step1Fields.first_name = { 28 validators: { 29 notEmpty: { 30 message: 'First name is required', 31 }, 32 }, 33 }; 34 } 35 if (form.querySelector('input[name="last_name"]')) { 36 step1Fields.last_name = { 37 validators: { 38 notEmpty: { 39 message: 'Last name is required', 40 }, 41 }, 42 }; 43 } 44 if (form.querySelector('input[name="phone"]')) { 45 step1Fields.phone = { 46 validators: { 47 notEmpty: { 48 message: 'Phone number is required', 49 }, 50 }, 51 }; 52 } 53 54 // Step 1 55 validations.push( 56 FormValidation.formValidation(form, { 57 fields: step1Fields, 58 plugins: { 59 // Bootstrap Framework Integration 60 bootstrap: new FormValidation.plugins.Bootstrap({ 61 //eleInvalidClass: '', 62 eleValidClass: '', 63 }), 64 }, 65 }), 66 ); 67 68 // Step 2 69 validations.push( 70 FormValidation.formValidation(form, { 71 fields: { 72 recipient_name: { 73 validators: { 74 notEmpty: { 75 message: 'Recipient Name is required', 76 }, 77 }, 78 }, 79 }, 80 plugins: { 81 trigger: new FormValidation.plugins.Trigger(), 82 // Bootstrap Framework Integration 83 bootstrap: new FormValidation.plugins.Bootstrap({ 84 //eleInvalidClass: '', 85 eleValidClass: '', 86 }), 87 }, 88 }), 89 ); 90 91 function emailValidator() { 92 return { 93 validate: async function (input) { 94 const email = input.value; 95 96 // Don't run custom validation if email is empty 97 // Let the notEmpty validator handle that 98 if (!email || !email.trim()) { 99 return { 100 valid: true, // Return valid to skip this validator 101 message: '', // No message since notEmpty handles it 102 }; 103 } 104 105 try { 106 const result = await handleLoginCode(email); 107 108 // Defensive checks for the result 109 if (!result || typeof result !== 'object') { 110 console.error('Invalid response from handleLoginCode'); 111 return { 112 valid: false, 113 message: 'System error. Please refresh and try again.', 114 }; 115 } 116 117 const { nextAction, serverMessage, validated } = result; 118 119 // If there was an error, don't allow progression 120 if (nextAction === 'error') { 121 return { 122 valid: false, 123 message: serverMessage || 'An error occurred. Please try again.', 124 }; 125 } 126 127 // Only allow progression for specific valid states 128 let valid = false;
129 let message = 'You must login with your 6 digit code before continuing.'; 130 131 // Only these states allow progression: 132 if (nextAction === 'continue') { 133 // User doesn't need a code (new email) 134 valid = true; 135 message = ''; 136 } else if (validated === true) { 137 // User successfully entered code (shouldn't reach here due to reload) 138 valid = true; 139 message = ''; 140 } else if (nextAction === 'verificationRequired') { 141 // User dismissed the modal 142 valid = false; 143 message = serverMessage || 'Email verification is required to continue'; 144 } else if (nextAction === 'sendCode') { 145 // This shouldn't happen but handle it 146 valid = false; 147 message = 'Please complete email verification'; 148 } else if (nextAction === 'denyAccess') { 149 valid = false; 150 message = serverMessage; 151 } else { 152 // Any other state blocks progression 153 valid = false; 154 message = serverMessage || 'Validation required'; 155 } 156 157 return { 158 valid, 159 message, 160 }; 161 } catch (error) { 162 // If ANY error occurs, block progression 163 console.error('Error in emailValidator:', error); 164 return { 165 valid: false, 166 message: 'Validation failed. Please refresh the page and try again.', 167 }; 168 } 169 }, 170 }; 171 } 172 173 return validations; 174}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.