The only reference I could find was this.
Now if your model is that simple and this rule is the only one then this rule should be enough:
RuleFor(x => x)
.Must(x => !string.IsNullOrWhiteSpace(x.Email) || !string.IsNullOrWhiteSpace(x.UserName))
.WithName(".") // This adds error message to MVC validation summary
.WithMessage("Please enter either a username or email address");
Just add @Html.ValidationSummary()
to your view and you're good to go.
But if you're going to put more rules on your model then to my knowledge there is only one 'hacky' way I could think of:
in your controller action add this:
if (!ModelState.IsValid)
{
if (ModelState["."].Errors.Any())
{
ModelState.AddModelError(string.Empty, ModelState["."].Errors.First().ErrorMessage);
}
// ...
}
This will add first error message from "."
property to model property (adapt it to your needs). Also you will have to do a @Html.ValidationSummary(true)
to show only model level errors in validation summary.
Third option: add rule(s) to unused_property
and use @Html.ValidationSummaryFor(x => x.unused_property)
as a validation summary