この投稿に記載されているコードを実装しようとしています。言い換えれば、利用規約のチェックボックスに目立たない検証を実装しようとしています。ユーザーがチェックボックスを選択していない場合、入力は無効としてマークされます。
これはサーバー側の Validator コードです。追加しました:
/// <summary>
/// Validation attribute that demands that a boolean value must be true.
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public class MustBeTrueAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
return value != null && value is bool && (bool)value;
}
}
これがモデルです
[MustBeTrue(ErrorMessage = "You must accept the terms and conditions")]
[DisplayName("Accept terms and conditions")]
public bool AcceptsTerms { get; set; }
これは私の見解です:
@Html.EditorFor(x => x.AcceptTermsAndConditions)
@Html.LabelFor(x => x.AcceptTermsAndConditions)
@Html.ValidationMessageFor(x => x.AcceptTermsAndConditions)
これは、バリデーターのクライアント側をアタッチするために使用した jQuery です。
$.validator.unobtrusive.adapters.addBool("mustbetrue", "required");
ただし、クライアント側のスクリプトは機能していないようです。送信ボタンを押すたびに、他のフィールドの検証は正常に開始されますが、利用規約の検証は開始されていないようです。これは、送信ボタンをクリックした後のコードが Firebug でどのように見えるかです。
<input type="checkbox" value="true" name="AcceptTermsAndConditions" id="AcceptTermsAndConditions" data-val-required="The I confirm that I am authorised to join this website and I accept the terms and conditions field is required." data-val="true" class="check-box">
<input type="hidden" value="false" name="AcceptTermsAndConditions">
<label for="AcceptTermsAndConditions">I confirm that I am authorised to join this website and I accept the terms and conditions</label>
<span data-valmsg-replace="true" data-valmsg-for="AcceptTermsAndConditions" class="field-validation-valid"></span>
何か案は?私は一歩を踏み外したことがありますか?これは私をトイレに駆り立てています!
よろしくお願いします S