Quantcast
Channel: Fluent Validation for .NET
Viewing all articles
Browse latest Browse all 1917

New Post: Apply validator to one item in array

$
0
0

Hello,

I created a custom validator to validate the Mime type of an HttpPostedFileBase:

  public class FileMimeTypeValidator : PropertyValidator {

    private List<String> _types;

    public FileMimeTypeValidator(List<String> types)
      : base(() => Resources.Message.FileMimeType) {

      if (types == null) throw new ArgumentNullException("types");
      _types = types;

    } // FileMimeTypeValidator

    protected override Boolean IsValid(PropertyValidatorContext context) {

      HttpPostedFileBase file = (HttpPostedFileBase)context.PropertyValue;
      return file == null ? true : file.ContentLength == 0 ? true : _types.Contains(file.ContentType);

    } // Validate

  } // FileMimeTypeValidator

I have a model with a property of type HttpPostedFileBase[].

How can I apply my validator only to the first item of this array?

Is this possible?

Thank You,

Miguel

 


Viewing all articles
Browse latest Browse all 1917

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>