Skip to content

Commit

Permalink
Merge pull request #49 from plone/frisi/fix-upload-on-validation-errors
Browse files Browse the repository at this point in the history
bugfix: do not lose file-upload after validation error
  • Loading branch information
jensens authored Jul 5, 2021
2 parents 771235f + a6f5c96 commit d5082e3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions news/46.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix issue where already uploaded images were lost when file validation error occurs (https://github.com/plone/plone.formwidget.namedfile/issues/46) [fRiSi]
3 changes: 2 additions & 1 deletion plone/formwidget/namedfile/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def toWidgetValue(self, value):

def toFieldValue(self, value):
action = self.widget.request.get("%s.action" % self.widget.name, None)
if action == 'nochange':
file_upload_id = self.widget.request.get("%s.file_upload_id" % self.widget.name, None) or 0
if action == 'nochange' and not file_upload_id:
return NOT_CHANGED

if value is None or value == '':
Expand Down
14 changes: 14 additions & 0 deletions plone/formwidget/namedfile/widget.rst
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,20 @@ and the converter will always set the value to z3c.form.interfaces.NOT_CHANGED::
>>> image_converter.toFieldValue(u'') is NOT_CHANGED
True

On validation errors, file uploads are stored in a temporary storage. The id of the temporarily stored file is given
by file_upload_id and action is set to 'nochange'.
The widget returns the temporary file on `extract` as Named[Blob](File|Image) and the dataconverter will simply use it

>>> file_widget.request.form['widget.name.file.action'] = 'nochange'
>>> file_widget.request.form['widget.name.file.file_upload_id'] = '5c6cc90ce82941919daaeb62700e079a'
>>> file_converter.toFieldValue(NamedFile(data=b'testfile', filename=u'test.txt'))
<plone.namedfile.file.NamedFile object at ...>

>>> image_widget.request.form['widget.name.file.action'] = 'nochange'
>>> image_widget.request.form['widget.name.file.file_upload_id'] = '5c6cc90ce82941919daaeb62700e079a'
>>> file_converter.toFieldValue(NamedImage(data=b'testimage', filename=u'test.jpg'))
<plone.namedfile.file.NamedImage object at ...>


The Base64Converter for Bytes fields
------------------------------------
Expand Down

0 comments on commit d5082e3

Please sign in to comment.