3
プロジェクト:すべてが整備されているが、検証が動作していない
- ASP.NET 4.5.2
- MVC 5
- 流暢検証
- ノーDB、ノーバックエンド
Web.config:
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
モデル:
[Validator(typeof(ContactValidator))]
public class ContactViewModel {
[DisplayName("Name:")]
public string Name { get; set; }
[DisplayName("Phone:")]
[DataType(DataType.PhoneNumber)]
public string Phone { get; set; }
[DisplayName("eMail:")]
[DataType(DataType.EmailAddress)]
public string eMail { get; set; }
[DisplayName("Message:")]
public string Message { get; set; }
}
バリ:
public class ContactValidator : AbstractValidator<ContactViewModel> {
public ContactValidator() {
RuleFor(x => x.Name)
.NotEmpty().WithMessage("Please provide a name.")
.Length(2, 255).WithMessage("Please provide a name of some substantial length.");
RuleFor(x => x.Phone)
.NotEmpty().WithMessage("Please enter a valid 10-digit phone number.")
.Length(12, 12).WithMessage("Phone number must be in the form of “123-456-7890”")
.Matches(@"^\d{3}-\d{3}-\d{4}$").WithMessage("Phone number must be a valid 10-digit phone number with dashes, in the form of “123-456-7890”");
RuleFor(x => x.eMail)
.NotNull().WithMessage("Please provide an eMail address.")
.EmailAddress().WithMessage("Please provide a valid eMail address to recieve the download link at.");
RuleFor(x => x.Message)
.NotNull().WithMessage("Please provide a message.")
.Length(2, 4000).WithMessage("Please provide a message of some substantial length.");
}
}
コントローラー:
[HttpGet]
public ActionResult Contact() {
var model = new ContactViewModel() { };
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Contact(ContactViewModel model) {
if(ModelState.IsValid) {
return View("Index");
}
return View(model);
}
ビュー:
@using(Html.BeginForm()) {
@Html.AntiForgeryToken()
<ul><li>All fields are required.</li></ul>
<fieldset>
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label" })@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control", maxlength = "255" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.Phone, htmlAttributes: new { @class = "control-label" })@Html.EditorFor(model => model.Phone, new { htmlAttributes = new { @class = "form-control", maxlength = "12" } })
@Html.ValidationMessageFor(model => model.Phone, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.eMail, htmlAttributes: new { @class = "control-label" })@Html.EditorFor(model => model.eMail, new { htmlAttributes = new { @class = "form-control", maxlength = "255" } })
@Html.ValidationMessageFor(model => model.eMail, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.Message, htmlAttributes: new { @class = "control-label" })@Html.TextAreaFor(model => model.Message, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Message, "", new { @class = "text-danger" })
<label class="control-label"> </label><button type="submit" value="Save" title="Save" class="btn btn-primary glyphicon glyphicon-send"></button>
</fieldset>
}
レイアウトビュー:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Lawyer Case | @ViewBag.Title</title>
@Styles.Render("~/Content/bootstrap")
@Styles.Render("~/Content/lightbox")
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/bootstrap")
@Scripts.Render("~/bundles/inputmask")
@Scripts.Render("~/bundles/lightbox")
</head>
<body>
<div class="navbar navbar-blue navbar-fixed-top">
<div class="container">
<header class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<h1>@Html.ActionLink("Lawyer Case", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })</h1>
</header>
<nav class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "Index")</li>
<li>@Html.ActionLink("Contact Us", "Contact")</li>
</ul>
</nav>
</div>
</div>
<main class="container body-content">
@RenderBody()
</main>
<footer class="container">
<hr />
<p>Site Contents © @DateTime.Now.Year - Lawyer Case.<br />Site Design & Development © @DateTime.Now.Year - Eclipse Computing Ltd.</p>
</footer>
@RenderSection("scripts", required: false)
</body>
</html>
は私が@Scripts.Render("~/bundles/jqueryval")
を置く場所、検証はクライアント側ORサーバー側のどちらかのために働いていない問題ではありません。 ModelState.IsValid
がはfalse
をする必要があるためだけで...ここで少し当惑
私はちょうどヒットすべてでそれを記入せずにフォームの「提出」した場合、それは、Contactページやフォームに私を返す必要がありますしかし、明らかにそうではありません - インデックスページが代わりに取得されています。これは、フォームが空のために検証が適切に検証されていないことを示していますが、ModelState.IsValid = true
です。なぜ、私は知らない。
の
Application_Start()
には 'FluentValidationModelValidatorProvider.Configure()を経由して、それを配線してください作りましたか;'? –Oooookay ...それを解決策として追加できたら、それを正しい答えとしてマークしていくつかのポイントを挙げることができます。また、適切な場所で狩りをする必要があるため、Global.asax.csファイルに移動する必要があることにも言及してください。また、なぜFluentValidationのようなパッケージをNuGet経由で持ってきたときに、このような自動設定ができないのですか?これは簡単ではないようですが、あなたがパッケージを持ってきたら明らかにそれを使いたいので、ツールをどのように実装しようとも、必要ないくつかの基本的な要点でプロジェクトを自動設定しましょう。 –