1

請求先住所フィールドにカスタム バリデータを適用する必要があります。ビューには、Country ドロップダウン リスト (米国とカナダのオプションあり) や BillingPostalCode テキスト ボックスなど、複数の住所フィールドが表示されます。最初に、米国またはカナダの郵便番号を許可する正規表現をメッセージ コントラクトに適用しました。次のようにします。

[MessageBodyMember]
[Display(Name = "Billing Postal Code")]
[Required]
[StringLength(10, MinimumLength = 5)]
[RegularExpression("(^\\d{5}(-\\d{4})?$)|(^[ABCEGHJKLMNPRSTVXY]{1}\\d{1}[A-Z]{1} *\\d{1}[A-Z]{1}\\d{1}$)", ErrorMessage = "Zip code is invalid.")] // US or Canada
public string BillingPostalCode
{
   get { return _billingPostalCode; }
   set { _billingPostalCode = value; }
}

上記は、米国またはカナダの郵便番号を許可します。ただし、事業主は、BillingCountry ドロップダウン リストで米国またはカナダがそれぞ​​れ選択されている場合にのみ、フォームで米国またはカナダの郵便番号を許可することを望んでいます。彼のテスト ケースでは、カナダを選択し、米国の郵便番号を入力しました。そのシナリオは許可されるべきではありません。

これを行う最初の試みは、これをビューに入れることでしたが、2 つのテキスト ボックス フィールドを作成することに満足していません。必要なフィールドは 1 つだけです。

<div style="float: left; width: 35%;">
   Zip Code<br />
   <span id="spanBillingZipUS">
      @Html.TextBoxFor(m => m.BillingPostalCode, new { @class = "customer_input", @id = "BillingPostalCode" })
   </span>
   <span id="spanBillingZipCanada">
      @Html.TextBoxFor(m => m.BillingPostalCode, new { @class = "customer_input", @id = "BillingPostalCodeCanada" })
   </span>
   @Html.ValidationMessageFor(m => m.BillingPostalCode)
   <br />
</div>

国ドロップダウンリストが切り替えられたときに適切なスパンを表示または非表示にするために jQuery を使用するというのが私の思考プロセスです。その作品は簡単です。

しかし、両方のテキスト ボックスに、上記で貼り付けた MessageBodyMember にマップされる単一のバリデータが適用されているという問題に悩まされています。jQuery で検証コードを記述する方法は知っていますが、サーバー側にも検証を適用したいと考えています。

私はWebフォームから来て、MVCにかなり慣れていません。「古い学校」の Web フォームのカスタム検証は、実装が簡単でした。MVC のカスタム検証についてオンラインで見つけた例は、かなり複雑です。最初は、これは非常に基本的な要求のように見えました。このコードでは、1 つの変数 (選択された国) を評価し、その国に適した正規表現を BillingPostalCode フィールドに適用する必要があります。

MVC3 でこの要件を簡単に満たすにはどうすればよいですか? ありがとう。

4

3 に答える 3

1

さて、私はこの男が作ったものを実装しました、そしてそれはデータ注釈でchramとして機能します。ドロップダウン値のチェックを変更するには少し作業する必要がありますが、これは、データ注釈目立たない方法で検証を実装するために私が見つけたより洗練された方法です。


ここに例があります:

モデル

...
        [RequiredIf("IsUKResident", true, ErrorMessage = "You must specify the City if UK resident")]
        public string City { get; set; }
...

