bootstrap-validator icon indicating copy to clipboard operation
bootstrap-validator copied to clipboard

Send Two Fields to AJAX backend.

Open tsawyer opened this issue 7 years ago • 2 comments

I'm building a reset password form with username and email address. For the email address I want a custom validation to find the user name in the database and check the form's email against the database's email. Where I have trouble is passing two fields to my backend.

The php backend only sees formData[Email]. I also want to send formData[Username] to the backend. Is there a way?

<div class="form-group">
    <label for="email" class="control-label">Email</label>
    <input type="email" class="form-control" id="email" placeholder="[email protected]" name="formData[Email]" data-remote="forgot-pw-emailcheck.ajax.php" required>
    <div class="help-block with-errors">Enter your email.</div>
</div>

Thanks for your help.

tsawyer avatar Jan 28 '18 01:01 tsawyer

I'm trying to solve it this way. Console is logging the correct response but validator is ignoring the return. Is this not a valid approach? Appears validator thinks all is well as soon as the ajax request posted.

   // callsign is our user ID
   $('form[data-toggle="validator"]').validator({
       custom: {
         emailmatch: function ($el) {
            var email = $el.val()
            var callsign = $('#callsign').val()
            $.ajax({
               method: "POST",
               url: "forgot-pw-emailcheck.ajax.php",
               cache: false,
               data: { callsign: callsign, email: email }
            })
            .done( function( result ) {
                // this won't work even here:  return 'Email does not match for this callsign.'
               console.log('result: ' + result)
               if ( result == 'bad') {
                  return 'Email does not match for this callsign.'
               }
            });
         }
       }
     });

tsawyer avatar Jan 28 '18 22:01 tsawyer

Validator is ignoring it because you have an asynchronous call and the validation function returns undefined before your response comes out. You should use data-remote, documentation is not so clear about this though

jonatassales avatar Mar 20 '18 12:03 jonatassales