Results 1 to 2 of 2

Thread: Completely custom validation

  1. #1
    Ext Premium Member
    Join Date
    Oct 2009
    Posts
    20

    Default Completely custom validation

    Hi all,

    I want to use completely custom validator objects, because vtypes are too limited and I want to reuse the validators I use for other projects.

    Unfortunately customizing ExtJS to work with completely custom validators is a lot trickier than I had hoped. I did the following, which does work to a degree, but it sets the "valid" flag on the form field, which is apparently reused in a an editor grid (and perhaps in forms as well?), making it useless:

    PHP Code:
    Ext.override(Ext.form.Field, {
        
    validtrue,
        
    validationDelay 500,
        
    getValidator: function(){
            
            if(
    this.validator){
                return 
    this.validator;    
            }
            
            if(!
    this.vParams){
                return 
    null;
            }
            
            
    this.validator kwd.jquery.validation.factory(this.vParams);
            
            return 
    this.validator;
        },
        
    fireEvent: function(){ 
            
    //Stop any default action on valid/invalid
            
    if(arguments[0] === 'valid' && !this.valid){
                return 
    false;
            }
            if(
    arguments[0] === 'invalid' && this.valid) {
                return 
    false;
            }
            
            
    Ext.form.Field.superclass.fireEvent.apply(thisarguments);
        }
    });

    Ext.override(Ext.form.TextField, {

        
    validateValue: function(value){
            
            var 
    validator this.getValidator();
            
            if(!
    validator){
                return 
    true;
            }
            
            
    this.valid validator.validate(value);
            
            
    /**
             * Reparse the message template
             */
            
    validator.parseTemplate();
            
            if(!
    this.valid) {
                
    this.markInvalid(validator.message());
            }
            
            return 
    true//Stop any default action on invalid
        
    }
    }); 
    If I let validateValue() return false when invalid, the grid reverts the value. I want the invalid value to be used, and deal with the invalid value later, as for example with this grid plugin:

    PHP Code:
    kwd.ext.jquery.GridValidator = function(config) {

        
    this.focusInvalid true;
        
        
    Ext.apply(thisconfig);
        
        
    this.init = function(grid) {
            
            
    grid.addEvents(
                
    'valid''invalid'
            
    );
            
            if(
    this.focusInvalid){
                
    grid.on('invalid', function(colrow){
                    
                    
    grid.startEditing(rowcol);
                });
            }
            
            
    /**
             * Fires after editing a cell
             * 
             * @param {Object} event
             */
            
    grid.on('afteredit', function(event){
                
    /**
                 * Check the status of the whole form
                 */
                
    event.grid.isValid();
            });
            
            
    Ext.apply(grid, {
                
    /**
                 * Check if  the currently active part of the grid has no fields marked as invalid
                 * 
                 * @return {Boolean}
                 */
                
    isValid:function() 
                {
                    try {
                        var 
    colCount this.colModel.getColumnCount();
                        var 
    rowCount this.store.getCount();
                        var 
    rowcol;
                        
                        for (
    row 0row rowCount; ++row) {
                        
                            for (
    col 0col colCount; ++col
                            {
                                
    console.log("col " col " row " row);
                                
                                if (!
    this.colModel.isCellEditable(colrow)) {
                                    continue;
                                }
                                
                                var 
    editor this.colModel.getCellEditor(colrow);
                                
                                if (!
    editor) {
                                    continue;
                                }
                                
                                if (!
    editor.field.valid) {
                                    
                                    
    this.fireEvent('invalid'colrow);
                                    return 
    false;
                                }
                            }
                        }
                        
                        
    this.fireEvent('valid');
                        
                        return 
    true;
                    }
                    catch(
    e){
                        
    kwd.ext.triggerException(e);
                    }
                } 
            });
        };
    }; 
    This is when the problem became apparent: when a cell is invalid, it always brings focus to the first editor in the column: row is always 0. I am assuming this is because "editor" is actually the same object for all cells in a column, which makes sense but really screws my plans.

    Any ideas? Maybe set the valid flag on the record?

  2. #2
    Ext Premium Member
    Join Date
    Oct 2009
    Posts
    20

    Default

    *bump*

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •