2

私はSenchaを初めて使用するので、基本的に検証で問題が発生します。これが私のコードです

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"、{

    拡張: "Ext.app.Controller"、
    構成:{
        参照:{
            //ビューをxtypeで検索します。
            notesListView: "playslistview"、
            noteEditorView: "playeditorview"、
            notesList: "#notesList"
        }、
        コントロール: {
            notesListView:{
                //ノートリストコンテナによって起動されるコマンド。
                newNoteCommand: "onNewNoteCommand"、
                editNoteCommand:"onEditNoteCommand"
            }、
            noteEditorView:{
        //ノートエディタによって起動されるコマンド。
                 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();
        msg ='';
        if(!errors.isValid()){
            //Ext.Msg.alert('Wait!'、'すべてのフィールドに入力してください'、Ext.emptyFn);
            //Ext.Msg.alert('Wait!'、errors.getByField( "title")[0] .getMessage()、Ext.emptyFn);
            errors.each(function(err){
                                            msg + = err.getMessage()+' 
'; }); // 各() Ext.Msg.alert('ERROR!'、msg); currentNote.reject(); 戻る; } var notesStore = Ext.getStore( "Notes"); //notesStore.sync(); //notesStore.sort([{property:'dateCreated'、direction:'DESC'}]); this.activateNotesList(); }、
4

2 に答える 2

1

私はあなたの問題に対していくつかの作業を行いましたが、「PlayListApp.controller.Plays」にカスタム検証を追加しました。これで、「物語」フィールドの長さは 3 桁を超えなくなりました。また、ユーザーは負の数を入力できません。

2 つのファイルを変更する必要があります:-

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.' }
    ]
}
});

これがcontroller.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();
},

これがお役に立てば幸いです。
これがあなたが望んでいたものではない場合は、要件をもう一度説明してください。
さよなら。

于 2012-09-15T06:44:08.640 に答える
0

PlayController では、onSaveNoteCommandが呼び出されますが、 currentNote の null 値を取得しているためアプリが停止します。

  var currentNote = noteEditor.getRecord(); // 常に null を返す!?

Sencha Fiddleにアラートを追加しました。 noteEditorには値があります。

于 2012-05-31T13:41:32.033 に答える