Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make an extra field required in flask-admin?

I know using form_extra_fields can add an extra field in flask-admin. But how can I make it 'required'? Thanks in advance.

form_extra_fields = {
    'password2': PasswordField('Password')
}
like image 607
Conner Mo Avatar asked Jan 15 '16 02:01

Conner Mo


People also ask

What is admin interface in Flask?

The basic concept behind Flask-Admin, is that it lets you build complicated interfaces by grouping individual views together in classes: Each web page you see on the frontend, represents a method on a class that has explicitly been added to the interface.


2 Answers

Thanks a lot, Mech. Actually I figured out a simpler way:

from wtforms import validators
form_extra_fields = {
    'password2': PasswordField('password',[validators.DataRequired()])
}
like image 139
Conner Mo Avatar answered Oct 19 '22 00:10

Conner Mo


You can use WTForms. See example below, pulled from Flask's documentation:

from wtforms import Form, BooleanField, TextField, PasswordField, validators

class RegistrationForm(Form):
    username = TextField('Username', [validators.Length(min=4, max=25)])
    email = TextField('Email Address', [validators.Length(min=6, max=35)])
    password = PasswordField('New Password', [
        validators.Required(),
        validators.EqualTo('confirm', message='Passwords must match')
    ])
    confirm = PasswordField('Repeat Password')
    accept_tos = BooleanField('I accept the TOS', [validators.Required()])

See the link for the other snippets (view, template, etc).

like image 3
mech Avatar answered Oct 18 '22 23:10

mech