0

Using aspnet mvc2 with the model auto-generated by entity framework:

Is it possible to tell entity framework to auto-annotate all fields? eg:

If database field says not null then add [Required] If DB field is a nvarchar(x) then add [StringLength(x)]

And so on?

What if the field name contains the string "email" eg CustomerEmail - can I get EF to auto-annotate that with an appropriate annotation ([Regex()] maybe)

As I understand it, if the model fields are annotated, and I use both Html.ValidationMessageFor() and use if (ModelState.IsValid) in my controller, then that is all I need to do to have basic clientside input validation working?

Thanks

JK.
  • 21,477
  • 35
  • 135
  • 214
  • possible duplicate of http://stackoverflow.com/questions/2330573/generating-data-annotations-from-generated-classes – Craig Stuntz Apr 28 '10 at 12:59
  • Yes, its a duplicate - I didn't see the older question because its question and tags are too vague. The answer in that question hasn't helped either. – JK. Apr 28 '10 at 21:17

2 Answers2

1

I am not aware of any automatically generated attributes.

As I understand it, if the model fields are annotated, and I use both Html.ValidationMessageFor() and use if (ModelState.IsValid) in my controller, then that is all I need to do to have basic clientside input validation working?

That is correct. http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-2-modelmetadata.html

Raj Kaimal
  • 8,304
  • 27
  • 18
  • Its seems like it would be such a common requirement. Eg The field in the database is nvarchar(20), so you would expect to be able to somehow be able to get an automatic annotation of [StringLength(20)]. To have to manually type the annontation for each and every field would be very wrong. – JK. Apr 28 '10 at 21:31
  • I saw your new blog post http://weblogs.asp.net/rajbk/archive/2010/05/04/a-basic-t4-template-for-generating-model-metadata-in-asp-net-mvc2.aspx solving this problem .. thanks a lot and +1 :) – JK. May 04 '10 at 22:27
  • JK, the post is an early attempt. For example, you cannot make any changes to the auto generated fields. You could copy the generated class into another class but you have to do the extra work of making sure your classes are in sync. – Raj Kaimal May 04 '10 at 23:11
0

in jQuery, you can run through every object in a page. If it's client side validation you're looking for, you can use jQuery's field validation.

Martin Ongtangco
  • 22,657
  • 16
  • 58
  • 84
  • 1
    With 50+ tables and many hundreds of fields, any solution must be automatic. It would be wrong to manually type any code when the database already contains the metadata describing if the field is required, the field length and so on. – JK. Apr 28 '10 at 21:19
  • you can separate these fields from one another by applying classes to the objects. for say, you search every input field there is, then locate if this field has, let's say, a "chumbawamba" class name in it. – Martin Ongtangco Apr 29 '10 at 01:16