Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.Web.Mvc;
- namespace IncassoBeheer.UI.Helpers.Filter
- {
- /// <summary>
- /// Specifies the minimum and maximum length of characters that are allowed in a data field.
- /// </summary>
- [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
- public class MinMaxStringLengthAttribute : StringLengthAttribute, IClientValidatable
- {
- public MinMaxStringLengthAttribute(int maximumLength, string errorMessage = "") : base(maximumLength)
- {
- ErrorMessage = errorMessage;
- }
- public MinMaxStringLengthAttribute(int minimumLength , int maximumLength, string errorMessage="") : base(maximumLength)
- {
- MinimumLength = minimumLength;
- ErrorMessage = errorMessage;
- }
- public override string FormatErrorMessage(string name)
- {
- EnsureLegalLengths();
- return String.IsNullOrWhiteSpace(ErrorMessage)
- ? String.Format("{0} moet minimum {1} en maximum {2} tekens bevatten.", name, MinimumLength, MaximumLength)
- : ErrorMessage;
- }
- private void EnsureLegalLengths()
- {
- if (MaximumLength < 0)
- throw new InvalidOperationException("Maximum lengte kan niet lager zijn dan 0.");
- if (MaximumLength < MinimumLength)
- throw new InvalidOperationException("Minimum lengte kan niet hoger zijn dan de maximum lengte");
- }
- public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
- {
- var adapt = new StringLengthAttributeAdapter(metadata, context, this);
- return adapt.GetClientValidationRules();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment