Validation Flask Wtforms

Asked

Viewed 24 times

0

I am developing a web project and am having problems in the form validation part.

Just follow my validations:

# REGRAS DE VALIDAÇÃO #

def validate_username(form, username):
    if dao.buscar_por_id(username.data):
        raise ValidationError('Esse username já existe')


def validate_email(form, email):
    if dao.busca_por_email(email.data):
        raise ValidationError('Esse email já foi cadastrado')


class ValidaFormulario(Form):
    username = StringField('username', [
        validators.Length(min=4, max=25,
                          message='Username deve ter entre 4 e 25 caracteress'),
        validators.DataRequired(), validate_username
    ])
    email = StringField('email', [validators.DataRequired(), validate_email])
    senha = PasswordField('senha', [
        validators.Length(min=8, max=32,
                          message='Senha deve ter entre 8 e 32 caracteres'),
        validators.EqualTo('confirma', message='Senha confirmada incorretamente'),
        validators.DataRequired()
    ])
    confirma = PasswordField('confirma', [validators.DataRequired()])

My html file:

<!DOCTYPE html>

{% from "_formhelpers.html" import render_field %}

<html lang="en" dir="ltr">
<head>
    <meta charset="UTF-8">
    <title>Cadastro</title>
    <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
    <link rel="icon" href="{{ url_for('static', filename='morgan_icon.png') }}">
</head>
<body>
    <form class="box" action="{{ url_for('criar') }}" method="post">
        <h1>Cadastro</h1>
        <br>
        <div class="social.media">
            <ul class="list-social-media">
                <a href="https://pt-br.facebook.com/login/"><i class="fab fa-facebook-f"></i></a>
                <a href="https://accounts.google.com/ServiceLogin/signinchooser?service=accountsettings&continue=https%3A%2F%2Fmyaccount.google.com%3Futm_source%3Daccount-marketing-page%26utm_medium%3Dgo-to-account-button&flowName=GlifWebSignIn&flowEntry=ServiceLogin"><i class="fab fa-google-plus-g"></i></a>
                <a href="https://www.instagram.com/?hl=pt-br"><i class="fab fa-instagram"></i></a>
             </ul>
        </div>
        <br>
        <p class="description-primary">Coloque seus dados pessoais</p>

        {{ render_field(form.username, 'Username', autofocus=true) }}
        {{ render_field(form.email, 'Email', type='email') }}
        {{ render_field(form.senha, 'Senha') }}
        {{ render_field(form.confirma, 'Confirmar Senha') }}

        <!--
        <input class="inputField" required type="text" name="username" placeholder="Username">
        <input class="inputField" required type="email" name="email" placeholder="Email">
        <input class="inputField" required type="password" name="senha" placeholder="Senha">
        <input class="inputField" required type="password" name="confirma" placeholder="Confirmar senha">
        -->

        <input type="submit" value="Criar conta">
    </form>

</body>
</html>

If I put the data correctly, they are registered, but when I error something, I get this error:

TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.
  • Wtfoms can automatically scan a view based on a model. But you have to configure this: putting the model code, and the snippets where you associate the model to the WTF to be able to function these validators is important to make it easier to answer this question. Without this, some very specialized WTF user may even know the answer, but there are far fewer people than everyone who understands Python on the site, who can help in the case of a more complete example.

  • The ideal would be to be able to create a "minimum reproducible example": an HTML of few lines and the entire setup of your project in minimal order to reproduce the error. That is: if any one could come here, copy 3 or 4 minimum example arches to the local machine, and with that manage to install and run the project. I understand that this may be complicated - but some more information about your project is needed anyway.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.