validate: Fix FileField content type checks

This commit is contained in:
Tobe O 2020-05-04 17:03:32 -04:00
parent d233b1c37e
commit 8aad408c32
2 changed files with 8 additions and 3 deletions

View file

@ -1,5 +1,6 @@
# 3.0.0-alpha.1
* Add `MapField` and `UriField`.
* Fix how `FileField` handles allowed content types.
# 3.0.0-alpha
* Rewrite, based on `Field` and `Form`, rather than strings.

View file

@ -261,6 +261,8 @@ class FileField extends Field<UploadedFile> {
/// If provided, then the `content-type` must be present in this [Iterable].
final Iterable<MediaType> allowedContentTypes;
Iterable<String> _allowedContentTypeStrings;
FileField(String name,
{String label,
bool isRequired = true,
@ -269,6 +271,8 @@ class FileField extends Field<UploadedFile> {
this.allowedContentTypes})
: super(name, 'file', label: label, isRequired: isRequired) {
assert(allowedContentTypes == null || allowedContentTypes.isNotEmpty);
_allowedContentTypeStrings =
_allowedContentTypeStrings?.map((c) => c.toString());
}
@override
@ -288,12 +292,12 @@ class FileField extends Field<UploadedFile> {
} else if (requireFilename && file.filename == null) {
return FieldReadResult.failure(
['A filename must be given for file "$name".']);
} else if (allowedContentTypes != null &&
!allowedContentTypes.contains(file.contentType)) {
} else if (_allowedContentTypeStrings != null &&
!_allowedContentTypeStrings.contains(file.contentType.toString())) {
return FieldReadResult.failure([
'File "$name" cannot have content type '
'"${file.contentType}". Allowed types: '
'${allowedContentTypes.join(', ')}'
'${_allowedContentTypeStrings.join(', ')}'
]);
} else {
return FieldReadResult.success(file);