2013-03-26 41 views
0

比較,並希望比較它們的信息被放進形式。截至目前我已經被宣佈的價值觀和他們驗證如何將價值觀的形成對驗證中我已經設置了驗證規則的典範模型

Ext.define('FirstApp.view.ReviewsContainer', { 
    extend: 'Ext.NavigationView', 
    xtype: 'reviewscontainer', 


    config: { 

     title: 'Reviews', 
     iconCls: 'compose', 
     items: [{ 
       xtype: 'reviews' 

      }, { 
       xtype: 'button', 
       align: 'right', 
       ui: 'confirm', 
       text: 'Leave Review', 
       docked: 'bottom', 
       centered: 'true', 

       handler: function() { 
        if (!this.overlay) { 
         this.overlay = Ext.Viewport.add({ 
          xtype: 'formpanel', 
          requires: ['FirstApp.model.ReviewCheck'], 
          id: 'reviewed', 
          modal: true, 
          hideOnMaskTap: true, 
          showAnimation: { 
           type: 'popIn', 
           duration: 250, 
           easing: 'ease-out' 
          }, 
          hideAnimation: { 
           type: 'popOut', 
           duration: 250, 
           easing: 'ease-out' 
          }, 
          centered: true, 
          width: Ext.os.deviceType == 'Phone' ? 260 : 400, 
          height: Ext.os.deviceType == 'Phone' ? 220 : 400, 
          styleHtmlContent: true, 

          items: [{ 
            docked: 'top', 
            xtype: 'toolbar', 
            title: 'Leave Review' 
           }, 

           { 
            xtype: 'fieldset', 

            title: 'Reviews', 
            items: [{ 
              xtype: 'textfield', 
              name: 'name', 
              label: 'Name', 
              placeHolder: 'name', 
              id: 'text' 
             }, { 
              xtype: 'textfield', 
              name: 'business', 
              label: 'Place' 
             }, { 
              xtype: 'textfield', 
              name: 'rating', 
              label: 'Rating' 
             }, { 
              xtype: 'textareafield', 
              name: 'review', 
              label: 'Review' 
             } 
            ] 
           }, { 
            items: [{ 
              xtype: 'button', 
              text: 'Submit', 
              ui: 'confirm', 
              handler: function() { 

               console.log("Hello"); 
               //var values = Ext.getCmp('reviewed').getValues(); 

               // var text= Ext.getCmp('text').getValue(); 

               // var value = Ext.ComponentQuery.query('#text')[0].getValue(); 


               //var reviewCheckForm = this.getReviewCheck(); 
               //var currentForm = reviewCheckForm.getRecord(); 
               // var newValues = reviewed.getValues(); 

               // currentForm.set("name", newValues.name); 
               // currentForm.set("business", newValues.business); 
               // currentForm.set("review", newValues.review); 


               var form = formPanel.getForm(); //Get the basicform instance 
               var record = form.getRecord(); //Get your record loaded in the form 
               form.updateRecord(record); //Update your record with the current form values 
               var errors = record.validate(); //Validate your record 



               // console.log(newValues); 
               // var data = Ext.create('FirstApp.model.ReviewCheck', { 
               //   name: 'dfhd', 
               //  business: 'sdfs', 
               //  id: 1, 
               // review: 'sfsd' 
               // }); 

               // var errors = data.validate(); 


               // var errors = currentForm.validate(); 
               console.log('Number of errors: ' + errors.getCount()); 




               if (!errors.isValid()) { 
                console.log('Number of errors: ' + errors.getCount()); 

                errors.each(function (item, index, length) { 
                 // Each item in the errors collection is an instance of the Ext.data.Error class. 
                 console.log('Field "' + item.getField() + '" ' + item.getMessage()); 
                }); 
                Ext.Msg.alert('Form is invalid!'); 
               } else { 

                var values = Ext.getCmp('reviewed').getValues(); 
                // prints the values filled in the form 
                // text fields of name, email and message.  
                console.log(values.name + "," + values.place + "," + values.rating); 


                Ext.Ajax.request({ 
                 url: 'http://insert.php', 
                 params: values, 
                 success: function (response) { 
                  var text = response.responseText; 
                  Ext.getCmp('reviewed').reset(); 
                  Ext.Msg.alert('Success', "Review successfully created.", Ext.getCmp('reviewed').hide(), Ext.StoreMgr.get('Reviews').load()); 
                  console.log("Im Here"); 
                 } 
                }); 
               } 
              } 
             } 
            ] 
           } 
          ], 
          scrollable: false 
         }); 
        } 

        this.overlay.show(); 
       } 
      } 

     ] 
    } 
}) 



