/// <summary>When implemented in a derived class, returns metadata for client validation.</summary> /// /// <returns>The metadata for client validation.</returns> public override IEnumerable<ModelClientValidationRule> GetClientValidationRules() { if (!ShouldGenerateClientSideRules()) yield break; var formatter = new MessageFormatter().AppendPropertyName(Rule.GetDisplayName()); var message = formatter.BuildMessage(Validator.ErrorMessageSource.GetString()); yield return new ModelClientValidationRequiredRule(message); }
/// <summary>When implemented in a derived class, returns metadata for client validation.</summary> /// /// <returns>The metadata for client validation.</returns> public override IEnumerable<ModelClientValidationRule> GetClientValidationRules() { if (!ShouldGenerateClientSideRules()) yield break; var formatter = new MessageFormatter().AppendPropertyName(Rule.GetDisplayName()); string message = formatter.BuildMessage(EmailValidator.ErrorMessageSource.GetString()); yield return new ModelClientValidationRule { ValidationType = "email", ErrorMessage = message }; }
/// <summary>When implemented in a derived class, returns metadata for client validation.</summary> /// /// <returns>The metadata for client validation.</returns> public override IEnumerable<ModelClientValidationRule> GetClientValidationRules() { if (!ShouldGenerateClientSideRules()) yield break; var propertyToCompare = EqualValidator.MemberToCompare as PropertyInfo; if(propertyToCompare != null) { // If propertyToCompare is not null then we're comparing to another property. // If propertyToCompare is null then we're either comparing against a literal value, a field or a method call. // We only care about property comparisons in this case. var formatter = new MessageFormatter() .AppendPropertyName(Rule.GetDisplayName()) .AppendArgument("PropertyValue", propertyToCompare.Name); string message = formatter.BuildMessage(EqualValidator.ErrorMessageSource.GetString()); yield return new ModelClientValidationEqualToRule(message, CompareAttribute.FormatPropertyForClientValidation(propertyToCompare.Name)) ; } }
/// <summary>When implemented in a derived class, returns metadata for client validation.</summary> /// /// <returns>The metadata for client validation.</returns> public override IEnumerable<ModelClientValidationRule> GetClientValidationRules() { if(!ShouldGenerateClientSideRules()) yield break; var formatter = new MessageFormatter() .AppendPropertyName(Rule.GetDisplayName()) .AppendArgument("MinLength", LengthValidator.Min) .AppendArgument("MaxLength", LengthValidator.Max); string message = LengthValidator.ErrorMessageSource.GetString(); if(LengthValidator.ErrorMessageSource.ResourceType == typeof(Messages)) { // If we're using the default resources then the mesage for length errors will have two parts, eg: // '{PropertyName}' must be between {MinLength} and {MaxLength} characters. You entered {TotalLength} characters. // We can't include the "TotalLength" part of the message because this information isn't available at the time the message is constructed. // Instead, we'll just strip this off by finding the index of the period that separates the two parts of the message. message = message.Substring(0, message.IndexOf(".") + 1); } message = formatter.BuildMessage(message); yield return new ModelClientValidationStringLengthRule(message, LengthValidator.Min, LengthValidator.Max) ; }