1

謝罪、それはおそらく本当に馬鹿だが、私はこれを解決できない。jQuery-validateカスタムルールによって他の無効なフィールドが無視される

私はC#の、ASP.NET MVC 5を使用していますし、私のモデルでは、次のしている:ブラウザで

/// <summary> 
    /// A none-nullable datetime representing the start of the meeting. 
    /// </summary> 
    [Display(Name = "Date")] 
    [UIHint("Date")] 
    public System.DateTime Start { get; set; } 

    /// <summary> 
    /// The time aspect of the Meeting Start is split off from the date 
    /// portion. This property allows us to correctly display a separate 
    /// date and time but uses the same underlying property. 
    /// </summary> 
    [Display(Name = "Time")] 
    [UIHint("Time")] 
    public System.DateTime StartTime 
    { 
     get 
     { 
      return Start; 
     } 
     set 
     { 
      Start = System.DateTime.ParseExact(Start.ToString("dd/MM/yyyy ") + value.ToString("HH:mm"), "dd/MM/yyyy HH:mm", System.Globalization.CultureInfo.InvariantCulture); 

     } 
    } 

    /// <summary> 
    /// The end time of the meeting. 
    /// Since the system does not allow meetings to span multiple days this 
    /// will be married up with the date part of Start when we save. 
    /// </summary> 
    [Display(Name = "Planned finish")] 
    [UIHint("Time")] 
    public System.DateTime Finish { get; set; } 

    /// <summary> 
    /// Set after the meeting to display actual start time. 
    /// </summary> 
    [Display(Name = "Started")] 
    [UIHint("Time")] 
    public System.DateTime Started { get; set; } 

    /// <summary> 
    /// Set after the meeting to display actual finished time. 
    /// </summary> 
    [Display(Name = "Finished")] 
    [UIHint("Time")] 
    public System.DateTime Finished { get; set; } 

    [Required] 
    [Display(Name ="Primary contact")] 
    public string Contact { get; set; } 

    [Required] 
    [Display(Name = "Secondary contact")] 
    public string Contact2 { get; set; } 

これは両方の接点と正常に動作します(どちらも持っていない限り、つまり、フォームが送信されません。値)。jquery-validate.jsライブラリとmicrosoft jquery-validate-unobtrusive.jsの両方が含まれているので、これらはうまく動作しているようです。

私の問題の一部は、基本的にDevExpressの日付時間ピッカーを作成するカスタムエディタテンプレートを使用する(UIHint属性に気づいた)日付と時刻のピッカーです(後で便宜のためにピッカーが日付や時間の入力(日付や時刻の入力)これは問題なく、必要なものなどの検証プロパティは正常に機能しますが、日付と時刻の入力とみなされるため、 (yyyy-MM-ddの形式を受け入れるだけの日付の例を取っ​​てみましょう。ここでは、この例を次の例にしてみましょう:Custom date format with jQuery validation pluginと同様のものを作っています。

私はこれをすべて敷きました別のjsファイルに出力します両方の検証ライブラリ(バンドルを使用)。これはすべて概念的には大丈夫です(このガイダンスはhttp://www.devtrends.co.uk/blog/the-complete-guide-to-validation-in-asp.net-mvc-3-part-2から得ました)。しかし、ここでは物事は少しトリッキーになります。私CustomValidators.js明確にするため

が含まれています

(function ($) { 
$.validator.addMethod("date", function (value, element, params) { 
    if (!this.optional(element)) { 
     var bits = value.match(/([0-9]+)/gi), str; 
     if (!bits) 
      return this.optional(element) || false; 
     str = bits[1] + '/' + bits[0] + '/' + bits[2]; 
     alert("testing date " + value); 
     return this.optional(element) || !/Invalid|NaN/.test(new Date(str)); 
    } 
    return true; 
}); 
$.validator.unobtrusive.adapters.addBool("date"); 

$.validator.addMethod('time', function (value, element) { 
    value = value.trim(); 
    alert("Testing time: " + value); 
    if (value.length != 5 || value.indexOf(":") == -1) { 
     return false; 
    } 
    var parts = value.split(":"); 
    var hour = ParseInt(parts[0]); 
    var minutes = ParseInt(parts[1]); 
    if (isNaN(hour) || isNaN(minutes)) { 
     return false; 
    } 
    if (hour < 0 || hour > 23) { 
     return false; 
    } 
    if (minutes < 0 || minutes > 59) { 
     return false; 
    } 
    return this.optional(element) || true; 
}); 

$.validator.unobtrusive.adapters.addBool("time"); 

を}(jQueryの));

**問題**

(falseを返すと、私に無効な日付についての素敵な検証の概要警告を与える)、それは標準の日時規則を使用して検証しようとdatetimeオブジェクトとしてDevExpress社の入力が登録しているため。私は別のルールを持っているDevExpress社、日付とDevExpress社-時間特性を持つ任意の入力をバインドするために$(ドキュメント).ready()で試してみましたが、この問題を回避するには

:申し訳ありません

 $(document).ready(function() { 
     if ($("form").length > 0) 
     { 
      $("form").each(function() { 
       $(this).validate(); 
       $(".devexpress-date input").each(function() { 
        $(this).addClass("devexpress-date"); 
        $(this).rules("add", "date"); 
       }); 
      }); 

      $("form").each(function() { 
       $(this).validate(); 
       $(".devexpress-time input").each(function() { 
        $(this).addClass("devexpress-time"); 
        $(this).rules("add", "time"); 
        $(this).rules("remove", "date"); 
       }); 
      }); 
      } 
     } 
  • を、私は私が手フォームを送信しようとすると、そう、私のバリデータ機能で、いくつかの警告を気づくでしょう

*私は個々の入力にルールを追加するためのソースを持っていたが、私は今それを見つけることができないと考えている。

しようと予定日:2017年8月4日
しようとすると時間:午後12時00

私は連絡先やContact2フィールドはフォーム空白になっているページでもあれば、他の3つのタイムボックスの警告を得ることはありません罰金を科す。それは私がこれらの要素の後で検証を壊したようですが、私は理由を理解していません。

なぜ私に説明することができますか?アプリケーションには数字、テキスト、日付、時刻の入力がすべて同様のルールを持つため、理想的な解決策はカスタムルールが機能するようにして、form.ready()にフォームとそれは基本的に自動的に動作するためです。私は近くにいると思ったが、他のバリデーションがうまくいかないという事実は、私が私の意図から離れているように感じるので、すべての助けに感謝する。

ありがとうございました。

答えて

0

私はこれをバージョン1.14で実行しました。 jquery.validate.jsを1.17に、jquery.validate.unobtrusive.jsを3.2.6にアップグレードして問題を修正しました。

関連する問題