2

I have form defined like this

class DogForm(Form):
    birthdate = DateField(u'Birthdate', validators=[DataRequired(message=u"User correct form D.M.Y")], format='%d.%m.%Y')
    image = FileField(u'Image of a dog', validators=[Optional()])
    submit = SubmitField(u'Save')

template like this

{{ wtf.form_field(dogForm.birthdate) }}
{{ wtf.form_field(dogForm.image) }}
{{ wtf.form_field(form.submit) }}

Upon subbmitting, if birthdate is given in wrong form,

form.validate_on_submit()

stops validating procces and gives back birthday error

view

form = dogForm()
if form.validate_on_submit():
    add_dog(form)

return render_template("dog.html", form=form)

My problem is that file path from the FileField disappears in reloaded form and i can not event set it like other values

form.image.data = "somepath.."
jojkos
  • 121
  • 1
  • 6
  • Please provide the view code. – Patrick Allen Aug 23 '15 at 07:49
  • I have some other forms as well and everything works fine. This behaviour can be seen only at PasswordField, where it makes sense for security and FileField where it doesn't. I was hoping that there is some way to set it manually, other than form.image.data which apparently does something else. – jojkos Aug 23 '15 at 08:01
  • possible duplicate of [How to keep input type=file field value after failed validation in ASP.NET MVC?](http://stackoverflow.com/questions/967916/how-to-keep-input-type-file-field-value-after-failed-validation-in-asp-net-mvc) – dirn Aug 23 '15 at 13:21
  • yes, thank you very much – jojkos Aug 23 '15 at 19:58

1 Answers1

0

Remember to set the enctype of your HTML form to multipart/form-data

<form  method="POST" enctype="multipart/form-data">
    ....
</form>

for more detail, check the following link:

http://pythonhosted.org/Flask-WTF/form.html

Howcanoe Wang
  • 133
  • 1
  • 8