4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
13 * @class Roo.form.Field
14 * @extends Roo.BoxComponent
15 * Base class for form fields that provides default event handling, sizing, value handling and other functionality.
18 * @param {Object} config Configuration options
20 Roo.form.Field = function(config){
21 Roo.form.Field.superclass.constructor.call(this, config);
24 Roo.extend(Roo.form.Field, Roo.BoxComponent, {
26 * @cfg {String} fieldLabel Label to use when rendering a form.
29 * @cfg {String} qtip Mouse over tip
33 * @cfg {String} invalidClass The CSS class to use when marking a field invalid (defaults to "x-form-invalid")
35 invalidClass : "x-form-invalid",
37 * @cfg {String} invalidText The error text to use when marking a field invalid and no message is provided (defaults to "The value in this field is invalid")
39 invalidText : "The value in this field is invalid",
41 * @cfg {String} focusClass The CSS class to use when the field receives focus (defaults to "x-form-focus")
43 focusClass : "x-form-focus",
45 * @cfg {String/Boolean} validationEvent The event that should initiate field validation. Set to false to disable
46 automatic validation (defaults to "keyup").
48 validationEvent : "keyup",
50 * @cfg {Boolean} validateOnBlur Whether the field should validate when it loses focus (defaults to true).
52 validateOnBlur : true,
54 * @cfg {Number} validationDelay The length of time in milliseconds after user input begins until validation is initiated (defaults to 250)
56 validationDelay : 250,
58 * @cfg {String/Object} autoCreate A DomHelper element spec, or true for a default element spec (defaults to
59 * {tag: "input", type: "text", size: "20", autocomplete: "off"})
61 defaultAutoCreate : {tag: "input", type: "text", size: "20", autocomplete: "off"},
63 * @cfg {String} fieldClass The default CSS class for the field (defaults to "x-form-field")
65 fieldClass : "x-form-field",
67 * @cfg {String} msgTarget The location where error text should display. Should be one of the following values (defaults to 'qtip'):
70 ----------- ----------------------------------------------------------------------
71 qtip Display a quick tip when the user hovers over the field
72 title Display a default browser title attribute popup
73 under Add a block div beneath the field containing the error text
74 side Add an error icon to the right of the field with a popup on hover
75 [element id] Add the error text directly to the innerHTML of the specified element
80 * @cfg {String} msgFx <b>Experimental</b> The effect used when displaying a validation message under the field (defaults to 'normal').
85 * @cfg {Boolean} readOnly True to mark the field as readOnly in HTML (defaults to false) -- Note: this only sets the element's readOnly DOM attribute.
90 * @cfg {Boolean} disabled True to disable the field (defaults to false).
95 * @cfg {String} inputType The type attribute for input fields -- e.g. radio, text, password (defaults to "text").
97 inputType : undefined,
100 * @cfg {Number} tabIndex The tabIndex for this field. Note this only applies to fields that are rendered, not those which are built via applyTo (defaults to undefined).
102 tabIndex : undefined,
110 * @property {Roo.Element} fieldEl
111 * Element Containing the rendered Field (with label etc.)
114 * @cfg {Mixed} value A value to initialize this field with.
119 * @cfg {String} name The field's HTML name attribute.
122 * @cfg {String} cls A CSS class to apply to the field's underlying element.
126 initComponent : function(){
127 Roo.form.Field.superclass.initComponent.call(this);
131 * Fires when this field receives input focus.
132 * @param {Roo.form.Field} this
137 * Fires when this field loses input focus.
138 * @param {Roo.form.Field} this
143 * Fires when any key related to navigation (arrows, tab, enter, esc, etc.) is pressed. You can check
144 * {@link Roo.EventObject#getKey} to determine which key was pressed.
145 * @param {Roo.form.Field} this
146 * @param {Roo.EventObject} e The event object
151 * Fires just before the field blurs if the field value has changed.
152 * @param {Roo.form.Field} this
153 * @param {Mixed} newValue The new value
154 * @param {Mixed} oldValue The original value
159 * Fires after the field has been marked as invalid.
160 * @param {Roo.form.Field} this
161 * @param {String} msg The validation message
166 * Fires after the field has been validated with no errors.
167 * @param {Roo.form.Field} this
174 * Returns the name attribute of the field if available
175 * @return {String} name The field name
178 return this.rendered && this.el.dom.name ? this.el.dom.name : (this.hiddenName || '');
182 onRender : function(ct, position){
183 Roo.form.Field.superclass.onRender.call(this, ct, position);
185 var cfg = this.getAutoCreate();
187 cfg.name = this.name || this.id;
190 cfg.type = this.inputType;
192 this.el = ct.createChild(cfg, position);
194 var type = this.el.dom.type;
196 if(type == 'password'){
199 this.el.addClass('x-form-'+type);
202 this.el.dom.readOnly = true;
204 if(this.tabIndex !== undefined){
205 this.el.dom.setAttribute('tabIndex', this.tabIndex);
208 this.el.addClass([this.fieldClass, this.cls]);
213 * Apply the behaviors of this component to an existing element. <b>This is used instead of render().</b>
214 * @param {String/HTMLElement/Element} el The id of the node, a DOM node or an existing Element
215 * @return {Roo.form.Field} this
217 applyTo : function(target){
218 this.allowDomMove = false;
219 this.el = Roo.get(target);
220 this.render(this.el.dom.parentNode);
225 initValue : function(){
226 if(this.value !== undefined){
227 this.setValue(this.value);
228 }else if(this.el.dom.value.length > 0){
229 this.setValue(this.el.dom.value);
234 * Returns true if this field has been changed since it was originally loaded and is not disabled.
236 isDirty : function() {
240 return String(this.getValue()) !== String(this.originalValue);
244 afterRender : function(){
245 Roo.form.Field.superclass.afterRender.call(this);
250 fireKey : function(e){
251 Roo.log('field ' + e.getKey());
252 if(e.isNavKeyPress()){
253 this.fireEvent("specialkey", this, e);
258 * Resets the current field value to the originally loaded value and clears any validation messages
261 this.setValue(this.originalValue);
266 initEvents : function(){
267 // safari killled keypress - so keydown is now used..
268 this.el.on("keydown" , this.fireKey, this);
269 this.el.on("focus", this.onFocus, this);
270 this.el.on("blur", this.onBlur, this);
272 // reference to original value for reset
273 this.originalValue = this.getValue();
277 onFocus : function(){
278 if(!Roo.isOpera && this.focusClass){ // don't touch in Opera
279 this.el.addClass(this.focusClass);
282 this.hasFocus = true;
283 this.startValue = this.getValue();
284 this.fireEvent("focus", this);
288 beforeBlur : Roo.emptyFn,
293 if(!Roo.isOpera && this.focusClass){ // don't touch in Opera
294 this.el.removeClass(this.focusClass);
296 this.hasFocus = false;
297 if(this.validationEvent !== false && this.validateOnBlur && this.validationEvent != "blur"){
300 var v = this.getValue();
301 if(String(v) !== String(this.startValue)){
302 this.fireEvent('change', this, v, this.startValue);
304 this.fireEvent("blur", this);
308 * Returns whether or not the field value is currently valid
309 * @param {Boolean} preventMark True to disable marking the field invalid
310 * @return {Boolean} True if the value is valid, else false
312 isValid : function(preventMark){
316 var restore = this.preventMark;
317 this.preventMark = preventMark === true;
318 var v = this.validateValue(this.processValue(this.getRawValue()));
319 this.preventMark = restore;
324 * Validates the field value
325 * @return {Boolean} True if the value is valid, else false
327 validate : function(){
328 if(this.disabled || this.validateValue(this.processValue(this.getRawValue()))){
335 processValue : function(value){
340 // Subclasses should provide the validation implementation by overriding this
341 validateValue : function(value){
346 * Mark this field as invalid
347 * @param {String} msg The validation message
349 markInvalid : function(msg){
350 if(!this.rendered || this.preventMark){ // not rendered
353 this.el.addClass(this.invalidClass);
354 msg = msg || this.invalidText;
355 switch(this.msgTarget){
357 this.el.dom.qtip = msg;
358 this.el.dom.qclass = 'x-form-invalid-tip';
359 if(Roo.QuickTips){ // fix for floating editors interacting with DND
360 Roo.QuickTips.enable();
364 this.el.dom.title = msg;
368 var elp = this.el.findParent('.x-form-element', 5, true);
369 this.errorEl = elp.createChild({cls:'x-form-invalid-msg'});
370 this.errorEl.setWidth(elp.getWidth(true)-20);
372 this.errorEl.update(msg);
373 Roo.form.Field.msgFx[this.msgFx].show(this.errorEl, this);
377 var elp = this.el.findParent('.x-form-element', 5, true);
378 this.errorIcon = elp.createChild({cls:'x-form-invalid-icon'});
380 this.alignErrorIcon();
381 this.errorIcon.dom.qtip = msg;
382 this.errorIcon.dom.qclass = 'x-form-invalid-tip';
383 this.errorIcon.show();
384 this.on('resize', this.alignErrorIcon, this);
387 var t = Roo.getDom(this.msgTarget);
389 t.style.display = this.msgDisplay;
392 this.fireEvent('invalid', this, msg);
396 alignErrorIcon : function(){
397 this.errorIcon.alignTo(this.el, 'tl-tr', [2, 0]);
401 * Clear any invalid styles/messages for this field
403 clearInvalid : function(){
404 if(!this.rendered || this.preventMark){ // not rendered
407 this.el.removeClass(this.invalidClass);
408 switch(this.msgTarget){
410 this.el.dom.qtip = '';
413 this.el.dom.title = '';
417 Roo.form.Field.msgFx[this.msgFx].hide(this.errorEl, this);
422 this.errorIcon.dom.qtip = '';
423 this.errorIcon.hide();
424 this.un('resize', this.alignErrorIcon, this);
428 var t = Roo.getDom(this.msgTarget);
430 t.style.display = 'none';
433 this.fireEvent('valid', this);
437 * Returns the raw data value which may or may not be a valid, defined value. To return a normalized value see {@link #getValue}.
438 * @return {Mixed} value The field value
440 getRawValue : function(){
441 var v = this.el.getValue();
442 if(v === this.emptyText){
449 * Returns the normalized data value (undefined or emptyText will be returned as ''). To return the raw value see {@link #getRawValue}.
450 * @return {Mixed} value The field value
452 getValue : function(){
453 var v = this.el.getValue();
454 if(v === this.emptyText || v === undefined){
461 * Sets the underlying DOM field's value directly, bypassing validation. To set the value with validation see {@link #setValue}.
462 * @param {Mixed} value The value to set
464 setRawValue : function(v){
465 return this.el.dom.value = (v === null || v === undefined ? '' : v);
469 * Sets a data value into the field and validates it. To set the value directly without validation see {@link #setRawValue}.
470 * @param {Mixed} value The value to set
472 setValue : function(v){
475 this.el.dom.value = (v === null || v === undefined ? '' : v);
480 adjustSize : function(w, h){
481 var s = Roo.form.Field.superclass.adjustSize.call(this, w, h);
482 s.width = this.adjustWidth(this.el.dom.tagName, s.width);
486 adjustWidth : function(tag, w){
487 tag = tag.toLowerCase();
488 if(typeof w == 'number' && Roo.isStrict && !Roo.isSafari){
489 if(Roo.isIE && (tag == 'input' || tag == 'textarea')){
493 if(tag = 'textarea'){
496 }else if(Roo.isOpera){
500 if(tag = 'textarea'){
510 // anything other than normal should be considered experimental
511 Roo.form.Field.msgFx = {
513 show: function(msgEl, f){
514 msgEl.setDisplayed('block');
517 hide : function(msgEl, f){
518 msgEl.setDisplayed(false).update('');
523 show: function(msgEl, f){
524 msgEl.slideIn('t', {stopFx:true});
527 hide : function(msgEl, f){
528 msgEl.slideOut('t', {stopFx:true,useDisplay:true});
533 show: function(msgEl, f){
535 msgEl.alignTo(f.el, 'tl-tr');
536 msgEl.slideIn('l', {stopFx:true});
539 hide : function(msgEl, f){
540 msgEl.slideOut('l', {stopFx:true,useDisplay:true});