/// <summary> /// Validate input /// </summary> /// <param name="args">The validation args.</param> /// <returns> /// true if input passed the validation; otherwise false. /// </returns> public override bool Validate(ValidationArgs args) { var input = args.PropertyValue as string; if (!string.IsNullOrEmpty(input)) { MembershipProvider provider = Membership.Providers[this.MembershipProviderName] ?? Membership.Provider; if (provider != null) { MembershipUser user = provider.GetUser(input, false); if (user != null) { bool valid = user.IsApproved; args.AbortValidationPipeline = !valid; return valid; } LogFailedCondition("The user name not found and validation can't be executed!"); return false; } LogFailedCondition("The MembershipProvider not found and validation can't be executed!"); return false; } LogFailedCondition("The user name can't be empty!"); return false; }
/// <summary> /// Initializes a new instance of the <see cref="ValidationArgs"/> class. /// </summary> /// <param name="args">The validations args.</param> public ValidationArgs(ValidationArgs args) { Ensure.IsNotNull(args, "ValidationArgs"); this.PropertyName = args.PropertyName; this.PropertyValue = args.PropertyValue; this.Instance = args.Instance; this.Collection = args.Collection; }
/// <summary> /// Validate input /// </summary> /// <param name="args">The validation args.</param> /// <returns> /// true if input passed the validation; otherwise false. /// </returns> public override bool Validate(ValidationArgs args) { var input = args.PropertyValue as string; if (this.Optional) { /* Info: Empty field is valid */ return input.IsValidZipCodeFiveOptional(); } return input.IsValidZipCodeFive(); }
/// <summary> /// Validate input /// </summary> /// <param name="args">The validation args.</param> /// <returns> /// true if input passed the validation; otherwise false. /// </returns> public override bool Validate(ValidationArgs args) { var input = args.PropertyValue as string; if (this.Optional && string.IsNullOrEmpty(input)) { /* Info: Empty field is valid */ return true; } return input.IsValidUsStateAbbreviation(); }
/// <summary> /// Validate input /// </summary> /// <param name="args">The validation args.</param> /// <returns> /// true if input passed the validation; otherwise false. /// </returns> public override bool Validate(ValidationArgs args) { var input = args.PropertyValue as string; if (this.Optional) { /* INFO: Empty field is valid. If value non empty validate */ return input.IsValidPhoneOptional(); } return input.IsValidPhone(); }
/// <summary> /// Validate input /// </summary> /// <param name="args">The validation args.</param> /// <returns> /// true if input passed the validation; otherwise false. /// </returns> public override bool Validate(ValidationArgs args) { var input = args.PropertyValue as string; if (!string.IsNullOrEmpty(input)) { MembershipProvider provider = Membership.Providers[this.MembershipProviderName] ?? Membership.Provider; var result = !provider.IsUserExist(input).GetValueOrDefault(); return result; } LogFailedCondition("The user name can't be empty and duplicate email pipeline aborted!"); return false; }
/// <summary> /// Validate input /// </summary> /// <param name="args">The validation args.</param> /// <returns> /// true if input passed the validation; otherwise false. /// </returns> public override bool Validate(ValidationArgs args) { var input = args.PropertyValue as string; /* ReSharper disable AssignNullToNotNullAttribute */ Ensure.IsNotNullOrEmpty(input, "value"); /* ReSharper restore AssignNullToNotNullAttribute */ MembershipProvider provider = Membership.Providers[this.MembershipProviderName] ?? Membership.Provider; if (provider != null) { var user = provider.GetUser(input, false); return user != null; } return false; }
/// <summary> /// Validate input /// </summary> /// <param name="args">The validation args.</param> /// <returns> /// true if input passed the validation; otherwise false. /// </returns> public override bool Validate(ValidationArgs args) { var input = args.PropertyValue as string; if (!string.IsNullOrEmpty(input)) { var provider = Membership.Providers[this.MembershipProviderName] ?? Membership.Provider; if (provider != null) { var result = !provider.IsUserEmailExist(input).GetValueOrDefault(); return result; } } LogFailedCondition("The user email can't be empty!"); return false; }
/// <summary> /// Compare two property values as strings. /// </summary> /// <param name="input">The input.</param> /// <param name="comparepropertyname">The compare property name.</param> /// <param name="args">The validation args.</param> /// <param name="caseinsensitive">if set to <c>true</c> [case insensitive].</param> /// <returns> /// The true if strings are equals, otherwise false /// </returns> protected bool CompareValueAsStrings(string input, string comparepropertyname, ValidationArgs args, bool caseinsensitive = true) { #if DEBUG /* Development only: Do with reflection to ensure property exists */ Type type = args.Instance.GetType(); PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.IgnoreCase | System.Reflection.BindingFlags.FlattenHierarchy | BindingFlags.Default); PropertyInfo propertyinfo = (from property in properties where string.Equals(property.Name, comparepropertyname, StringComparison.OrdinalIgnoreCase) select property).FirstOrDefault(); Ensure.IsTrue(propertyinfo != null, string.Concat("The Property '", comparepropertyname, "' not found to compare!")); #endif string valuetocompare = args.Collection[comparepropertyname]; if (caseinsensitive) { return string.Equals(input, valuetocompare, StringComparison.OrdinalIgnoreCase); } return string.Equals(input, valuetocompare); }
/// <summary> /// Validate input /// </summary> /// <param name="args">The validation args.</param> /// <returns> /// true if input passed the validation; otherwise false. /// </returns> public override bool Validate(ValidationArgs args) { var input = args.PropertyValue as string; if (!string.IsNullOrEmpty(input)) { MembershipProvider provider = Membership.Providers[this.MembershipProviderName]; if (provider != null) { int min = provider.MinRequiredPasswordLength; this.MessageParams = new object[] { min }; return input.Length >= min; } } LogFailedCondition("The users password can't be empty!"); return false; }
/// <summary> /// Validate input /// </summary> /// <param name="args">The validation args.</param> /// <returns> /// true if input passed the validation; otherwise false. /// </returns> public override bool Validate(ValidationArgs args) { var input = args.PropertyValue as string; if (!string.IsNullOrEmpty(input)) { MembershipProvider provider = Membership.Providers[this.MembershipProviderName] ?? Membership.Provider; if (provider != null) { int min = provider.MinRequiredNonAlphanumericCharacters; if (min > 0) { byte counter = 0; foreach (char character in input) { if (!char.IsLetterOrDigit(character)) { counter++; if (counter >= min) { return true; } } } this.MessageParams = new object[] { min }; return false; } return true; } } LogFailedCondition("The users password can't be empty!"); return false; }
/// <summary> /// Validate input /// </summary> /// <param name="args">The validation args.</param> /// <returns> /// true if input passed the validation; otherwise false. /// </returns> public override bool Validate(ValidationArgs args) { var input = args.PropertyValue as string; return input.IsValidEmail(); }
/// <summary> /// Validate input /// </summary> /// <param name="args">The validation args.</param> /// <returns> /// true if input passed the validation; otherwise false. /// </returns> public override bool Validate(ValidationArgs args) { var input = args.PropertyValue as string; bool isempty = string.IsNullOrWhiteSpace(input); if (this.Optional && isempty) { return true; } return !isempty && input.Length <= this.MaxLength; }
/// <summary> /// Runs the validators. /// </summary> /// <param name="validationGroup">The validation group.</param> /// <returns>True if valid otherwise false</returns> private bool RunValidators(string validationGroup) { /* Reset errors */ this.errorMessages = new HashSet<IValidationError>(); /* Run some events before validation */ if (this.BeforeValidationStart != null) { this.BeforeValidationStart(this, EventArgs.Empty); } Type type = this.GetType(); var properties = VFormTypeCacheManager.GetOrderedProperties(type); foreach (PropertyValidator validator in properties) { foreach (var attribute in validator.PropertyValidateAttribute(validationGroup)) { object input = validator.Property.GetValue(this, null); /* Init validation args */ var args = new ValidationArgs(validator.Property.Name, input, this, this.Collection); if (this.AfterValidationArgInit != null) { this.AfterValidationArgInit(this, args); } if (!attribute.Validate(args)) { var error = this.GetValidationErrorInstance(); error.Message = attribute.GetValidationErrorMessage(args); error.Property = validator.Property.Name; error.JsId = attribute.ClientSideId ?? validator.Property.Name.ToLowerInvariant(); error.Ordinal = attribute.DisplayOrder; this.AddErrorMessage(error); /* Abort Validation Pipeline */ if (args.AbortValidationPipeline) { return false; } /* Add first property error and jump to next property */ break; } } } this.isvalid = this.ErrorMessages.Count == 0; /* Run some events before validation */ if (this.AfterValidationEnd != null) { this.AfterValidationEnd(this, EventArgs.Empty); } // ReSharper disable PossibleInvalidOperationException return this.isvalid.Value; // ReSharper restore PossibleInvalidOperationException }
/// <summary> /// Tries the resolve error message by id. /// </summary> /// <param name="validateAttribute">The validate attribute.</param> /// <param name="args">The args.</param> /// <returns>The validation message</returns> internal static string TryResolveErrorMessageById(ValidateAttribute validateAttribute, ValidationArgs args) { string message = string.Empty; if (ResolveErrorMessageById != null) { message = ResolveErrorMessageById(validateAttribute, args); } if (string.IsNullOrWhiteSpace(message)) { string key = string.Concat(validateAttribute.MessageId, '|', args.Instance.ContextLanguage); if (!Cache.TryGetValue(key, out message)) { message = args.PropertyName; } } return message; }
// ReSharper restore UnusedAutoPropertyAccessor.Global /// <summary> /// Validate input /// </summary> /// <param name="args">The validation args.</param> /// <returns> /// true if input passed the validation; otherwise false. /// </returns> public override bool Validate(ValidationArgs args) { HttpPostedFile postedfile = HttpContext.Current.Request.Files[this.InputClientSideId]; if (this.Optional && postedfile == null) { return true; } Ensure.IsNotNull(postedfile, "The file not found and validation can't be executed!"); if (postedfile.ContentType == VContentTypes.Jpeg || postedfile.ContentType == VContentTypes.Jpg || postedfile.ContentType == VContentTypes.Gif || postedfile.ContentType == VContentTypes.Png) { using (System.Drawing.Image image = System.Drawing.Image.FromStream(postedfile.InputStream)) { if (this.MaxWidth > 0 && this.MaxWidth < image.Width) { this.MessageId = Guid.Parse(!string.IsNullOrWhiteSpace(this.MessageImageMaxWidthGuid) ? this.MessageImageMaxWidthGuid : "66E654DD-1FA4-481F-A36C-3217CAD5629A"); return false; } if (this.MaxHeight > 0 && this.MaxHeight < image.Height) { this.MessageId = Guid.Parse(!string.IsNullOrWhiteSpace(this.MessageImageMaxHeightGuid) ? this.MessageImageMaxHeightGuid : "A64FFE7B-597E-4427-958D-5C42C669C17E"); return false; } if (this.MaxSize > 0 && this.MaxSize < postedfile.ContentLength) { this.MessageId = Guid.Parse(!string.IsNullOrWhiteSpace(this.MessageImageMaxSizeGuid) ? this.MessageImageMaxSizeGuid : "A2D207F3-386A-49C2-8C28-BE5B7168CE11"); return false; } if (this.MinWidth > 0 && this.MinWidth > image.Width) { this.MessageId = Guid.Parse(!string.IsNullOrWhiteSpace(this.MessageImageMinWidthGuid) ? this.MessageImageMinWidthGuid : "A06FA8-C5EA-494D-8088-CD77694E55BD"); return false; } if (this.MinHeight > 0 && this.MinHeight > image.Height) { this.MessageId = Guid.Parse(!string.IsNullOrWhiteSpace(this.MessageImageMinHeightGuid) ? this.MessageImageMinHeightGuid : "FE7150BE-B134-4606-BDD9-2842525705EC"); return false; } return true; } } return false; }
/// <summary> /// Validate input /// </summary> /// <param name="args">The validation args.</param> /// <returns> /// true if input passed the validation; otherwise false. /// </returns> public override bool Validate(ValidationArgs args) { var input = args.PropertyValue as string; return this.CompareValueAsStrings(input, this.ComparePropertyName, args, caseinsensitive: true); }
/// <summary> /// Validate input /// </summary> /// <param name="args">The validation args.</param> /// <returns> /// true if input passed the validation; otherwise false. /// </returns> public override bool Validate(ValidationArgs args) { var input = args.PropertyValue as Guid?; return input.HasValue; }
/// <summary> /// Validate input /// </summary> /// <param name="args">The validation args.</param> /// <returns> /// true if input passed the validation; otherwise false. /// </returns> public abstract bool Validate(ValidationArgs args);
/// <summary> /// Sets the default error message. /// </summary> /// <param name="args">The event args.</param> /// <returns> /// The validation error message /// </returns> public virtual string GetValidationErrorMessage(ValidationArgs args) { #if DEBUG Ensure.IsTrue(this.MessageId.HasValue, "The item id '{0}' is missing!"); #endif if (this.MessageId.HasValue) { string message = VFormMessageCacheManager.TryResolveErrorMessageById(this, args); if (!string.IsNullOrWhiteSpace(message)) { if (this.MessageParams != null) { return string.Format(message, this.MessageParams.ToArray()); } return message; } } return args.PropertyName; }
/// <summary> /// Validate input /// </summary> /// <param name="args">The validation args.</param> /// <returns> /// true if input passed the validation; otherwise false. /// </returns> public override bool Validate(ValidationArgs args) { var input = args.PropertyValue as string; var username = args.Collection[this.UserNameFieldNameKey]; if (!string.IsNullOrEmpty(input) && !string.IsNullOrEmpty(username)) { MembershipProvider provider = Membership.Providers[this.MembershipProviderName] ?? Membership.Provider; if (provider != null) { MembershipUser user = provider.GetUser(username, false); if (user != null) { // TODO : not implemented return true; } LogFailedCondition("The user name can't be resolved!"); return false; } LogFailedCondition("The MembershipProvider not found and validation can't be executed!"); return false; } LogFailedCondition("The user name or PasswordAnswer can't be empty!"); return false; }
/// <summary> /// Validate input /// </summary> /// <param name="args">The validation args.</param> /// <returns> /// true if input passed the validation; otherwise false. /// </returns> public override bool Validate(ValidationArgs args) { var input = args.PropertyValue as string; return !string.IsNullOrWhiteSpace(input); }
/// <summary> /// Validate input /// </summary> /// <param name="args">The validation args.</param> /// <returns> /// true if input passed the validation; otherwise false. /// </returns> public override bool Validate(ValidationArgs args) { HttpPostedFile photoImage = HttpContext.Current.Request.Files[this.InputClientSideId]; return photoImage != null; }
/// <summary> /// Validate input /// </summary> /// <param name="args">The validation args.</param> /// <returns> /// true if input passed the validation; otherwise false. /// </returns> public override bool Validate(ValidationArgs args) { double input; bool isvalidnumber = double.TryParse(Convert.ToString(args.PropertyValue, CultureInfo.InvariantCulture), NumberStyles.Any, NumberFormatInfo.InvariantInfo, out input); if (this.Optional && !isvalidnumber) { return true; } return isvalidnumber && input >= this.MinValue; }
/// <summary> /// Validate input /// </summary> /// <param name="args">The validation args.</param> /// <returns> /// true if input passed the validation; otherwise false. /// </returns> public override bool Validate(ValidationArgs args) { var input = args.PropertyValue as string; var username = args.Collection[this.UserNameFieldNameKey]; if (!string.IsNullOrEmpty(input) && !string.IsNullOrEmpty(username)) { MembershipProvider provider = Membership.Providers[this.MembershipProviderName] ?? Membership.Provider; if (provider != null) { // is this the call we want to make? bool valid = provider.ValidateUser(username, input); return valid; } LogFailedCondition("The MembershipProvider not found and validation can't be executed!"); return false; } LogFailedCondition("The user name or password can't be empty!"); return false; }
/// <summary> /// Validate input /// </summary> /// <param name="args">The validation args.</param> /// <returns> /// true if input passed the validation; otherwise false. /// </returns> public override bool Validate(ValidationArgs args) { string input = string.Concat(args.PropertyValue); return input.ConvertToBoolean().GetValueOrDefault(); }