Tomas Aschan
Tomas Aschan

Reputation: 60614

Validate complex types with DataAnnotations

I've decided to use Entity Framework for O/R Mapping, and DataAnnotations for validation in my project, and I have now encountered an odd problem when trying to implement this.

This is what I've done:

I have the following entity type

Contact
*******
Int32 Id (not null, Entity Key)
Name Name (not null) 
Address Address (not null)
String Phone
String Email

where Name and Address are complex types defined as follows:

Name                                  Address
****                                  *******
String First (not null)               String Street (not null)
String Last (not null)                String ZipCode (not null)
                                      String City (not null)

And the following classes reside in the same namespace as my entities:

public class ContactMetadata
{
    [Required]
    public Name Name { get; set; }
}

[MetadataType(typeof(ContactMetadata))]
partial class Contact { }

However, when I create a new Contact item, the Name and Address types are filled with instances of Name and Address where all values are null, instead of Name and Address having null values themselves. Thus, the Required attribute doesn't throw any errors, although all values are null. How do I work around this?

Upvotes: 2

Views: 4990

Answers (4)

BlackjacketMack
BlackjacketMack

Reputation: 5692

I'm struggling with the same issue right now. I think a semi-graceful way to do it is to reference the key primitive type as a property, and put the dataannotation on that. Here's an example with AddressID being the key field.

public class Contact{

[Required]
public int? AddressIDForValidation{
get{return this.Address.AdressID;}
}

public Address Address{get;set;}
}



public class Address{
public int? AddressID{get;set;}
public string Street{get;set;}
}

Upvotes: 0

Robert F Burke
Robert F Burke

Reputation:

Check out this blog post complex-dataannotations-validation. I think the RequiredAssociation attribute is what you need. You might have to tweak it a little bit for Entity Framework instead of LINQ to SQL.

Upvotes: 0

blu
blu

Reputation: 13175

So it creates instances of the Name and Address objects who's properties are null? Interesting.

Can you just put the [Required] attribute on the children?

EDIT: I know this might be considered a smelly way of doing this, but for clarity I edit your answer into the post, so that it can easier be found by the next person having problems with this...

Suggested (and accepted, but yet untested) solution:

Write a custom validation attribute which validates against the null values.

Upvotes: 1

Brad Wilson
Brad Wilson

Reputation: 70626

Make sure the names that end up in the HTML fields line up with the property names of the class.

For example, if you have this:

public class Contact {
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Address Address { get; set; }
}

public class Address {
    public string Street { get; set; }
    public string City { get; set; }
    [...]
}

Your calls to the HTML helpers should look like this:

<%= Html.TextBox("FirstName") %>
<%= Html.TextBox("LastName") %>
<%= Html.TextBox("Address.Street") %>
<%= Html.TextBox("Address.City") %>
[...]

Upvotes: 1

Related Questions