Server side validation of int datatype

2019-02-07 12:24发布

问题:

I made custom Validator attribute

partial class DataTypeInt : ValidationAttribute
{
    public DataTypeInt(string resourceName)
    {
        base.ErrorMessageResourceType = typeof(blueddPES.Resources.PES.Resource);
        base.ErrorMessageResourceName = resourceName;
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        string number = value.ToString().Trim();
        int val;
        bool result = int.TryParse(number,out val );
        if (result)
        {
            return ValidationResult.Success;
        }
        else 
        {
            return new ValidationResult("");
        }
    }
}

But when entered string instead of int value in my textbox then value==null and when i entered int value then value==entered value;. Why?

Is there any alternate by which i can achieve the same (make sure at server side only)

回答1:

The reason this happens is because the model binder (which runs before any validators) is unable to bind an invalid value to integer. That's why inside your validator you don't get any value. If you want to be able to validate this you could write a custom model binder for the integer type.

Here's how such model binder could look like:

public class IntegerBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
        int temp;
        if (value == null || 
            string.IsNullOrEmpty(value.AttemptedValue) || 
            !int.TryParse(value.AttemptedValue, out temp)
        )
        {
            bindingContext.ModelState.AddModelError(bindingContext.ModelName, "invalid integer");
            bindingContext.ModelState.SetModelValue(bindingContext.ModelName, value);
            return null;
        }

        return temp;
    }
}

and you will register it in Application_Start:

ModelBinders.Binders.Add(typeof(int), new IntegerBinder());

But you might ask: what if I wanted to customize the error message? After all, that's what I was trying to achieve in the first place. What's the point of writing this model binder when the default one already does that for me, it's just that I am unable to customize the error message?

Well, that's pretty easy. You could create a custom attribute which will be used to decorate your view model with and which will contain the error message and inside the model binder you will be able to fetch this error message and use it instead.

So, you could have a dummy validator attribute:

public class MustBeAValidInteger : ValidationAttribute, IMetadataAware
{
    public override bool IsValid(object value)
    {
        return true;
    }

    public void OnMetadataCreated(ModelMetadata metadata)
    {
        metadata.AdditionalValues["errorMessage"] = ErrorMessage;
    }
}

that you could use to decorate your view model:

[MustBeAValidInteger(ErrorMessage = "The value {0} is not a valid quantity")]
public int Quantity { get; set; }

and adapt the model binder:

public class IntegerBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
        int temp;
        var attemptedValue = value != null ? value.AttemptedValue : string.Empty;

        if (!int.TryParse(attemptedValue, out temp)
        )
        {
            var errorMessage = "{0} is an invalid integer";
            if (bindingContext.ModelMetadata.AdditionalValues.ContainsKey("errorMessage"))
            {
                errorMessage = bindingContext.ModelMetadata.AdditionalValues["errorMessage"] as string;
            }
            errorMessage = string.Format(errorMessage, attemptedValue);
            bindingContext.ModelState.AddModelError(bindingContext.ModelName, errorMessage);
            bindingContext.ModelState.SetModelValue(bindingContext.ModelName, value);
            return null;
        }

        return temp;
    }
}