views:

328

answers:

1

Example: We have a conditional field. This conditional field is a radio button with the following two values “yes” and “no”. Lets say the name of this radiobutton is “AAA”.

This conditional field “AAA” should only be displayed when another radio button field “BBB” is set to “yes”. (Values of radio button “BBB” are also “yes” and no”) .

But the conditional field “AAA” should be displayed with NO pre-set value, means “yes” nor “no” should be set when the field is first displayed.

The problem occurs based on the requirement that the conditional field “AAA” should ONLY be required when (the non-conditional) field “BBB” is set to “yes” – and not required when the field “BBB” is set to “no”.

(Sounds, that I didn’t heard anything about an if statement, or? But hold on and continue reading ...)

Believe me, it would not be a problem for me to solve this topic when we would use the “Modelstate” – but we are talking here about Validation (Data Annotations) that looks like this here:

public class Input1FormModel 
{
     [Required(ErrorMessageResourceName="Error_Field_AAA_Empty",
               ErrorMessageResourceType=typeof(Resources.MyDialog))]
     public int AAA { get; set; }
}

I fully understand ALSO these lines of code - I believe ;-)

...

//property limits
public int UpperBound { get { return DateTime.Now.Year; } }
public int LowerBound { get { return 1900; } }

...

[NotNullValidator]
[PropertyComparisonValidator("LowerBound", ComparisonOperator.GreaterThan)]
[PropertyComparisonValidator("UpperBound", ComparisonOperator.LessThanEqual)]
public int? XYZ { get; set; }

But, how to solve the above described dependency (AAA <-> BBB)?

Changing “return DateTime.Now.Year;” to a function call which checks first the other field and returns then true or false? But how to fetch there the value of the other field?

+1  A: 

You may need to use IDataErrorInfo.

See this question, where I answered this:

Check out IDataErrorInfo and this question I asked about IDataErrorInfo vs. DataAnnotations.

Martin
thx 4 u very fast reply. u are right. The tutorial says: "For example, you might want to enforce a validation rule that depends on the values of multiple properties of the Movie class" ... so its possible. Another way to solve it is using the Modelstate as I already said in my original posting ... but the goal was to use Attributes like [Required ...] or [PropertyComparisonValidator("LowerBound", ComparisonOperator.GreaterThan)] ... So finally is this not my solution :-) May be that this couldnt solved with Attributes ... One else any idea?But dont get me wrong: Thanks for your reply/answer!!
I was just trying to give you an alternative approach, since I do not know of a way to do what you are asking using Attributes. Here, check out this question/answer: http://stackoverflow.com/questions/1511495/using-idataerrorinfo-in-asp-net-mvc/1511545#1511545
Martin
Martin, believe me. I am very thankful about your fast reply and very good answer! Sorry, when I used wrong words in my reply. It wasnt my approach to insult you.