1
votes

In my model I have an nullable int.

In the view I have an input of type 'number'.

The problem is that the user could possibly input a number larger than the maximum possible integer value, which results in the following error:

The value [value] is not valid for [field]

I want to change this message, and putting a [Range(...)] validation on the attribute does not help, as non of my validations are being called, because the framework cannot parse the value to an int.

--------- EDIT ---------

My validations perform normally at normal input as shown here:

enter image description here

What I mean about my validations not being called is that I've read that the if the framework fails to parse the value to an int (because the value exceeds the max value of an integer), it ignores any further "unnecessary" validation.

Therefore my Range validation is ignored, as shown here:

enter image description here

I've tried creating a custom ValidationAttribute, but that has the same behaviour (Being called correctly at normal integer input, but is ignored at overflowing integer input)

--------- /EDIT ---------

All the solutions I have seen, proposes binding a custom resource file, in which I could override the default message for invalid values.

But I want to be able to specify a custom message at each attribute in case of an invalid value.

I thought about using strings instead of ints, and creating a custom ValidationAttribute which tries to parse it to an int.

Are there any alternatives?

// EDIT: I could avoid this problem (to some degree) by having client-side validation - But I want to know if it is possible from the server-side, in case of someone editing the html and forcing a higher value

1
What do you mean none of my validations are being called? If your code is correct they will be called. You need to show your code. - user3559349
It works fine for me (refer this DotNetFiddle). If you enter 9999999999999 the range validation message is displayed as soon as you tab out of the control. - user3559349
But that is with client-side validation - Which partly solves the problem, but I want server-side validation.. - Jpst
You are getting server side validation (but maybe not the way you expect). What is happening is the DefaultModelBinder tries to set the value to 9999999999999 which it cant, so the value is not valid error is added to ModelState (the 'Range' condition is not even checked because there is no point) - user3559349
Exactly - So my question still remains, how can I (if I can) override that default message, and if so, can I set different messages to different attributes? - Jpst

1 Answers

0
votes

There is couple of ways in which you can validate user input:

Here is an example of custom validation attribute:

public class MyIntegerValidationAttribute : ValidationAttribute
{
    public string[] PropertyNames { get; }

    public MyIntegerValidationAttribute(params string[] propertyNames)
    {
        PropertyNames = propertyNames;
    }    

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        var properties = this.PropertyNames.Select(validationContext.ObjectType.GetProperty);

        //here you have values of your properties 
        var values = properties.Select(p => p.GetValue(validationContext.ObjectInstance, null)).OfType<int>();

        if (YOUR_CUSTOM_CONDITION)
        {
            return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName));
        }

        return null;
    }
}

public class ViewModel
{
    [MyIntegerValidationAttribute("B", "C", ErrorMessage = "My error message")]
    public int A { get; set; }
    public int B { get; set; }
    public int C { get; set; }
}