Skip to content

FileExtensionAttributeAdapter Fails jQuery Validation #61753

Open
@jditch

Description

@jditch

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When using the FileExtensionAttribute on an IFormFile, the data-val-fileextensions-extensions attribute is not constructed per jQuery documentation https://jqueryvalidation.org/extension-method/. The client-side validation always fails because each file extension is prefixed with a period.

// Build the extension list based on how the JQuery Validation's 'extension' method expects it
// https://jqueryvalidation.org/extension-method/
// These lines follow the same approach as the FileExtensionsAttribute.
var normalizedExtensions = Attribute.Extensions.Replace(" ", string.Empty).Replace(".", string.Empty).ToLowerInvariant();
var parsedExtensions = normalizedExtensions.Split(',').Select(e => "." + e);
_formattedExtensions = string.Join(", ", parsedExtensions);
_extensions = string.Join(",", parsedExtensions);

MergeAttribute(context.Attributes, "data-val-fileextensions-extensions", _extensions);

jQuery validation expects the file extensions to be given as comma or pipe-delimited without periods or spaces so they can be matched using RegEx.
https://github.com/jquery-validation/jquery-validation/blob/6cd68f68e395b1c3a2588e6e7d64f561410c2dc0/src/additional/extension.js#L2-L4

The normalized list of comma-delimited extensions should be used for the attribute instead of _extensions.

var normalizedExtensions = Attribute.Extensions.Replace(" ", string.Empty).Replace(".", string.Empty).ToLowerInvariant();

Expected Behavior

No response

Steps To Reproduce

No response

Exceptions (if any)

In jquery-validation 1.13.1 and older, this issue likely went mostly unnoticed due to a bug (This was fixed in jquery-validation 1.14.0)

.NET Version

No response

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templates

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions