Flask-WTForms 为渲染模板设置默认值但在验证时忽略
Flask-WTForms Set Default for Render Template but Ignore in Validation
我有一个表单 class,我设置了默认值(用户当前信息),以便它可以预填充表单。但是,这会导致验证问题,因为在 Required() 的情况下,它接受默认值作为输入。然而, InputRequired() 似乎解决了这个问题,但它没有正确验证 Email() 。它似乎接受任何输入...
如何使用用户当前数据预填充表单但只提交当前值进行验证?
#My Form Class
class UserInfoForm(Form):
id = HiddenField('Employee ID')
fullName = TextField('Full Name', validators=[InputRequired()])
position = SelectField('Employee Position', coerce=int)
email = TextField('Email Address', validators=[InputRequired(), Email()])
password = PasswordField(
'New Password',
validators=[EqualTo('confirm', message='Passwords must match.')])
confirm = PasswordField('Repeat Password')
#My View
form = UserInfoForm()
form.position.choices = [(p.id, p.title) for p in EmpPosition.query.order_by('id')]
form.position.default = current_user.position.id
form.fullName.default = current_user.fullName
form.email.default = current_user.email
form.process()
if form.validate_on_submit():
#Submit to DB
return render_template('profile.html', data=data, form=form)
form = UserInfoForm()
if form.validate_on_submit():
current_user.fullName = form.fullname.data
current_user.position = form.position.data
current_user.email = form.email.data
current_user.password = form.password.data
... your db stuff here ...
... and whatever else you want to do afterwards ...
form.fullname.data = current_user.fullName
form.position.data = current_user.position
form.email.data = current_user.email
form.password.data = current_user.position
return render_template('profile.html', data=data, form=form)
唯一的另一件事是您需要向表单提交一个字段。
参考:Miguel Grinberg 的 Flask Web 开发
我有一个表单 class,我设置了默认值(用户当前信息),以便它可以预填充表单。但是,这会导致验证问题,因为在 Required() 的情况下,它接受默认值作为输入。然而, InputRequired() 似乎解决了这个问题,但它没有正确验证 Email() 。它似乎接受任何输入...
如何使用用户当前数据预填充表单但只提交当前值进行验证?
#My Form Class
class UserInfoForm(Form):
id = HiddenField('Employee ID')
fullName = TextField('Full Name', validators=[InputRequired()])
position = SelectField('Employee Position', coerce=int)
email = TextField('Email Address', validators=[InputRequired(), Email()])
password = PasswordField(
'New Password',
validators=[EqualTo('confirm', message='Passwords must match.')])
confirm = PasswordField('Repeat Password')
#My View
form = UserInfoForm()
form.position.choices = [(p.id, p.title) for p in EmpPosition.query.order_by('id')]
form.position.default = current_user.position.id
form.fullName.default = current_user.fullName
form.email.default = current_user.email
form.process()
if form.validate_on_submit():
#Submit to DB
return render_template('profile.html', data=data, form=form)
form = UserInfoForm() if form.validate_on_submit(): current_user.fullName = form.fullname.data current_user.position = form.position.data current_user.email = form.email.data current_user.password = form.password.data ... your db stuff here ... ... and whatever else you want to do afterwards ... form.fullname.data = current_user.fullName form.position.data = current_user.position form.email.data = current_user.email form.password.data = current_user.position return render_template('profile.html', data=data, form=form)
唯一的另一件事是您需要向表单提交一个字段。
参考:Miguel Grinberg 的 Flask Web 开发