[RESOLVED]DataAnnotations for validating Either Or group of text boxes

I have a class with a no. of different properties, out of them 3 are int? Prop1, int? Prop2, int? Prop3, which are textbox fields.

UI is like:

Enter value:                       Textbox1        Textbox2      Or     Textbox3

Now validation for these 3 is either Prop1 & Prop2 should be entered or Prop3.

I am using Data Annotations for validation. So my code for validation is:

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
	var memberNames = new List<string>();

	// Other properties validations
	
	if (!this.Prop1.HasValue && !this.Prop2.HasValue && !this.Prop3.HasValue)
            {
                memberNames.Add("Prop1");
                yield return new ValidationResult("This field is required", memberNames);
            }

	if (this.Prop1.HasValue && this.Prop2.HasValue && this.Prop3.HasValue)
            {
                memberNames.Add("Prop1");
                yield return new ValidationResult("This field is required", memberNames);
            }

}

The code is failing when i enter value as decimal eg. 15.5 in Prop3 textbox.

Two validation messages come up is such case:

1. This field is required.

2. The value "15.5" is not valid for "Prop1" for Prop3.

Any alternative way of doing it…Any help would be appreciated..

DreamBig

Data Annotations for validation. So my code for validation is:

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)

This is not DataAnnotation, is IValidatableObject.

DreamBig

is failing when i enter value as decimal eg. 15.5 in Prop3 textbox.

In what Culture do you support that?  en-US? fr-FR ?

yes ur right..IValidatableObject. Culture is en-US.

But i guess since the property is an int so entering 15.5 shows that message. and at that time this.Prop3.HasValue is null.

i made a custom validation method a long time ago. it might be handy for you

        public static Dictionary<string, object> Validate(ModelStateDictionary ms)
        {
            Dictionary<string, object> dict = new Dictionary<string, object>();
            string errors = "";

            var errorList = ms.Select(x => x.Value.Errors.Select(e => e.ErrorMessage).ToArray());
            var errorFields = ms.Where(x => x.Value.Errors.Count > 0).Select(x => x.Key).ToArray();

            foreach (var item in errorList)
            {
                for (int i = 0; i < item.Length; i++)
                {
                    if (errors == "")
                    {
                        errors += @"<div class='alert alert-danger'>
                                        <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>";
                    }
                    errors += "<p>" + item[i] + "</p>";
                }
            }
            errors += "</div>";

            dict.Add("ErrorMessage", errors);
            dict.Add("ErrorFields", errorFields);

            return dict;
        }

i used this with ajax posts. anyway it’s old but it already had the bootstrap markup 

Sorry not what I am looking for. Not sure what to mark as answer. Hope any of the ASP.NET Team may help.

Leave a Reply