0

I have a Entity Framework model class that has a view to create an instance of that model class. I wanted to validate dates on that class so that users add dates that meet certain criteria. I have a metadata class that uses attributes from the System.ComponentModel and System.ComponentModel.DataAnnotations namespaces.

Is there any attribute from there that I can use to perform the validation or is there some other way I can do this?

Steven
  • 166,672
  • 24
  • 332
  • 435
Sachin Kainth
  • 45,256
  • 81
  • 201
  • 304
  • 1
    Hard to say without more details / examples. I suppose you mean they are constraints on your possible dates coming from different properties of your model ? If so (and even if not), I would take a look at FluentValidation http://fluentvalidation.codeplex.com/ – Raphaël Althaus Jun 11 '12 at 14:16
  • All it is, is that I have two dates - a start and an end date = the start date has to be before the end date - that's all - I just don't know where to put that code. – Sachin Kainth Jun 11 '12 at 14:19
  • 2
    So you'll need a CustomValidationAttribute, as existing attributes don't know their "object" (each property has no idea of other properties of the instance). Or use FluentValidation. Or use Fluentvalidation. Or use FluentValidation. ;) – Raphaël Althaus Jun 11 '12 at 14:26
  • :-). I think I might use FluentValidation then :-) – Sachin Kainth Jun 11 '12 at 14:28
  • @dskh hmm. Good point - not sure how I can do that though – Sachin Kainth Jun 11 '12 at 14:29
  • @RaphaëlAlthaus I can't get FluentValidation to work. It won't recognise the Validator attribute. I'm using http://fluentvalidation.codeplex.com/wikipage?title=mvc – Sachin Kainth Jun 11 '12 at 14:43
  • 1
    You downloaded FluentValidation, read doc, added references in your project (FluentValidation.dll), used the right "using"s (`using FluentValidation;`) in so little time ? (kidding, but there must be a ref / using missing somehwere). – Raphaël Althaus Jun 11 '12 at 14:46
  • @RaphaëlAlthaus I'm using "using FluentValidation.Mvc;" – Sachin Kainth Jun 11 '12 at 14:48
  • @RaphaëlAlthaus I've got it all sorted - please post a quick answer so I can mark it as answer – Sachin Kainth Jun 11 '12 at 15:11

2 Answers2

1

You can use the CustomValidationAttribute to do whatever additional validation you want. You'll have to work a bit harder if you need client-side validation to go with it though!

http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.customvalidationattribute.aspx

Richard
  • 29,854
  • 11
  • 77
  • 120
1

You'll need a CustomValidationAttribute, as existing attributes don't know their "object" (each property has no idea of other properties of the instance).

But my choice would go to FluentValidation, which has a nice fluent interface and give you the choice to interact between the properties of the object to validate.

http://fluentvalidation.codeplex.com

Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122