2012-03-15 126 views
2

當我在我的字段中輸入「31/12/2012」(日期格式爲MM/DD/YYYY)時,它將該日期更改爲「7/12/2014」。我寧願錯誤與「無效」的錯誤信息。ExtJS解析日期錯誤

我繼承了這個代碼從以前開發商:

function dateRangeCheck(val, field) { 
    field.vtypeText = ''; 

    var date = field.parseDate(val); 
    if (!date) { 
     field.vtypeText = val + ' is not a valid date - it must be in the format (MM/DD/YYYY).'; 
     return false; 
    } 

    var retVal = true; 

    if (field.fromField) { 
     var fromField = Ext.getCmp(field.fromField); 
     var fromDate = fromField.parseDate(fromField.getValue()); 
     // If we don't have a fromDate to validate with then return true 
     if (!fromDate) 
      return true; 

     retVal = (date >= fromDate); 

     if (retVal) 
      fromField.clearInvalid(); 
    } 
    else if (field.toField) { 
     var toField = Ext.getCmp(field.toField); 
     var toDate = toField.parseDate(toField.getValue()); 
     // If we don't have a toDate to validate with then return true 
     if (!toDate) 
      return true; 

     retVal = (date <= toDate); 

     if (retVal) 
      toField.clearInvalid(); 
    } 

    if (!retVal) { 
     field.vtypeText = 'From Date must be less than or equal to To Date.'; 
    } 
    return retVal; 
} 

當我嘗試,我鍵入一個在外地「3」,它拋出一個JS儘快使用默認的「日期範圍」 V型,運行時異常'對象不支持此屬性或方法'。

+0

你使用ExtJS4嗎?使用Ext.form.field.Date組件? – 2012-03-15 20:12:32

+0

我不確定。我能找到的ExtJS版本的唯一參考是3.0 RC 1.1。 – michaelkoss 2012-03-15 21:41:45

+0

您可以通過鍵入'Ext.version ||獲得版本Ext.getVersion()。version'到加載Ext的控制檯中 – 2012-03-17 17:25:40

回答

1

看起來您撥打電話parseDate只需要設置strict switch

strict(可選)真驗證日期字符串在解析(即 防止JavaScript的日期 「翻轉」)(默認爲false)。日期無效 字符串在分析時將返回空值。

> Date.parseDate('31/12/2012','m/d/Y') 
    Sat Jul 12 2014 00:00:00 GMT-0500 (Central Daylight Time) 
> Date.parseDate('31/12/2012','m/d/Y', true) 
    null 

中的DateField的parseDate方法是私有的和無證,和discussion允許在ExtJS的3.X嚴格日期解析從未孔任何水果。我認爲你最好的選擇是使用重寫來允許嚴格的日期解析。

// before you use your DateFields 
Ext.override(Ext.form.DateField, { 
    safeParse : function(value, format) { 
     if (Date.formatContainsHourInfo(format)) { 
      // if parse format contains hour information, no DST adjustment is necessary 
      return Date.parseDate(value, format, this.strict); 
     } else { 
      // set time to 12 noon, then clear the time 
      var parsedDate = Date.parseDate(value + ' ' + this.initTime, format + ' ' + this.initTimeFormat, this.strict); 

      if (parsedDate) { 
       return parsedDate.clearTime(); 
      } 
     } 
    } 
}); 

//... and in your DateField config: 
strict: true, 
+0

我在ext-all-debug.js中找到了parseDate的定義。該文件的標題說:「Ext JS Library 3.0 RC 1.1」。該定義在6051行。我對javascript並不是很好,並且有很多對其他函數和屬性的引用。我試圖追蹤它。 – michaelkoss 2012-03-15 21:28:32

+0

我只是查看了'Ext.form.DateField'代碼,並且*是一個'parseDate'方法,它只是私有的,沒有記錄。我認爲你需要用重寫來處理。我會盡快更新我的答案。 – wes 2012-03-16 05:18:29

2

請注意,您可以設置Date.useStrict = true全局和的DateField將使用默認。

enter image description here

對於擴展4+這將是Ext.Date.useStrict = true代替。