sync
[roojs1] / Roo / bootstrap / CheckBox.js
index 08fcb71..66d9b5f 100644 (file)
@@ -39,7 +39,7 @@ Roo.bootstrap.CheckBox = function(config){
 };
 
 Roo.extend(Roo.bootstrap.CheckBox, Roo.bootstrap.Input,  {
-    
+  
     inputType: 'checkbox',
     inputValue: 1,
     valueOff: 0,
@@ -66,12 +66,20 @@ Roo.extend(Roo.bootstrap.CheckBox, Roo.bootstrap.Input,  {
             tag: 'input',
             id : id,
             type : this.inputType,
-            value : this.inputType == 'radio' ? this.inputValue : ((!this.checked) ? this.valueOff : this.inputValue),
+            value : this.inputValue,
             cls : 'roo-' + this.inputType, //'form-box',
             placeholder : this.placeholder || ''
             
         };
         
+         
+        var hidden =  {
+            tag: 'input',
+            type : 'hidden',
+            cls : 'roo-hidden-value',
+            value : this.checked ? this.valueOff : this.inputValue
+        };
+            
         if (this.weight) { // Validity check?
             cfg.cls += " " + this.inputType + "-" + this.weight;
         }
@@ -82,10 +90,13 @@ Roo.extend(Roo.bootstrap.CheckBox, Roo.bootstrap.Input,  {
         
         if(this.checked){
             input.checked = this.checked;
+            
         }
         
+        
         if (this.name) {
-            input.name = this.name;
+            hidden.name = this.name;
+            input.name = '_hidden_' + this.name;
         }
         
         if (this.size) {
@@ -100,7 +111,13 @@ Roo.extend(Roo.bootstrap.CheckBox, Roo.bootstrap.Input,  {
             }
         });
         
-        var inputblock = input;
+        var inputblock = {
+            tag: 'span',
+            cn : [
+                    input,
+                    hidden
+                ]
+        };
          
         if (this.before || this.after) {
             
@@ -118,6 +135,7 @@ Roo.extend(Roo.bootstrap.CheckBox, Roo.bootstrap.Input,  {
             }
             
             inputblock.cn.push(input);
+            inputblock.cn.push(hidden);
             
             if (this.after) {
                 inputblock.cn.push({
@@ -130,7 +148,7 @@ Roo.extend(Roo.bootstrap.CheckBox, Roo.bootstrap.Input,  {
         }
         
         if (align ==='left' && this.fieldLabel.length) {
-                Roo.log("left and has label");
+//                Roo.log("left and has label");
                 cfg.cn = [
                     
                     {
@@ -149,7 +167,7 @@ Roo.extend(Roo.bootstrap.CheckBox, Roo.bootstrap.Input,  {
                     
                 ];
         } else if ( this.fieldLabel.length) {
-                Roo.log(" label");
+//                Roo.log(" label");
                 cfg.cn = [
                    
                     {
@@ -167,18 +185,19 @@ Roo.extend(Roo.bootstrap.CheckBox, Roo.bootstrap.Input,  {
 
         } else {
             
-                Roo.log(" no label && no align");
+//                Roo.log(" no label && no align");
                 cfg.cn = [  inputblock ] ;
                 
                 
         }
+        
         if(this.boxLabel){
              var boxLabelCfg = {
                 tag: 'label',
                 //'for': id, // box label is handled by onclick - so no for...
                 cls: 'box-label',
                 html: this.boxLabel
-            }
+            };
             
             if(this.tooltip){
                 boxLabelCfg.tooltip = this.tooltip;
@@ -200,6 +219,10 @@ Roo.extend(Roo.bootstrap.CheckBox, Roo.bootstrap.Input,  {
     {
         return this.el.select('input.roo-' + this.inputType,true).first();
     },
+    hiddenEl: function ()
+    {
+        return this.el.select('input.hidden-value',true).first();
+    },
     
     labelEl: function()
     {
@@ -212,6 +235,11 @@ Roo.extend(Roo.bootstrap.CheckBox, Roo.bootstrap.Input,  {
         return this.labelEl();
     },
     
+    boxLabelEl: function()
+    {
+        return this.el.select('label.box-label',true).first();
+    },
+    
     initEvents : function()
     {
 //        Roo.bootstrap.CheckBox.superclass.initEvents.call(this);
@@ -252,6 +280,8 @@ Roo.extend(Roo.bootstrap.CheckBox, Roo.bootstrap.Input,  {
                 this.fireEvent('check', this, true);
             }
             
+            this.validate();
+            
             return;
         }
         
@@ -259,11 +289,14 @@ Roo.extend(Roo.bootstrap.CheckBox, Roo.bootstrap.Input,  {
         
         this.inputEl().dom.checked = state;
         
-        this.inputEl().dom.value = state ? this.inputValue : this.valueOff;
+        
+        this.hiddenEl().dom.value = state ? this.inputValue : this.valueOff;
         
         if(suppressEvent !== true){
             this.fireEvent('check', this, state);
         }
+        
+        this.validate();
     },
     
     getValue : function()
@@ -272,7 +305,7 @@ Roo.extend(Roo.bootstrap.CheckBox, Roo.bootstrap.Input,  {
             return this.getGroupValue();
         }
         
-        return this.inputEl().getValue();
+        return this.hiddenEl() ? this.hiddenEl().dom.value : this.value;
         
     },
     
@@ -289,11 +322,11 @@ Roo.extend(Roo.bootstrap.CheckBox, Roo.bootstrap.Input,  {
     {
         if(this.inputType == 'radio'){
             this.setGroupValue(v, suppressEvent);
-            this.validate();
             return;
         }
         
         this.setChecked(((typeof(v) == 'undefined') ? this.checked : (String(v) === String(this.inputValue))), suppressEvent);
+        
         this.validate();
     },
     
@@ -313,6 +346,8 @@ Roo.extend(Roo.bootstrap.CheckBox, Roo.bootstrap.Input,  {
             this.fireEvent('check', this, true);
         }
 
+        this.validate();
+        
         return;
     },
     
@@ -320,8 +355,8 @@ Roo.extend(Roo.bootstrap.CheckBox, Roo.bootstrap.Input,  {
     {
         if(
                 this.disabled || 
-                (this.inputType == 'radio' && this.getValue().length) ||
-                (this.inputType == 'checkbox' && this.validateGroup())
+                (this.inputType == 'radio' && this.validateRadio()) ||
+                (this.inputType == 'checkbox' && this.validateCheckbox())
         ){
             this.markValid();
             return true;
@@ -331,10 +366,31 @@ Roo.extend(Roo.bootstrap.CheckBox, Roo.bootstrap.Input,  {
         return false;
     },
     
-    validateGroup : function()
+    validateRadio : function()
+    {
+        if(this.allowBlank){
+            return true;
+        }
+        
+        var valid = false;
+        
+        Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
+            if(!e.dom.checked){
+                return;
+            }
+            
+            valid = true;
+            
+            return false;
+        });
+        
+        return valid;
+    },
+    
+    validateCheckbox : function()
     {
         if(!this.groupId){
-            return (this.getValue() == this.inputValue) ? true : false;
+            return (this.getValue() == this.inputValue || this.allowBlank) ? true : false;
         }
         
         var group = Roo.bootstrap.CheckBox.get(this.groupId);
@@ -359,35 +415,148 @@ Roo.extend(Roo.bootstrap.CheckBox, Roo.bootstrap.Input,  {
     /**
      * Mark this field as valid
      */
-    markValid : function(){
-        Roo.log('run marValid on checkbox !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
-        if(!this.el  || this.preventMark){ // not rendered
+    markValid : function()
+    {
+        if(this.allowBlank){
             return;
         }
         
-        this.el.removeClass([this.invalidClass, this.validClass]);
-        
-        this.el.addClass(this.validClass);
+        var _this = this;
         
         this.fireEvent('valid', this);
+        
+        var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
+        
+        if(this.groupId){
+            label = Roo.bootstrap.FieldLabel.get(this.groupId + '-group');
+        }
+        
+        if(label){
+            label.markValid();
+        }
+        
+        if(this.inputType == 'radio'){
+            Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
+                e.findParent('.form-group', false, true).removeClass([_this.invalidClass, _this.validClass]);
+                e.findParent('.form-group', false, true).addClass(_this.validClass);
+            });
+            
+            return;
+        }
+        
+        if(!this.groupId){
+            this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
+            this.el.findParent('.form-group', false, true).addClass(this.validClass);
+            return;
+        }
+        
+        var group = Roo.bootstrap.CheckBox.get(this.groupId);
+            
+        if(!group){
+            return;
+        }
+        
+        for(var i in group){
+            group[i].el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
+            group[i].el.findParent('.form-group', false, true).addClass(this.validClass);
+        }
     },
     
      /**
      * Mark this field as invalid
      * @param {String} msg The validation message
      */
-    markInvalid : function(msg){
-        if(!this.el  || this.preventMark){ // not rendered
+    markInvalid : function(msg)
+    {
+        if(this.allowBlank){
             return;
         }
         
-        this.el.removeClass([this.invalidClass, this.validClass]);
-        
-        this.el.addClass(this.invalidClass);
+        var _this = this;
         
         this.fireEvent('invalid', this, msg);
+        
+        var label = Roo.bootstrap.FieldLabel.get(this.name + '-group');
+        
+        if(this.groupId){
+            label = Roo.bootstrap.FieldLabel.get(this.groupId + '-group');
+        }
+        
+        if(label){
+            label.markInvalid();
+        }
+            
+        if(this.inputType == 'radio'){
+            Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
+                e.findParent('.form-group', false, true).removeClass([_this.invalidClass, _this.validClass]);
+                e.findParent('.form-group', false, true).addClass(_this.invalidClass);
+            });
+            
+            return;
+        }
+        
+        if(!this.groupId){
+            this.el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
+            this.el.findParent('.form-group', false, true).addClass(this.invalidClass);
+            return;
+        }
+        
+        var group = Roo.bootstrap.CheckBox.get(this.groupId);
+        
+        if(!group){
+            return;
+        }
+        
+        for(var i in group){
+            group[i].el.findParent('.form-group', false, true).removeClass([this.invalidClass, this.validClass]);
+            group[i].el.findParent('.form-group', false, true).addClass(this.invalidClass);
+        }
+        
+    },
+    
+    disable : function()
+    {
+        if(this.inputType != 'radio'){
+            Roo.bootstrap.CheckBox.superclass.disable.call(this);
+            return;
+        }
+        
+        var _this = this;
+        
+        if(this.rendered){
+            Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
+                _this.getActionEl().addClass(this.disabledClass);
+                e.dom.disabled = true;
+            });
+        }
+        
+        this.disabled = true;
+        this.fireEvent("disable", this);
+        return this;
+    },
+
+    enable : function()
+    {
+        if(this.inputType != 'radio'){
+            Roo.bootstrap.CheckBox.superclass.enable.call(this);
+            return;
+        }
+        
+        var _this = this;
+        
+        if(this.rendered){
+            Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
+                _this.getActionEl().removeClass(this.disabledClass);
+                e.dom.disabled = false;
+            });
+        }
+        
+        this.disabled = false;
+        this.fireEvent("enable", this);
+        return this;
     }
     
+
 });
 
 Roo.apply(Roo.bootstrap.CheckBox, {