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.
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
ModelStatedictionary for the propertyGroup.Name.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
ModelStatedictionary wasGroup.Group.Name. All I had to do was change the returnedValidationResult.