-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v1.1.0: Added UmbracoIFormFileExtensions and UmbracoMaxFileSize attri…
…butes, as suggested in #1
- Loading branch information
Showing
5 changed files
with
212 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 50 additions & 1 deletion
51
src/App_Plugins/Our.Umbraco.ValidationAttributes/Scripts/jquery.validation.custom.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,51 @@ | ||
// Requires jquery, jquery-validation and jquery-validation-unobtrusive to work. | ||
$.validator.unobtrusive.adapters.addBool("mustbetrue", "required"); | ||
|
||
// Validator for UmbracoMustBeTrueAttribute | ||
$.validator.unobtrusive.adapters.addBool("mustbetrue", "required"); | ||
|
||
// Validators for UmbracoMaxFileSizeAttribute | ||
$.validator.addMethod("maxfilesize", function (value, element, param) { | ||
if (value === "") { | ||
return true; | ||
} | ||
var maxBytes = parseInt(param); | ||
if (element.files !== undefined && element.files[0] !== undefined && element.files[0].size !== undefined) { | ||
var filesize = parseInt(element.files[0].size); | ||
return filesize <= maxBytes; | ||
} | ||
return true; | ||
}); | ||
$.validator.unobtrusive.adapters.add('maxfilesize', ['size'], function (options) { | ||
options.rules['maxfilesize'] = options.params.size; | ||
if (options.message) { | ||
options.messages['maxfilesize'] = options.message; | ||
} | ||
}); | ||
|
||
// Validators for UmbracoIFormFileExtensionsAttribute | ||
$.validator.addMethod("filetypes", function(value, element, param) { | ||
if (value === "") { | ||
return true; | ||
} | ||
var validFileTypes = []; | ||
if (param.indexOf(',') > -1) { | ||
validFileTypes = param.split(','); | ||
} else { | ||
validFileTypes = [param]; | ||
} | ||
var currentFileType = value.split('.')[value.split('.').length - 1]; | ||
for (var i = 0; i < validFileTypes.length; i++) | ||
{ | ||
if (validFileTypes[i] === currentFileType) | ||
{ | ||
return true; | ||
} | ||
} | ||
return false; | ||
}); | ||
$.validator.unobtrusive.adapters.add('filetypes', ['types'], function(options) { | ||
options.rules['filetypes'] = options.params.types; | ||
if (options.message) { | ||
options.messages['filetypes'] = options.message; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Linq; | ||
using J2N.Collections.Generic; | ||
using Lucene.Net.Analysis.Hunspell; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation; | ||
using Our.Umbraco.ValidationAttributes.Helpers; | ||
using Our.Umbraco.ValidationAttributes.Interfaces; | ||
using Our.Umbraco.ValidationAttributes.Services; | ||
|
||
namespace Our.Umbraco.ValidationAttributes | ||
{ | ||
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)] | ||
public sealed class UmbracoIFormFileExtensionsAttribute : ValidationAttribute, IClientModelValidator, IUmbracoValidationAttribute | ||
{ | ||
public string DictionaryKey {get; set;} = "FormFileExtensionsError"; | ||
|
||
public string[] ValidFileTypes { get; set; } | ||
|
||
public UmbracoIFormFileExtensionsAttribute(string fileTypes) | ||
{ | ||
ValidFileTypes = ParseFileTypes(fileTypes); | ||
} | ||
|
||
public UmbracoIFormFileExtensionsAttribute(string fileTypes, string dictionaryKey) | ||
{ | ||
DictionaryKey = dictionaryKey; | ||
ValidFileTypes = ParseFileTypes(fileTypes); | ||
} | ||
|
||
public void AddValidation(ClientModelValidationContext context) | ||
{ | ||
ErrorMessage = ValidationAttributesService.DictionaryValue(DictionaryKey); | ||
ErrorMessage = FormatErrorMessage(string.Join(", ", ValidFileTypes)); | ||
AttributeHelper.MergeAttribute(context.Attributes, "data-val", "true"); | ||
AttributeHelper.MergeAttribute(context.Attributes, "data-val-filetypes", ErrorMessage); | ||
AttributeHelper.MergeAttribute(context.Attributes, "data-val-filetypes-types", string.Join(',', ValidFileTypes)); | ||
|
||
// input type="file" accept attribute | ||
List<string> validExtensions = new List<string>(); | ||
foreach (string type in ValidFileTypes) | ||
{ | ||
validExtensions.Add($".{type}"); | ||
} | ||
AttributeHelper.MergeAttribute(context.Attributes, "accept", string.Join(',', validExtensions)); | ||
} | ||
|
||
public override bool IsValid(object value) | ||
{ | ||
IFormFile file = value as IFormFile; | ||
bool isValid = true; | ||
|
||
if (file != null) | ||
{ | ||
isValid = ValidFileTypes.Any(x => file.FileName.EndsWith(x)); | ||
} | ||
|
||
return isValid; | ||
} | ||
|
||
private string[] ParseFileTypes(string fileTypes) | ||
{ | ||
return fileTypes.ToLower().Split(','); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
using System.ComponentModel.DataAnnotations; | ||
using Lucene.Net.Analysis.Hunspell; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation; | ||
using Our.Umbraco.ValidationAttributes.Helpers; | ||
using Our.Umbraco.ValidationAttributes.Interfaces; | ||
using Our.Umbraco.ValidationAttributes.Services; | ||
|
||
namespace Our.Umbraco.ValidationAttributes | ||
{ | ||
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)] | ||
public sealed class UmbracoMaxFileSizeAttribute : ValidationAttribute, IClientModelValidator, IUmbracoValidationAttribute | ||
{ | ||
public string DictionaryKey { get; set; } = "MaxFileSizeError"; | ||
|
||
public int MaxFileSize { get; set; } | ||
|
||
public UmbracoMaxFileSizeAttribute(int maxFileSize) | ||
{ | ||
MaxFileSize = maxFileSize; | ||
} | ||
|
||
public UmbracoMaxFileSizeAttribute(int maxFileSize, string dictionaryKey) | ||
{ | ||
DictionaryKey = dictionaryKey; | ||
MaxFileSize = maxFileSize; | ||
} | ||
|
||
public void AddValidation(ClientModelValidationContext context) | ||
{ | ||
ErrorMessage = ValidationAttributesService.DictionaryValue(DictionaryKey); | ||
ErrorMessage = FormatErrorMessage(GetMaxFileSizeInMB()); | ||
AttributeHelper.MergeAttribute(context.Attributes, "data-val", "true"); | ||
AttributeHelper.MergeAttribute(context.Attributes, "data-val-maxfilesize", ErrorMessage); | ||
AttributeHelper.MergeAttribute(context.Attributes, "data-val-maxfilesize-size", MaxFileSize.ToString()); | ||
} | ||
|
||
public override bool IsValid(object value) | ||
{ | ||
IFormFile file = value as IFormFile; | ||
bool isValid = true; | ||
|
||
if (file != null) | ||
{ | ||
isValid = file.Length <= MaxFileSize; | ||
} | ||
|
||
return isValid; | ||
} | ||
|
||
private string GetMaxFileSizeInMB() | ||
{ | ||
return (MaxFileSize % 1048576M == 0) ? (MaxFileSize / 1048576).ToString() : Math.Round(MaxFileSize / 1048576M, 3).ToString(); | ||
} | ||
} | ||
} |