This project is read-only.
3

Closed

Validation should honor member field in ValidationResult

description

Reported by a user:

The problem I am having is with models like:

// the view model
public class RegisterModel : IValidatableObject
{

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
        if (somethingIsWrong)
            yield return new ValidationResult("Something went wrong", new[] { "Field1" });

        if (somethingElseIsWrong)
            yield return new ValidationResult("Something went wrong", new[] { "Field2" });
}
}

// the controller action
public HttpResponseMessage Post(RegisterModel model)
    {
        return Request.CreateResponse(HttpStatusCode.OK);
    }
In the controller I expect this error to be retrievable from ModelState["model.Field1"] but instead it is ModelState["model"]. Additionally, because the field name is dropped there can be only one error. If somethingIsWrong && somethingElseIsWrong only one of the errors is available.
Closed Jun 5, 2013 at 10:21 PM by kirthik
Verified

comments

besterbr wrote Apr 19, 2013 at 9:57 AM

This is a really big problem. Essentially it renders the 'IValidatableObject support' useless.