Adds validation for file names
This commit is contained in:
@@ -1,16 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Umbraco.Core;
|
||||
|
||||
namespace Umbraco.Web.Models.ContentEditing
|
||||
{
|
||||
[DataContract(Name = "scriptFile", Namespace = "")]
|
||||
public class CodeFileDisplay : INotificationModel
|
||||
public class CodeFileDisplay : INotificationModel, IValidatableObject
|
||||
{
|
||||
public CodeFileDisplay()
|
||||
{
|
||||
Notifications = new List<Notification>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// VirtualPath is the path to the file on disk
|
||||
/// /views/partials/file.cshtml
|
||||
@@ -47,5 +54,27 @@ namespace Umbraco.Web.Models.ContentEditing
|
||||
public string Id { get; set; }
|
||||
|
||||
public List<Notification> Notifications { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Some custom validation is required for valid file names
|
||||
/// </summary>
|
||||
/// <param name="validationContext"></param>
|
||||
/// <returns></returns>
|
||||
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
|
||||
{
|
||||
var illegalChars = System.IO.Path.GetInvalidFileNameChars();
|
||||
if (Name.ContainsAny(illegalChars))
|
||||
{
|
||||
yield return new ValidationResult(
|
||||
"The file name cannot contain illegal characters",
|
||||
new[] { "Name" });
|
||||
}
|
||||
else if (System.IO.Path.GetFileNameWithoutExtension(Name).IsNullOrWhiteSpace())
|
||||
{
|
||||
yield return new ValidationResult(
|
||||
"The file name cannot be empty",
|
||||
new[] { "Name" });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user