ValidateData

class webix.ValidateData()

Validatedata mixin

References

helpers
assert(), rules.

Referenced by

components
DataProcessor().
views
toolbar().

External references

Official documentation page.

Code

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
webix.ValidateData = {
    $init:function(){
        if(this._events)
            this.attachEvent("onChange",this.clearValidation);
    },
    clearValidation:function(){
        if(this.elements){
            for(var id in this.elements){
                this._clear_invalid(id);
            }
        }
    },
    validate:function(mode, obj) {
        webix.assert(this.callEvent, "using validate for eventless object");

        this.callEvent("onBeforeValidate", []);
        var failed = this._validate_details = {};

        //optimistic by default :)
        var result =true;
        var rules = this._settings.rules;

        var isHidden = this.isVisible && !this.isVisible();
        var validateHidden = mode && mode.hidden;
        var validateDisabled = mode && mode.disabled;

        //prevent validation of hidden elements
        var elements = {}, hidden = {};
        for(var i in this.elements){
            var name = this.elements[i].config.name;
            //we are ignoring hidden and disabled fields during validation
            //if mode doesn not instruct us otherwise
            //if form itself is hidden, we can't separate hidden fiels,
            //so we will vaidate all fields
            if((isHidden || this.elements[i].isVisible() || validateHidden) && (this.elements[i].isEnabled() || validateDisabled))
                elements[name] = this.elements[i];
            else{
                hidden[name]=true;
            }
        }


        if (rules || elements)
            if(!obj && this.getValues)
                obj = this.getValues();

        if (rules){
            //complex rule, which may chcek all properties of object
            if (rules.$obj)
                result = this._validate(rules.$obj, obj, obj, "") && result;

            //all - applied to all fields
            var all = rules.$all;
            if (all)
                for (var key in obj){
                    if(hidden[key]) continue;
                    var subresult = this._validate(all, obj[key], obj, key);
                    if (!subresult)
                        failed[key] = true;
                    result =  subresult && result;
                }


            //per-field rules
            for (var key in rules){
                if(hidden[key]) continue;
                if (key.indexOf("$")!==0 && !failed[key]){
                    webix.assert(rules[key], "Invalid rule for:"+key);
                    var subresult = this._validate(rules[key], obj[key], obj, key);
                    if (!subresult)
                        failed[key] = true;
                    result = subresult && result;
                }
            }
        }

        //check personal validation rules
        if (elements){
            for (var key in elements){
                if (failed[key]) continue;

                var subview = elements[key];
                if (subview.validate){
                    var subresult = subview.validate();
                    result = subresult && result;
                    if (!subresult)
                        failed[key] = true;
                } else {
                    var input = subview._settings;
                    if (input){    //ignore non webix inputs
                        var validator = input.validate;
                        if (!validator && input.required)
                            validator = webix.rules.isNotEmpty;

                        if (validator){
                            var subresult = this._validate(validator, obj[key], obj, key);
                            if (!subresult)
                                failed[key] = true;
                            result = subresult && result;
                        }
                    }
                }
            }
        }

        this.callEvent("onAfterValidation", [result, this._validate_details]);
        return result;
    },
    _validate:function(rule, data, obj, key){
        if (typeof rule == "string")
            rule = webix.rules[rule];
        if (rule.call(this, data, obj, key)){
            if(this.callEvent("onValidationSuccess",[key, obj]) && this._clear_invalid)
                this._clear_invalid(key);
            return true;
        }
        else {
            if(this.callEvent("onValidationError",[key, obj]) && this._mark_invalid)
                this._mark_invalid(key);
        }
        return false;
    }
};