カスタム属性

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace Mvc3ConditionalValidation.Validation
{
    public class RequiredIfAttribute : ValidationAttribute, IClientValidatable
    {
        private RequiredAttribute _innerAttribute = new RequiredAttribute();

        public string DependentProperty { get; set; }
        public object TargetValue { get; set; }

        public RequiredIfAttribute(string dependentProperty, object targetValue)
        {
            this.DependentProperty = dependentProperty;
            this.TargetValue = targetValue;
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            // get a reference to the property this validation depends upon
            var containerType = validationContext.ObjectInstance.GetType();
            var field = containerType.GetProperty(this.DependentProperty);

            if (field != null)
            {
                // get the value of the dependent property
                var dependentvalue = field.GetValue(validationContext.ObjectInstance, null);

                // compare the value against the target value
                if ((dependentvalue == null && this.TargetValue == null) ||
                    (dependentvalue != null && dependentvalue.Equals(this.TargetValue)))
                {
                    // match => means we should try validating this field
                    if (!_innerAttribute.IsValid(value))
                        // validation failed - return an error
                        return new ValidationResult(this.ErrorMessage, new[] { validationContext.MemberName });
                }
            }

            return ValidationResult.Success;
        }

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            var rule = new ModelClientValidationRule()
            {
                ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()),
                ValidationType = "requiredif",
            };

            string depProp = BuildDependentPropertyId(metadata, context as ViewContext);

            // find the value on the control we depend on;
            // if it's a bool, format it javascript style 
            // (the default is True or False!)
            string targetValue = (this.TargetValue ?? "").ToString();
            if (this.TargetValue.GetType() == typeof(bool))
                targetValue = targetValue.ToLower();

            rule.ValidationParameters.Add("dependentproperty", depProp);
            rule.ValidationParameters.Add("targetvalue", targetValue);

            yield return rule;
        }

        private string BuildDependentPropertyId(ModelMetadata metadata, ViewContext viewContext)
        {
            // build the ID of the property
            string depProp = viewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(this.DependentProperty);
            // unfortunately this will have the name of the current field appended to the beginning,
            // because the TemplateInfo's context has had this fieldname appended to it. Instead, we
            // want to get the context as though it was one level higher (i.e. outside the current property,
            // which is the containing object (our Person), and hence the same level as the dependent property.
            var thisField = metadata.PropertyName + "_";
            if (depProp.StartsWith(thisField))
                // strip it off again
                depProp = depProp.Substring(thisField.Length);
            return depProp;
        }
    }
}

クライアント側

...
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>

<script>
    $.validator.addMethod('requiredif',
        function (value, element, parameters) {
            var id = '#' + parameters['dependentproperty'];

            // get the target value (as a string, 
            // as that's what actual value will be)
            var targetvalue = parameters['targetvalue'];
            targetvalue = 
              (targetvalue == null ? '' : targetvalue).toString();

            // get the actual value of the target control
            // note - this probably needs to cater for more 
            // control types, e.g. radios
            var control = $(id);
            var controltype = control.attr('type');
            var actualvalue =
                controltype === 'checkbox' ?
                control.attr('checked').toString() :
                control.val();

            // if the condition is true, reuse the existing 
            // required field validator functionality
            if (targetvalue === actualvalue)
                return $.validator.methods.required.call(
                  this, value, element, parameters);

            return true;
        }
    );

    $.validator.unobtrusive.adapters.add(
        'requiredif',
        ['dependentproperty', 'targetvalue'], 
        function (options) {
            options.rules['requiredif'] = {
                dependentproperty: options.params['dependentproperty'],
                targetvalue: options.params['targetvalue']
            };
            options.messages['requiredif'] = options.message;
        });

</script>
...
    <div class="editor-label">
        @Html.LabelFor(model => model.City)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.City)
        @Html.ValidationMessageFor(model => model.City)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.IsUKResident)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.IsUKResident)
        @Html.ValidationMessageFor(model => model.IsUKResident)
    </div>
...
于 2012-09-05T20:17:52.373 に答える
0

This doesn't sound like it will be something easy to accomplish using ModelBinding and validation attributes.

A straight forward way to do it could be that you leave the validation attribute as is and simply validate if its a USA or Canadian postcode. Then when it gets to the server, do some manual validation.

E.g.

[Post]
public ActionResult SaveInfo(MyViewModel viewModel)
{
    var isValid = true;
    if (ModelState.IsValid)
    {
        if (!IsValidPostCode(viewModel))
        {
            isValid = false;
            ModelState.AddModelError("BillingPostalCode", "The billing postcode appears to be invalid.");
        }

        if (isValid)
        {
            return RedirectToAction("success");
        }
    }

    return View(viewModel);
}

private static IDictionary<string, string> countryPostCodeRegex = new Dictionary<string, string>
    {
        { "USA", "USAPostCodeRegex" },
        { "Canada", "CanadianPostCodeRegex" },
    }

private bool IsValidPostCode(MyViewModel viewModel)
{
    var regexString = countryPostCodeRegex[viewModel.SelectedCountry];
    var regexMatch = Regex.Match(viewModel.BillingPostalCode, regexString, RegexOptions.IgnoreCase);

    return regexMatch.Success;
}
于 2012-09-05T20:19:52.737 に答える
0

プロジェクトの 1 つでMVC のフールプルーフ検証を使用しました。フィールドの非表示と表示で言ったように行いますが、検証は同じロジックに従います。郵便番号用に 2 つのフィールドがあります。1 つはカナダ用、もう 1 つは米国用です。適切な形式であることを確認するために、それぞれに独自の正規表現検証があります。

疑似コード

[RequiredIf(DependentName = "Country", DependentValue="USA")]
public string PostalCodeUSA { get; set; }

[RequiredIf(DependentName = "Country", DependentValue="Canada")]
public string PostalCodeCanada { get; set; }
于 2012-09-05T20:31:24.490 に答える