2012-05-30 21 views
2
正常工作

我在煎茶新,所以bascially我在驗證 讓問題在這裏是我的代碼驗證是不是在煎茶

Ext.define("PlayListApp.view.PlayEditor", { 
    extend: "Ext.form.Panel", 
    requires: "Ext.form.FieldSet", 
    alias: "widget.playeditorview", 
    config:{ 
     scrollable: 'vertical', 
     items: [ 
      { 
       xtype: "toolbar", 
       docked: "top", 
       title: "Edit PlayList", 
       items: [ 
        { 
         xtype: "button", 
         ui: "action", 
         iconCls:"home", 
         iconMask:true, 
         itemId: "backButton" 
        }, 
        { xtype: "spacer" }, 
        { 
         xtype: "button", 
         ui: "action", 
         iconCls:"compose", 
         iconMask:true, 
         itemId: "saveButton" 
        } 
       ] 
      }, 
      { 
       xtype: "toolbar", 
       docked: "bottom", 
       items: [ 
        { 
         xtype: "button", 
         iconCls: "trash", 
         iconMask: true, 
         itemId: "deleteButton" 
        } 
       ] 
      }, 
      { xtype: "fieldset", 
       items: [ 
        { 
         xtype: 'textfield', 
         name: 'title', 
         label: 'Link', 
         placeHolder: 'http://yousite.com', 
         required: true, 

        }, 
        { 
         xtype: 'numberfield', 
         name: 'narrative', 
         label: 'Duration', 
         placeHolder:'99', 
         required:true 
        } 
       ] 


      } 
     ], 
listeners: [ 
      { 
       delegate: "#backButton", 
       event: "tap", 
       fn: "onBackButtonTap" 
      }, 
      { 
       delegate: "#saveButton", 
       event: "tap", 
       fn: "onSaveButtonTap" 
      }, 
      { 
       delegate: "#deleteButton", 
       event: "tap", 
       fn: "onDeleteButtonTap" 
      }, 


     ] 
    }, 

    onSaveButtonTap: function() { 
     //console.log("saveNoteCommand"); 
     this.fireEvent("saveNoteCommand", this); 
    }, 
    onDeleteButtonTap: function() { 
     //console.log("deleteNoteCommand"); 
     this.fireEvent("deleteNoteCommand", this); 
    }, 
    onBackButtonTap: function() { 
     //console.log("backToHomeCommand"); 
     this.fireEvent("backToHomeCommand", this); 
    } 

}); 

我想驗證這兩個標題和敘述,但問題是,只有標題當我點擊保存按鈕時沒有爲敘述分配任何值然後在沒有檢查敘述的驗證條件的情況下保存按鈕正常工作。

Ext.define("PlayListApp.model.Play", { 
    extend: "Ext.data.Model", 
    config: { 
     idProperty: 'id', 
     fields: [ 
      { name: 'title', type: 'string' }, 
      { name: 'narrative', type: 'int'} 
     ], 
     validations: [ 
      { type: 'presence', field: 'title', message: 'Please enter a link in playlist.' },//This validation only works 
      { type: 'presence', field: 'narrative', message: 'Please enter duration in playlist'}, 
      { type: 'length', field:'narrative', min:'1', max:'3', message:'Please enter digit between 1 and 3'} 
     ] 
    } 
}); 

