Roo/bootstrap/FieldLabel.js
[roojs1] / Roo / bootstrap / FieldLabel.js
1 /*
2  * - LGPL
3  *
4  * FieldLabel
5  * 
6  */
7
8 /**
9  * @class Roo.bootstrap.FieldLabel
10  * @extends Roo.bootstrap.Component
11  * Bootstrap FieldLabel class
12  * @cfg {String} html contents of the element
13  * @cfg {String} tag tag of the element default label
14  * @cfg {String} cls class of the element
15  * @cfg {String} for label target 
16  * @cfg {Boolean} allowBlank (true|false) target allowBlank default true
17  * @cfg {String} invalidClass default "text-danger fa fa-lg fa-exclamation-triangle"
18  * @cfg {String} validClass default "text-success fa fa-lg fa-check"
19  * 
20  * @constructor
21  * Create a new FieldLabel
22  * @param {Object} config The config object
23  */
24
25 Roo.bootstrap.FieldLabel = function(config){
26     Roo.bootstrap.Element.superclass.constructor.call(this, config);
27     
28     this.addEvents({
29             /**
30              * @event invalid
31              * Fires after the field has been marked as invalid.
32              * @param {Roo.form.FieldLabel} this
33              * @param {String} msg The validation message
34              */
35             invalid : true,
36             /**
37              * @event valid
38              * Fires after the field has been validated with no errors.
39              * @param {Roo.form.FieldLabel} this
40              */
41             valid : true
42         });
43 };
44
45 Roo.extend(Roo.bootstrap.FieldLabel, Roo.bootstrap.Component,  {
46     
47     tag: 'label',
48     cls: '',
49     html: '',
50     for: '',
51     allowBlank : true,
52     invalidClass : 'text-danger fa fa-lg fa-exclamation-triangle',
53     validClass : 'text-success fa fa-lg fa-check',
54     
55     
56     getAutoCreate : function(){
57         
58         var cfg = {
59             tag : this.tag,
60             cls : this.cls,
61             for : this.for,
62             cn : [
63                 {
64                     tag : 'i',
65                     cls : ''
66                 },
67                 {
68                     
69                 }
70             ] 
71         };
72         
73         return cfg;
74     },
75     
76     initEvents: function() 
77     {
78         Roo.bootstrap.Element.superclass.initEvents.call(this);
79         
80     },
81     
82     /**
83      * Mark this field as valid
84      */
85     markValid : function(){
86         if(!this.el  || this.preventMark){ // not rendered
87             return;
88         }
89         
90         this.el.removeClass([this.invalidClass, this.validClass]);
91         
92         var feedback = this.el.select('.form-control-feedback', true).first();
93             
94         if(feedback){
95             this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
96         }
97
98         if(this.disabled || this.allowBlank){
99             return;
100         }
101         
102         this.el.addClass(this.validClass);
103         
104         if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank && (this.getValue().length || this.forceFeedback)){
105             
106             var feedback = this.el.select('.form-control-feedback', true).first();
107             
108             if(feedback){
109                 this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
110                 this.el.select('.form-control-feedback', true).first().addClass([this.validFeedbackClass]);
111             }
112             
113         }
114         
115         this.fireEvent('valid', this);
116     },
117     
118     /**
119      * Mark this field as invalid
120      * @param {String} msg The validation message
121      */
122     markInvalid : function(msg)
123     {
124         if(!this.el  || this.preventMark){ // not rendered
125             return;
126         }
127         
128         this.el.removeClass([this.invalidClass, this.validClass]);
129         
130         var feedback = this.el.select('.form-control-feedback', true).first();
131             
132         if(feedback){
133             this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
134         }
135
136         if(this.disabled || this.allowBlank){
137             return;
138         }
139         
140         this.el.addClass(this.invalidClass);
141         
142         if(this.hasFeedback && this.inputType != 'hidden' && !this.allowBlank){
143             
144             var feedback = this.el.select('.form-control-feedback', true).first();
145             
146             if(feedback){
147                 this.el.select('.form-control-feedback', true).first().removeClass([this.invalidFeedbackClass, this.validFeedbackClass]);
148                 
149                 if(this.getValue().length || this.forceFeedback){
150                     this.el.select('.form-control-feedback', true).first().addClass([this.invalidFeedbackClass]);
151                 }
152                 
153             }
154             
155         }
156         
157         this.fireEvent('invalid', this, msg);
158     }
159     
160    
161 });
162
163  
164
165