FormatErrorMessage() public method

Formats the error message that is displayed when range validation fails.
public FormatErrorMessage ( string name ) : string
name string The name of the field that caused the validation failure.
return string
Exemplo n.º 1
0
        /// <summary>
        /// Checks to see whether the provided value falls within the range or not.
        /// </summary>
        /// <param name="variable">The variable in context.</param>
        /// <param name="getVariableName">The name of the variable that should be populated in the error context.</param>
        /// <param name="minimum">The minimum value allowed in the range.</param>
        /// <param name="maximum">The maximum value allowed in the range.</param>
        /// <param name="actionContext">Current action context.</param>
        /// <param name="modelState">Current model object.</param>
        /// <returns>The status of the operation.</returns>
        public static bool Range <T>(T variable, Expression <Func <T, T> > getVariableName, T minimum, T maximum, HttpActionContext actionContext, ModelStateDictionary modelState)
        {
            var variableName = ((MemberExpression)getVariableName.Body).Member.Name;
            var attr         = new RangeAttribute(Convert.ToDouble(minimum), Convert.ToDouble(maximum));

            if (!attr.IsValid(variable))
            {
                modelState.AddModelError(attr.ToString(), attr.FormatErrorMessage(variableName));

                AddErrorToResponse(actionContext, modelState);
                return(false);
            }

            return(true);
        }
Exemplo n.º 2
0
        public async Task BindModelAsync_EnforcesTopLevelDataAnnotationsAttribute()
        {
            // Arrange
            var actionContext       = GetControllerContext();
            var mockModelMetadata   = CreateMockModelMetadata();
            var validationAttribute = new RangeAttribute(1, 100);

            mockModelMetadata.Setup(o => o.DisplayName).Returns("My Display Name");
            mockModelMetadata.Setup(o => o.ValidatorMetadata).Returns(new[] {
                validationAttribute
            });

            var validator = new DataAnnotationsModelValidator(
                new ValidationAttributeAdapterProvider(),
                validationAttribute,
                stringLocalizer: null);

            var parameterBinder = CreateParameterBinder(
                mockModelMetadata.Object,
                validator);
            var modelBindingResult = ModelBindingResult.Success(123);

            // Act
            var result = await parameterBinder.BindModelAsync(
                actionContext,
                CreateMockModelBinder(modelBindingResult),
                CreateMockValueProvider(),
                new ParameterDescriptor { Name = "myParam", ParameterType = typeof(Person) },
                mockModelMetadata.Object,
                50); // This value is ignored, because test explicitly set the ModelBindingResult

            // Assert
            Assert.False(actionContext.ModelState.IsValid);
            Assert.Equal("myParam", actionContext.ModelState.Single().Key);
            Assert.Equal(
                validationAttribute.FormatErrorMessage("My Display Name"),
                actionContext.ModelState.Single().Value.Errors.Single().ErrorMessage);
        }
        public static string GetRangeErrorMessage(int min, int max, string field)
        {
            var attr = new RangeAttribute(min, max);

            return(attr.FormatErrorMessage(field));
        }
Exemplo n.º 4
0
 public override string FormatErrorMessage(string name)
 {
     return(m_range.FormatErrorMessage(name));
 }
        public void RangeAttribute_FormatErrorMessage()
        {
            var attrib = new RangeAttribute(-100.3, 10000000.5);

            Assert.AreEqual(String.Format(CultureInfo.CurrentCulture, Resources.DataAnnotationsResources.RangeAttribute_ValidationError, "SOME_NAME", -100.3, 10000000.5), attrib.FormatErrorMessage("SOME_NAME"));
        }