GithubHelp home page GithubHelp logo

ziotino / our.umbraco.validationattributes Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 1.0 59 KB

Contains validation attributes to decorate your classes, but using Umbraco dictionary as the resource.

Home Page: https://ziotino.github.io/Our.Umbraco.ValidationAttributes/

License: MIT License

JavaScript 5.37% C# 94.63%
umbraco umbraco-cms umbraco-package umbraco-v9

our.umbraco.validationattributes's People

Contributors

ziotino avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

bielu

our.umbraco.validationattributes's Issues

UmbracoDisplayName doesn't have the same parameter key as the other attributes

Need to uniform the parameters, as in UmbracoRequired and others.

[UmbracoDisplayName(dictionaryKey: "Name")]
[UmbracoRequired(DictionaryKey = "_form_error_name")]
public string Name { get; set; }

should be

[UmbracoDisplayName(DictionaryKey = "Name")]
[UmbracoRequired(DictionaryKey = "_form_error_name")]
public string Name { get; set; }

MaxFileSize and AllowedFileExtensions

Would be great if this included attributes for MaxFileSize and AllowedFileExtensions. Maybe my code for Umbraco v8 helps to get started. Haven't had time to port these to v9 yet.

(UmbracoValidationHelper was just a static class to help fetch dictionary items from cache)

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web;
using System.Web.Mvc;

namespace Application.Core.ValidationAttributes
{
    public class UmbracoMaxFileSize : ValidationAttribute, IClientValidatable
    {
        private readonly string _errorMessageDictionaryKey;
        private readonly int _maxFileSize;

        public UmbracoMaxFileSize(string errorMessageDictionaryKey, int maxFileSize)
        {
            _errorMessageDictionaryKey = errorMessageDictionaryKey;
            _maxFileSize = maxFileSize;
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var file = value as HttpPostedFileBase;
            if (file != null)
            {
                if (file.ContentLength > _maxFileSize)
                {
                    //Get the error message to return
                    var error = UmbracoValidationHelper.FormatErrorMessage(_errorMessageDictionaryKey, GetMaxFileSizeInMB());

                    //Return error
                    return new ValidationResult(error);
                }
            }

            return ValidationResult.Success;
        }

        private string GetMaxFileSizeInMB()
        {
            return (_maxFileSize % 1048576M == 0) ? (_maxFileSize / 1048576).ToString() : Math.Round(_maxFileSize / 1048576M, 3).ToString();
        }

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            ErrorMessage = UmbracoValidationHelper.GetDictionaryItem(_errorMessageDictionaryKey);

            var rule = new ModelClientValidationRule();
            rule.ErrorMessage = FormatErrorMessage(GetMaxFileSizeInMB());
            rule.ValidationType = "filesize";

            rule.ValidationParameters.Add("maxsize", _maxFileSize);

            yield return rule;
        }
    }
}
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Application.Core.ValidationAttributes
{
    public class UmbracoHttpPostedFileExtensions : ValidationAttribute, IClientValidatable
    {
        private readonly string _errorMessageDictionaryKey;
        private readonly string _fileExtensions;
        private readonly string _fileExtensionsDescription;

        public UmbracoHttpPostedFileExtensions(string errorMessageDictionaryKey, string fileExtensions, string fileExtensionsDescription)
        {
            _errorMessageDictionaryKey = errorMessageDictionaryKey;
            _fileExtensions = fileExtensions;
            _fileExtensionsDescription = fileExtensionsDescription;
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var file = value as HttpPostedFileBase;
            if (file != null)
            {
                var fileName = file.FileName;
                var isValidExtension = _fileExtensions.ToLower().Split(',').Any(e => fileName.EndsWith(e));

                if (!isValidExtension)
                {
                    //Get the error message to return
                    var error = UmbracoValidationHelper.FormatErrorMessage(_errorMessageDictionaryKey, _fileExtensionsDescription);

                    //Return error
                    return new ValidationResult(error);
                }
            }

            return ValidationResult.Success;
        }

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            ErrorMessage = UmbracoValidationHelper.GetDictionaryItem(_errorMessageDictionaryKey);

            var error = FormatErrorMessage(_fileExtensionsDescription);
            var rule = new ModelClientValidationRule();
            rule.ErrorMessage = error;
            rule.ValidationType = "fileextensions";

            rule.ValidationParameters.Add("fileextensions", string.Join(",", _fileExtensions));

            yield return rule;
        }
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.