0

I have used validator required field in my model as follow and its working

  [Required(ErrorMessage = "Description is required.")]
        public string Description { get; set; }

Now i have another property of integer array type

 public string[] Roles { get; set; }

I am not able to get how can i put required field validator on integer array ?

1
  • You should write custom validation attribute Commented Oct 2, 2013 at 11:41

2 Answers 2

3

Personally i don't like using attributes for model validation,

You can use fluent validation http://fluentvalidation.codeplex.com/

So the validation logic is gathered at one place and doesn't pollute the model.

Sign up to request clarification or add additional context in comments.

Comments

2

Write a custom validation attribute.

I've not tested but try code like this :

public class RequiredArray : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        var list = value as IList;
        if (list != null)
        {
            return list.Count > 0;
        }
        return false;
    }
}

[RequiredArray (ErrorMessage = "Roles is required.")]
public string[] Roles{ get; set; }

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.