动态创建验证框内的Dijit验证器问题,请帮忙

时间:2011-06-16 08:48:14

标签: dojo validation

我现在正尝试使用dijit按钮验证框来动态创建表格行。

按钮ID和文本框ID是由索引号生成的,那么我的验证框验证器如何知道哪一行验证框正在调用验证器?

我应该制作验证器:testcall,是验证器:function(){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函数调用我的验证器,但我在表单提交中发现,使用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;

    }

1 个答案:

答案 0 :(得分: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属性。

请参阅Dojo参考指南中的this