views:

25

answers:

2

I have a competition class and a competitionEntry class. A competition can have 5 questions (just string values) and the competitionEntry can have 5 answers (also only string values).

How would I validate that if a question is present that the answer should be present as well?

I think I need a custom validation attribute but how would I get the competition class properties to check if the answer is filled in and so the questions must also be?

A: 

I don't think you can do this with an attribute. It makes more sense to have the Competition class validate entries.

public class Competition
{
    public bool AnswersAreCorrectFor(CompetitionEntry entry)
    {
        // check answers
    }
    // ...
}
Ryan
I would like to do it via DataAnnotations as the next step would be the client side validation.
William
Sorry I misread the question. I'm not sure that there is a conditionally required validation attribute. You'd need a client side library that knows about one if you don't want to write the JS yourself.
Ryan
A: 

I haven't done this but AFAIK, you can definitely do it with DataAnnotations.

You can implement a subclass of ValidationAttribute.

Check out this question to get the idea.

Shimmy