开发者

Dijit validator question within dynamically created validationbox , please help

I'm now trying to do a dynamically created table line , with dijit button, validation box.

The button id and textbox id is generate by index number, so how can my Validation box validator can know which line of validationbox is calling the validator?

Should I make the validator : testcall , being validator : 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>

However I've try to make it as javascript function call to my validator , but I found in the form submittion , using form.isValid() to verify all information can pass validation, it always returns fail !

Here are my validator :

    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;

    }


You can do:

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. you need to pass the id to your testcall() function, but also you need to explicitly override the validator property of the widget.

See this in the Dojo reference guide.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