這裏下面我檢查每個字段

 
Ext.define("PlayListApp.controller.Plays", { 

    extend: "Ext.app.Controller", 
    config: { 
     refs: { 
      // We're going to lookup our views by xtype. 
      notesListView: "playslistview", 
      noteEditorView: "playeditorview", 
      notesList: "#notesList" 
     }, 
     control: { 
      notesListView: { 
       // The commands fired by the notes list container. 
       newNoteCommand: "onNewNoteCommand", 
       editNoteCommand: "onEditNoteCommand" 
      }, 
      noteEditorView: { 
     // The commands fired by the note editor. 
       saveNoteCommand: "onSaveNoteCommand", 
       deleteNoteCommand: "onDeleteNoteCommand", 
       backToHomeCommand: "onBackToHomeCommand" 
     } 
     } 
    }, 

    onSaveNoteCommand: function() { 

     //console.log("onSaveNoteCommand"); 

     var noteEditor = this.getNoteEditorView(); 

     var currentNote = noteEditor.getRecord(); 
     var newValues = noteEditor.getValues(); 

     // Update the current note's fields with form values. 
     currentNote.set("title", newValues.title); 
     currentNote.set("narrative", newValues.narrative); 

     var errors = currentNote.validate(); 
     msg=''; 
     if (!errors.isValid()) { 
      //Ext.Msg.alert('Wait!','Please fill all the fields',Ext.emptyFn); 
      //Ext.Msg.alert('Wait!', errors.getByField("title")[0].getMessage(), Ext.emptyFn); 
      errors.each(function (err) { 
              msg += err.getMessage() + '
'; }); // each() Ext.Msg.alert('ERROR!', msg); currentNote.reject(); return; } var notesStore = Ext.getStore("Notes"); //notesStore.sync(); //notesStore.sort([{ property: 'dateCreated', direction: 'DESC'}]); this.activateNotesList(); },
+0

我把這個在SenchaFiddle(重命名你的控制器)。稍後將嘗試看看:http://www.senchafiddle.com/#KIo66 – 2012-05-30 22:26:07

+0

@ M-x謝謝隊友! – Arpit

+0

@ M-x所以你找到了解決辦法? – Arpit

回答

0

在PlayController的驗證,onSaveNoteCommand被調用,但我得到一個空值currentNote,這是造成應用程序死亡。

 
    var currentNote = noteEditor.getRecord(); // Always returns null !? 

我給Sencha Fiddle加了個警報。 noteEditor有一個值。

+0

代替那個我應該增加什麼來獲得一些價值? – Arpit

+0

其實我從if(!errors.isValid())得到一個錯誤(如果我使用數字字段,如果代替,如果我使用textfield然後顯示所有錯誤意味着數字字段的錯誤,如果我使用文本字段,然後匹配器沒有如果我在type =「format」中使用以顯示錯誤類型 – Arpit

+0

我沒有validate()函數,最大和最小支持最小支持。我只有部分項目? – 2012-05-31 14:23:22

1

我已經對您的問題做了一些工作,但在「PlayListApp.controller.Plays」中添加了自定義驗證。 現在「敘述」字段的長度不會超過3位數。 用戶也不能輸入負數。

你必須要在兩個文件中的變化: -

1) 「PlayListApp.model.Play」
2) 「PlayListApp.controller.Plays」

// "PlayListApp.model.Play" 

    Ext.define("PlayListApp.model.Play", { 
extend: "Ext.data.Model", 
config: { 
    idProperty: 'id', 
    fields: [ 
     { name: 'title', type: 'string' }, 
     { name: 'narrative', type: 'int'} 
    ], 
    validations: [ 
     { type: 'presence', field: 'title', message: 'Please enter a link in playlist.' },//This validation only works 
     { type: 'presence', field: 'narrative', message: 'Please enter duration in playlist'}, 
     { type: 'presence', field: 'narrative', message: 'Enter a number for this note.' } 
    ] 
} 
}); 

這裏是控制器。 js - 「PlayListApp.controller.Plays」

Ext.define("PlayListApp.controller.Plays", { 

extend: "Ext.app.Controller", 
config: { 
    refs: { 
     // We're going to lookup our views by xtype. 
     notesListView: "playslistview", 
     noteEditorView: "playeditorview", 
     notesList: "#notesList" 
    }, 
    control: { 
     notesListView: { 
      // The commands fired by the notes list container. 
      newNoteCommand: "onNewNoteCommand", 
      editNoteCommand: "onEditNoteCommand" 
     }, 
     noteEditorView: { 
    // The commands fired by the note editor. 
      saveNoteCommand: "onSaveNoteCommand", 
      deleteNoteCommand: "onDeleteNoteCommand", 
      backToHomeCommand: "onBackToHomeCommand" 
    } 
    } 
}, 

onSaveNoteCommand: function() { 

    //console.log("onSaveNoteCommand"); 

    var noteEditor = this.getNoteEditorView(); 

    var currentNote = noteEditor.getRecord(); 
    var newValues = noteEditor.getValues(); 

    currentNote.set("title",newValues.title); 
    currentNote.set("narrative", newValues.narrative); 
    var errors = currentNote.validate(); 

    if (!errors.isValid()) { 
     //Ext.Msg.alert('', errors.getByField("title")[0].getMessage(), Ext.emptyFn); 
     errors.each(function (err) { 
             msg += err.getMessage() + '<br>'; 
            }); // each() 
            Ext.Msg.alert('ERROR!', msg); 
     currentNote.reject(); 
     return; 
    } 

     // checking for negative number 
    if(newValues.narrative<="0") 
    { 
     Ext.Msg.alert('','Choose number greater than 0'); 
     currentNote.reject(); 
     return; 
    } 
     // checking for value greater than 3-digit number 
    if(newValues.narrative>"999") 
    { 
     Ext.Msg.alert('','Length should not be greater than THREE'); 
     currentNote.reject(); 
     return; 
    } 


    var notesStore = Ext.getStore("Notes"); 

    //notesStore.sync(); 

    //notesStore.sort([{ property: 'dateCreated', direction: 'DESC'}]); 

    this.activateNotesList(); 
}, 

希望這對你有所幫助。
如果這不是您想要的,請再次解釋您的要求。
再見。