Ext.define('FirstApp.model.ReviewCheck', { 
    extend: 'Ext.data.Model', 
    config: { 
     fields: [{ 
       name: 'name', 
       type: 'string' 
      }, { 
       name: 'business', 
       type: 'string' 
      }, { 
       name: 'id', 
       type: 'int' 
      }, { 
       name: 'review', 
       type: 'string' 
      } 
     ], // fields 
     validations: [{ 
       field: 'name', 
       type: 'presence', 
       error: 'Name must be present', 
       message: 'Name is required.' 
      }, { 
       field: 'business', 
       type: 'presence', 
       message: 'Place is required.' 
      }, { 
       field: 'review', 
       type: 'presence', 
       message: 'Review is required.' 

      } 
     ] // validations 
    } // config 
}); // define() 

如何設置名稱,經營,ID和審查值從表單

回答

0

首先採取讓您的視圖實例

var reviewCheckForm = this.getReviewCheck(); 

然後採取記錄從它

var currentForm = reviewCheckForm.getRecord(); 

然後從獲得新值同樣的形式

var newValues = reviewCheckForm.getValues(); 

然後設置新值的記錄,驗證它

currentForm.set("name", newValues.name); 
currentForm.set("business", newValues.business); 
currentForm.set("review", newValues.review); 

然後驗證它

var errors = currentForm.validate(); 

if (!errors.isValid()) { 
    Ext.Msg.alert('Wait!', errors.getByField("title")[0].getMessage(), Ext.emptyFn); 
    currentForm.reject(); 
    return; 
} 

你可以按照this簡單的教程,以獲得更多的understading和爲貴參考和here是模型驗證部分。

+0

您好感謝您的答覆,我曾經嘗試都回答和一千種不同的東西,但我覺得,所以我已經張貼在原有問題的所有代碼,看看如果是這樣的情況下,它可能是下到我的文件結構。 – paulpwr 2013-03-26 12:05:23

0

您可以使用formupdateRecord功能。 不要手動設置@Mayur在他的例子中顯示的所有屬性。

事情是這樣的:

{ 
    xtype: 'button', 
    text: 'Submit', 
    ui: 'confirm', 
    handler: function() { 
     var formPanel = this.up('formpanel'); //Get the formpanel 
     var form = formPanel.getForm(); //Get the basicform instance 
     var record = form.getRecord(); //Get your record loaded in the form 
     form.updateRecord(record); //Update your record with the current form values 
     var errors = record.validate(); //Validate your record 

     console.log('Number of errors: ' + errors.getCount()); 

     if (!errors.isValid()) { 
      console.log('Number of errors: ' + errors.getCount()); 

      errors.each(function (item, index, length) { 
       // Each item in the errors collection is an instance of the Ext.data.Error class. 
       console.log('Field "' + item.getField() + '" ' + item.getMessage()); 
      }); 
      Ext.Msg.alert('Form is invalid!'); 
     } 
     //... 
    } 
} 
+0

你好,謝謝你的答覆,我已經嘗試了答案和一千個不同的東西,但我認爲這可能歸結於我的文件結構,所以我已經在原始問題中發佈了所有代碼,以查看是否如此。 – paulpwr 2013-03-26 12:04:47

+0

你不能只使用formPanel。我編輯了我的代碼,用up導航到formPanel。 – 2013-03-26 13:02:58

+0

另外,您似乎並未在表單中首先加載記錄。隨着'loadRecord(記錄)'某處 – 2013-03-26 13:08:50

0

我有類似的問題... JSF + RichFaces的 基本上,我有多個標籤......像......(不是精確的代碼)

<rich:tab name="a"> 
    <inputText id="a1" values=""> 
    <inputText id="a2" values=""> 
</rich:tab> 

<rich:tab name="b"> 
    <inputText id="b1" values=""> 
    <inputText id="b2" values=""> 
</rich:tab> 

步驟 1)用戶首先將去標籤「a」和改變「A1」值說「NEWVALUE」 2)在不提交表單..去標籤「b」。 因爲,用戶沒有提交表單,用戶應該會收到警告消息說:「你有未保存的數據在標籤a」...

如何實現這一點?

謝謝..真的很感激。