2

I have the following view models:

public class Search {
    public int Id { get; set; }

    [Required(ErrorMessage = "Please choose a name.")]
    public string Name { get; set; }

    [ValidGroup(ErrorMessage = "Please create a new group or choose an existing one.")]
    public Group Group { get; set; }
}

public class Group {
    public int Id { get; set; }
    public string Name { get; set; }
}

I have defined a custom validation attribute as follows:

public class ValidGroupAttribute : ValidationAttribute {
    public override bool IsValid(object value) {
        if (value == null)
            return false;

        Group group = (Group)value;

        return !(string.IsNullOrEmpty(group.Name) && group.Id == 0);
    }
}

I have the following view (omitted some for brevity):

    @Html.ValidationSummary()

    <p>
        <!-- These are custom HTML helper extensions. -->
        @Html.RadioButtonForBool(m => m.NewGroup, true, "New", new { @class = "formRadioSearch", id = "NewGroup" })
        @Html.RadioButtonForBool(m => m.NewGroup, false, "Existing", new { @class = "formRadioSearch", id = "ExistingGroup" })
    </p>
    <p>
        <label>Group</label>
        @if (Model.Group != null && Model.Group.Id == 0) {
            @Html.TextBoxFor(m => m.Group.Name)
        }
        else {
            @Html.DropDownListFor(m => m.Group.Id, Model.Groups)
        }
    </p>

The issue I'm having is the validation class input-validation-error does not get applied to the Group input. I assume this is because the framework is trying to find a field with id="Group" and the markup that is being generated has either id="Group_Id" or id=Group_Name. Is there a way I can get the class applied?

http://f.cl.ly/items/0Y3R0W3Z193s3d1h3518/Capture.PNG

Update

I've tried implementing IValidatableObject on the Group view model instead of using a validation attribute but I still can't get the CSS class to apply:

public class Group : IValidatableObject
{
    public int Id { get; set; }
    public string Name { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) {
        if (string.IsNullOrEmpty(Name) && Id == 0) {
            yield return new ValidationResult("Please create a new group or select an existing one.", new[] { "Group.Name" });
        }
    }
}

Update 2

Self validation doesn't work. I think this is because the second parameter in the ValidationResult constructor isn't used in the MVC framework.

From: http://www.devtrends.co.uk/blog/the-complete-guide-to-validation-in-asp.net-mvc-3-part-2

In some situations, you might be tempted to use the second constructor overload of ValidationResult that takes in an IEnumerable of member names. For example, you may decide that you want to display the error message on both fields being compared, so you change the code to this:

return new ValidationResult( FormatErrorMessage(validationContext.DisplayName), new[] { validationContext.MemberName, OtherProperty });

If you run your code, you will find absolutely no difference. This is because although this overload is present and presumably used elsewhere in the .NET framework, the MVC framework completely ignores ValidationResult.MemberNames.

3
  • What validation class did you expect to have added? ValidGroupAttribute does not specify a CSS class, and you didn't make the Group "Required". Commented Jan 13, 2012 at 0:28
  • 1
    I don't believe the ID has anything to do with it. (If I recall correctly, the name is what the validation keys off of.) Commented Jan 13, 2012 at 0:32
  • @StriplingWarrior I expected the class input-validation-error to be applied to the property. This does work if I put @Html.TextBoxFor(m => m.Group) in my view but that doesn't make sense as a single string doesn't bind to a Group object. Commented Jan 13, 2012 at 1:23

1 Answer 1

1

I've come up with a solution that works but is clearly a work around.

I've removed the validation attribute and created a custom model binder instead which manually adds an error to the ModelState dictionary for the property Group.Name.

public class SearchBinder : DefaultModelBinder {
    protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext,
       PropertyDescriptor propertyDescriptor) {
        if (propertyDescriptor.Name == "Group" &&
            bindingContext.ValueProvider.GetValue("Group.Name") != null &&
            bindingContext.ValueProvider.GetValue("Group.Name").AttemptedValue == "") {
            ModelState modelState = new ModelState { Value = bindingContext.ValueProvider.GetValue("Group.Name") };
            modelState.Errors.Add("Please create a new group or choose an existing one.");
            bindingContext.ModelState.Add("Group.Name", modelState);
        }
        base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
    }
}

// Register custom model binders in Application_Start()
ModelBinders.Binders.Add(typeof(SearchViewModel), new SearchBinder());

With ModelState["Group.Name"] now having an error entry, the CSS class is being rendered in the markup.

I would much prefer if there was a way to do this with idiomatic validation in MVC though.

Solved!

Found a proper way to do this. I was specifying the wrong property name in the self validating class, so the key that was being added to the ModelState dictionary was Group.Group.Name. All I had to do was change the returned ValidationResult.

yield return new ValidationResult("Please create a new group or select an existing one.", new[] { "Name" });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.