2011-06-16 77 views
1

我現在正在嘗試使用dijit按鈕驗證框來動態創建表格行。動態創建的驗證框中的Dijit驗證器問題,請幫助

按鈕ID和文本框的id是索引號生成,因此,如何將我的驗證框驗證就可以知道哪個validationbox行是調用驗證?

我應使驗證器:TESTCALL,作爲驗證:功能(){TESTCALL(this.id)},????

function createNewRow() { 

    var tablebodyname = "RPDetailbody" ; 

    var mytablebody  = document.getElementById(tablebodyname); 
    var thetabletr  = document.createElement('tr'); 
    var thetabletd1  = document.createElement('td'); 
    var thetabletd2  = document.createElement('td'); 
    var thetabletd3  = document.createElement('td'); 

    var thisButton = new dijit.form.Button({ 
     label : thelineindex , 
     id : "I_del_butt"+thelineindex, 
     name : "I_del_butt"+thelineindex, 
     onClick : function() { 
        document.getElementById(tablebodyname).removeChild(thetabletr); 
        } 
     }).placeAt(thetabletd1) ; 

    var thisNumberTextBox = new dijit.form.NumberTextBox({ 
     id : "I_seq_no"+thelineindex , 
     name : "I_seq_no"+thelineindex , 
     value : thelineindex+1 , 
     required : "true", 
     maxLength : 2, 
     size : 2 , 
     style : "width: 2em;", 
     missingMessage : "Every line must have sequence number"}).placeAt(thetabletd2) ; 

    var thisValidationTextBox1 = new dijit.form.ValidationTextBox({ 
     id : "I_pig_code"+thelineindex , 
     name : "I_pig_code"+thelineindex , 
     type : "text" , 
     required : "true", 
     maxLength : 3, 
     size : 3, 
     style : "width: 5em;", 
     validator : testcall , 
     missingMessage : "Must have pigment code" }).placeAt(thetabletd3) ; 

    thetabletr.appendChild(thetabletd1); 
    thetabletr.appendChild(thetabletd2); 
    thetabletr.appendChild(thetabletd3); 

    mytablebody.appendChild(thetabletr); 

    thelineindex ++ ; 
} 

<tbody id="RPDetailbody"> 
     <td><div id="delplace"></div></td> 
     <td><div id="seqplace"></div></td> 
     <td><div id="pigcodeplace"></div></td> 
    </tr> 
    </tbody> 

但是我已經儘量做到爲JavaScript函數調用我的驗證,但我的形式submittion發現,使用form.isValid()來驗證所有信息可以通過驗證,它總是返回失敗!

這裏是我的驗證:

function testcall (thebtnID) { 

     var strlen = thebtnID.length ; 
     var resultAreaID = "I_pigment_name"+ thebtnID.substring(10, strlen) ; 

     var pigcode = dijit.byId(thebtnID); 
     var bNoNameFound = ("Error" == pigcode.get("state")) ? false:true; 
     var query = "thePig=" + encodeURI(pigcode.value); 

     if("" == pigcode.value) { 

     // for some required=true is not kicking in, so we are forcing it. 
      bNoNameFound = false; 
      pigcode._setStateClass(); 

     } else { 

      if(false == pigcode._focused) { 
       console.log("Checking Pigment..."); 
       dojo.xhrPost({ 
        url: 'ValidatePigmentInput.php', 
        handleAs: 'text', 
        postData: query , 
        load: function(responseText) { 
         if (responseText == "no record!") { 
          // setting the state to error since the pigment is already taken 
          bNoNameFound = false; 
          pigcode.set("state", "Error"); 
          //pigcode.displayMessage("The Pigment dosen't exist..."); 
          document.getElementById(resultAreaID).innerHTML = "The Pigment dosen't exist..."; 
          // used to change the style of the control to represent a error 
          pigcode._setStateClass(); 
         } else { 

          if (responseText == "pigment BANNED!") { 
           bNoNameFound = false; 
           pigcode.set("state", "Error"); 
           document.getElementById(resultAreaID).innerHTML = "Pigment BANNED! Please don't use it!"; 
           pigcode._setStateClass(); 
          } else { 
           bNoNameFound = true; 
           pigcode.set("state", ""); 
           document.getElementById(resultAreaID).innerHTML = responseText; 
           pigcode._setStateClass(); 

          } 
         } 
        }, 
        error: function(responseText) { 
         document.getElementById(resultAreaID).innerHTML = "Error"; 
        } 
       }); 
      } 
     } 

     return bNoNameFound; 

    } 
+0

我想你必須通過包裝函數顯式傳遞id,是的。但是你說'form.isValid()'總是返回false - 你確定這是因爲'testcall',還是可能是因爲窗體中的其他窗口小部件無效? – Frode 2011-06-16 13:08:59

+0

我明白了,isValid總是返回false,因爲我缺少pigcode._setStateClass();在bNoNameFound = true的部分; – Colacat 2011-06-17 02:19:10

回答

0

你可以這樣做:

var thisValidationTextBox1 = new dijit.form.ValidationTextBox({ 
    id: "I_pig_code" + thelineindex, 
    name: "I_pig_code" + thelineindex, 
    type: "text", 
    required: "true", 
    maxLength: 3, 
    size: 3, 
    style: "width: 5em;", 
    missingMessage: "Must have pigment code" 
}).placeAt(thetabletd3); 

thisValidationTextBox1.validator = function() { 
    return testcall(thisValidationTextBox1.id); 
}; 

IE瀏覽器。您需要將id傳遞給testcall()函數,但是您還需要明確覆蓋小部件的validator屬性。

參見this道場參考指南中。

+0

THX,這完全解決了我的問題。 – Colacat 2011-06-17 02:19:41

+0

此外,我發現還有另外一種方法: 我可以通過ID約束: ====================== function testcall(value,約束){ \t \t var thebtnID = constraints.id; ==================================== \t var thisValidationTextBox1 = new dijit.form.ValidationTextBox( { \t \t ID: 「I_pig_code」 + thelineindex, \t \t名稱: 「I_pig_code」 + thelineindex, \t \t類型: 「文本」, \t \t要求: 「真」, \t \t最大長度:3, \t \t尺寸:3, \t \t風格:「width:5em;」, 個\t \t約束:{ID: 「I_pig_code」 + thelineindex}, \t \t驗證:TESTCALL, \t \t missingMessage: 「必須有顏料碼」})placeAt(thetabletd3); – Colacat 2011-06-17 02:22:49