2

I'm writing a custom validation attribute for validating the model and I need some additional information other than what ValidationResult offers. I need to return ErrorMessage and ErrorCode and access it in the controller class so that I can send it in the response payload.

public class CustomValidationResult : ValidationResult
{
    public int ErrorCode { get; set; }

    protected CustomValidationResult(ValidationResult validationResult) : base(validationResult)
    {
    }
}

public class Mandatory : RequiredAttribute
{
    public int ErrorCode { get; set; }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        ErrorCode = 10;

        var result = base.IsValid(value, validationContext);

        ErrorCode = 10;

        return new CustomValidationResult(result)
        {
            ErrorCode = ErrorCode
        };
    }
}

I need to get the ErrorCode out in the controller, if ModelState.IsValid fails.

Thanks in advance.

6
  • Have you tried putting a TryCatch block around base.IsValid Commented Aug 13, 2018 at 9:25
  • What problem are you having? Commented Aug 13, 2018 at 9:31
  • What will you deal with ErrorCode if ModelState.IsValid fails? It is impossible to extend class property, and you could not custom ValidationResult which is defined in ValidationAttribute. For a workaound, I suggest you try to store error code in error message like [Required(ErrorMessage ="[400]Name is required")], and then get Error Code 400 from ErrorMessage. Commented Aug 14, 2018 at 2:26
  • Thank you @Edward. I am already doing a workaround. I was trying to know if there is a right and neater way of doing this. Commented Aug 14, 2018 at 7:36
  • When will you use ErrorCode? Do you only use this in Controller like ModelState.IsValid, or you will use it in the View? If later, I am afriad it is impossible. If previous, you may consider define static class to valide the model by your self instead of using ModelState.IsValid. Anyway, it is also complex. I am wondering whether this ErrorCode is requrired since I did not see any meaning for it while validing Model. Commented Aug 14, 2018 at 8:06

1 Answer 1

-1

From a controller you can return for eg:- if

if(!ModelState.IsValid)
{
return BadRequest(ModelState)
}

If the Model is decorated with ErrorMessage, for eg:

Class TypeA
{

[Required[ErrorMessage="Should not be null"]]
public string Name;

}

The error message will be returned when the model state is returned as part of the BadRequest.

If you have any custom validation , error can be added to the ModelState as below from the controller API:

for eg:-

if(typeA.Name == "TestName")
{
    ModelState.AddModelError("Name","TestName is not a valid name.");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.