Fluent Validation in MVC: Geben Sie RuleSet für die clientseitige Validierung an.
In meinem ASP.NET MVC 4-Projekt habe ich einen Prüfer für eines meiner Ansichtsmodelle, die eine Regeldefinition für Regelsätze enthalten.Edit
Regelsatz, der in der Post-Aktion verwendet wird, wenn alle Client-Überprüfungen bestanden wurden.Url
undEmail
rule legt Regeln fest, die in @ verwendet werdeEdit
Regelsatz (siehe unten) und in speziellen Ajax-Aktionen, die nur E-Mail und nur URL entsprechend validieren.
Mein Problem ist, dass Ansicht nicht weiß, dass es @ verwenden sollEdit
Regelsatz für die Generierung von Client-HTML-Attributen und Verwendung vondefault
Regelsatz, der leer ist.Wie kann ich der Ansicht die Verwendung von @ mitteileEdit
Regelsatz für die Generierung von Eingabeattributen?
Modell
public class ShopInfoViewModel
{
public long ShopId { get; set; }
public string Name { get; set; }
public string Url { get; set; }
public string Description { get; set; }
public string Email { get; set; }
}
Validator:
public class ShopInfoViewModelValidator : AbstractValidator<ShopInfoViewModel>
{
public ShopInfoViewModelValidator()
{
var shopManagementService = ServiceLocator.Instance.GetService<IShopService>();
RuleSet("Edit", () =>
{
RuleFor(x => x.Name)
.NotEmpty().WithMessage("Enter name.")
.Length(0, 255).WithMessage("Name length should not exceed 255 chars.");
RuleFor(x => x.Description)
.NotEmpty().WithMessage("Enter name.")
.Length(0, 10000).WithMessage("Name length should not exceed 10000 chars.");
ApplyUrlRule(shopManagementService);
ApplyEmailRule(shopManagementService);
});
RuleSet("Url", () => ApplyUrlRule(shopManagementService));
RuleSet("Email", () => ApplyEmailRule(shopManagementService));
}
private void ApplyUrlRule(IShopService shopService)
{
RuleFor(x => x.Url)
.NotEmpty().WithMessage("Enter url.")
.Length(4, 30).WithMessage("Length between 4 and 30 chars.")
.Matches(@"[a-z\-\d]").WithMessage("Incorrect format.")
.Must((model, url) => shopService.Available(url, model.ShopId)).WithMessage("Shop with this url already exists.");
}
private void ApplyEmailRule(IShopService shopService)
{
// similar to url rule: not empty, length, regex and must check for unique
}
}
eispiel für eine @ Validierungsaktion:
public ActionResult ValidateShopInfoUrl([CustomizeValidator(RuleSet = "Url")]
ShopInfoViewModel infoViewModel)
{
return Validation(ModelState);
}
Get and Post-Aktionen fürShopInfoViewModel
:
[HttpGet]
public ActionResult ShopInfo()
{
var viewModel = OwnedShop.ToViewModel();
return PartialView("_ShopInfo", viewModel);
}
[HttpPost]
public ActionResult ShopInfo(CustomizeValidator(RuleSet = "Edit")]ShopInfoViewModel infoViewModel)
{
var success = false;
if (ModelState.IsValid)
{
// save logic goes here
}
}
View enthält den nächsten Code:
@{
Html.EnableClientValidation(true);
Html.EnableUnobtrusiveJavaScript(true);
}
<form class="master-form" action="@Url.RouteUrl(ManagementRoutes.ShopInfo)" method="POST" id="masterforminfo">
@Html.TextBoxFor(x => x.Name)
@Html.TextBoxFor(x => x.Url, new { validationUrl = Url.RouteUrl(ManagementRoutes.ValidateShopInfoUrl) })
@Html.TextAreaFor(x => x.Description)
@Html.TextBoxFor(x => x.Email, new { validationUrl = Url.RouteUrl(ManagementRoutes.ValidateShopInfoEmail) })
<input type="submit" name="asdfasfd" value="Сохранить" style="display: none">
</form>
Ergebnis-HTML-Eingabe (ohne Client-Validierungsattribute):
<input name="Name" type="text" value="Super Shop"/>