简体   繁体   中英

Sanic Form Validation Failing Silently

Hey guys I am using Sanic with WTForms and Sanic-WTF. I have this code for my SignUpForm:

class SignUpForm(SanicForm):
    email = EmailField('Email', validators=[DataRequired(), Email()])
    password = PasswordField('Password', validators=[DataRequired(), 
    Length(min=3, max=25), EqualTo('confirm_password', message='Passwords must match.')])
    confirm_password = PasswordField('Confirm Password', validators=[DataRequired()])

I also have this as my validation for my validation in a Class Based View.

async def post(self, request):
    form = SignUpForm(request)
    email = form.email.data
    print(email)
    email_exists = await unique_db(email)
    print(form.errors)
    print ('Form validated: {}, Email Exists: {}'.format(form.validate_on_submit(), email_exists))
    if form.validate_on_submit() and email_exists == False:
        print('Validated')
        await app.db.user_details.update_one({'user':'details'},{'$set': data}, upsert=True)
        return json({'success':True})
    return template('signup.html', form=form)

I have no idea what is going on. I have also tried doing form.validate() My SignUp Form is

<form name="SignupForm" style="padding-top: 50px;" method="post">
  {{ form.csrf_token }}
  <div class="row justify-content-center">
    <div class="col-lg-8 col-md-10 col-sm-12">
      <div class="card">
        <div class="card-body">
          <h3 class="card-title">Sign up</h3>
            {{ macros.render_field(form.email, label_visible=true, placeholder='Email', type='email') }}
            <small id="passwordHelpBlock" class="form-text text-muted">Your email must be in the form name@example.com</small>
            {{ macros.render_field(form.password, label_visible=true, placeholder='Password', type='password') }}
            {{ macros.render_field(form.confirm_password, label_visible=true, placeholder='Confirm Password', type='password') }}
            <button type="submit" class="btn btn-success" id="login-submit-button">Submit</button>
        </div>
        <div class="card-footer text-muted text-center">
          Already have an account? <a href="/login">Log in</a>
        </div>
      </div>
    </div>
  </div>
</form>

好的,所以它们似乎是SanicForm中的错误,因为当我将SignUpForm类更改为从Form(通用WTForm表单类)继承时,它可以正常工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM