Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to check is all editor value in dgrid are valid?? #196

Open
itdpong opened this issue Jun 9, 2012 · 7 comments
Open

how to check is all editor value in dgrid are valid?? #196

itdpong opened this issue Jun 9, 2012 · 7 comments

Comments

@itdpong
Copy link

itdpong commented Jun 9, 2012

I put a dgrid inside a form. When user click submit button the dgrid.save() will be trigger.
I know that the dgrid.save() function will ignore all the invalid value in editor.
But what I want is when user click submit, if there are any editor's value is invalid then the submission will stop and warn the user.
So are there any dgrid.isValid() function?

@myersware
Copy link

None that I know of. What I did was something like this after saving object ids of new rows:
for (var np in newPersons) {
var newPerson = personGrid.row(newPersons[np]).data;
var sd = newPerson.start_date;
if (sd == null) {
// check exist in dirty hash
var itemhash = personGrid.dirty[newPerson.id];
if (itemhash) sd = itemhash.start_date;
}
if (sd == null) {
alert('New person must have start date.');
return false;
}
}

@tsemachh
Copy link

I had same issue I ended up Wrapping the dgrid as Widget and added the next APIs:
validateBuffer:function(value){
return true;
},
validate:function(){
var widValid=this.validateInnerWidgets();
if (widValid)
{
widValid= this.validateBuffer(this.get('value'));
}

    return widValid;        
},
validateInnerWidgets: function(){
    // summary:
    //      returns if the form is valid - same as isValid - but
    //      provides a few additional (ui-specific) features.
    //      1 - it will highlight any sub-widgets that are not
    //          valid
    //      2 - it will call focus() on the first invalid
    //          sub-widget
    this.didFocus = false;
    var self=this;
    return array.every(array.map(this._getDescendantFormWidgets(), function(widget){
        // Need to set this so that "required" widgets get their
        // state set.
        widget._hasBeenBlurred = true;
        var valid = widget.disabled || !widget.validate || widget.validate();
        if(!valid && !self.didFocus){
            //setTimeout(dojo.hitch(this,"lateFocus",widget),0);
            self.lateFocus(widget);
            // Set focus of the first non-valid widget
        }
        return valid;
    }), function(item){ return item; });
},
focusedWidget:null,
didFocus:false,
focus:function(){
    if (this.didFocus){
        if (this.focusedWidget != null && this.focusedWidget.focus){                
            this.focusedWidget.focus();
            this.focusedWidget=null;
            return;
        }               
        this.didFocus=false;
    }
    this.inherited(arguments);
},
lateFocus:function(widget){
    winUtils.scrollIntoView(widget.containerNode || widget.domNode);
    if (widget.focus && this.focusedWidget == null){
        widget.focus(); 
        this.focusedWidget=widget;
        this.didFocus = true;
    }               
},

@jeff-ph
Copy link

jeff-ph commented Jan 10, 2013

I had a similar issue where I wanted my encapsulating dijit.form.Form to be able to validate my widgets within a dgrid when calling (for example) myDijitForm.validate() so that the widgets within the dgrid will get the focus/tooltip if they are the first invalid element in the form.

Here was my solution (using Dojo 1.7 with dgrid 0.3.5-dev):

  • Create the dgrid within a custom templated widget. The template itself just needs to have:
<div data-dojo-attach-point="containerNode">
    <div data-dojo-attach-point="dgrid"></div>
</div>
  • Defining the containerNode is necessary so that the encapsulating form will know that it should be looking for children in your custom widget when doing validation.
  • In the postCreate function of your custom widget, create/define your dgrid and attach it to "dgrid" from your template.
  • In your custom widget, also define a getChildren function; the default getChildren function would only return your dgrid, but none of its inner widgets, which is exactly what we want. My approach was to loop through the store attached to the grid to pull the specific cell that contains my widget using the row id and add it to an array of children:
getChildren: function() {
    var children = [];
    var myWidgetCell = null;

    array.forEach( this.store.query({}), lang.hitch( this, function( data ) {
        myWidgetCell = this.grid.cell( data.id, 'columnName' );
        children.push( myWidgetCell.element.widget );
    } ) );

    return children;
}
  • After placing your custom widget in your page, calling myDijitForm.validate() then results in your widget's children being seen as descendants of the form and are included in validation.

Sorry for the lack of comprehensive code samples. There is probably a more complete/concise way to do this, but I fought with this for a couple days. I definitely think dgrid widgets should be accessible to encapsulating forms by default, but hopefully this helps anyone having similar issues!

@tsemachh
Copy link

This direction can benefit next issue I checked:
http://dojo-toolkit.33424.n3.nabble.com/dgrid-validation-when-editor-editOn-is-used-td3990773.html
We just need access to the Widget used and validate each of the cell values with their relevant Widget.
BTW:We are examining now dgrid vs. gridx which has interesting editing capabilities.

@cwocwo
Copy link

cwocwo commented Jan 17, 2013

validate: function() {
    var grid = this;
    var dirty = grid.dirty;
    var store = grid.store;
    var colsMap = {};
    var cols = this.columns;
    for(colId in cols) {
        var col = cols[colId];
        if(col.field) {
            colsMap[col.field] = {col: col, colId: colId};
        }
    }
    var datas = grid.store.query(grid.get("query"), grid.get("queryOptions"));
    var isValid = true;
    for(var i=0; i<datas.length; i++) {
        var data = datas[i];
        var id = store.getIdentity(data);
        for(field in colsMap) {
            var colDef = colsMap[field];
            var cell = grid.cell(id, colDef.colId);
            var value = (grid.dirty && grid.dirty[id] && grid.dirty[id][field]) || data[field];
            var column = cell.column;
            if(column.canEdit && column.canEdit(data, value)) {
                //shared editorInstance, editor TODO
                var editor = column.editorInstance;
                if(editor && editor.get) {
                    var oldValue = editor.get("value");
                    editor.set("value", value);
                    //check
                    var isCellValid = editor.validate();
                    if(!isCellValid) {
                        grid._inValidCell(cell.element);
                        isValid = false;
                    }
                    editor.set("value", oldValue);
                }
            }
        }
    }
    return isValid;
}

@MYZ6
Copy link

MYZ6 commented Sep 10, 2014

In the above reference, I find another way that works for me after countless trials.

......
        var cell = grid.cell(rowId, colDef.colId);
        var valid = cell.element.widget.isValid();
        if (!valid) {
            TODO:
        }
......

element is a useful object

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants