0

I have a form class that I set defaults (the users current information) so that it would pre-populate the form. However, this is causing validation issues as in the case of Required() it is accepting the default as the input. However, InputRequired() seems to fix that issue but it is not validating Email() correctly. It seems to accept any input...

How do I pre-populate the form with the users current data but only submit the current values to validation?

#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)
4
  • did you try DataRequired()? Commented Dec 18, 2015 at 18:53
  • Not really but because the docs say specifically that it looks for post-coercion and InputRequired looks for actual input from the form. Commented Dec 18, 2015 at 19:38
  • If the data is already available for the current user, why do you need to submit it through the form? can't you just add it to the database on the backend? Commented Dec 18, 2015 at 19:47
  • Ah, I should have been more clear about the purpose. I'm attempting to allow users to "edit" there user profile. So I need to prepopulate the form with their current data and then validate any changes. I've done this successfully with ajax but I'm trying to do it with just the available utilities. Commented Dec 18, 2015 at 20:21

1 Answer 1

1
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)

The only other thing is that you need to a submit field to the form.

Reference: Flask Web Development by Miguel Grinberg

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, okay so I need to place it in only AFTER the if statement. Makes sense... so with that I should be able to use Required as opposed to the more specific classes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.