#Encountering issues with WTForms validation

5 messages · Page 1 of 1 (latest)

radiant eagle
#

Hello. I am currently trying use WTForms, but I am encountering problems
I am using javascript to send an ajax post request instead of using the default form action for submit

$("#submit").click(function (e) {
  e.preventDefault();
  getLocationData(pushpin.getLocation()).then(function (result) {
    let address = result.resourceSets[0].resources[0].name;
    $.ajax("/", {
      type: "POST",
      data: {
        name: $("#form #name").val(),
        address: address,
        phone: $("#form #phone").val(),
      },
      success: function () {
        $("#form").submit();
      },
    });
  });
});

But now, form.validate_on_submit() returns False
is_submitted() returns True but validate() returns False.

if request.method == "POST":
        form = AddStudentForm()
        if form.validate_on_submit():
            flash('Student added!', 'success')
            # ...
            return redirect(url_for('index'))

#

It was working fine before I implemented the block of code I sent in the previous message in javascript

#

If I just use is_submitted(),,, flash and redirect(url_for..) don't work anymore

#

I can provide more information if needed

daring ocean
#

try to manually call the form.validate() function in your view function after receiving the AJAX request