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
172 * Fires after the key up
173 * @param {Roo.form.Field} this
174 * @param {Roo.EventObject} e The event Object
181 * Returns the name attribute of the field if available
182 * @return {String} name The field name
185 return this.rendered && this.el.dom.name ? this.el.dom.name : (this.hiddenName || '');
189 onRender : function(ct, position){
190 Roo.form.Field.superclass.onRender.call(this, ct, position);
192 var cfg = this.getAutoCreate();
194 cfg.name = typeof(this.name) == 'undefined' ? this.id : this.name;
196 if (!cfg.name.length) {
200 cfg.type = this.inputType;
202 this.el = ct.createChild(cfg, position);
204 var type = this.el.dom.type;
206 if(type == 'password'){
209 this.el.addClass('x-form-'+type);
212 this.el.dom.readOnly = true;
214 if(this.tabIndex !== undefined){
215 this.el.dom.setAttribute('tabIndex', this.tabIndex);
218 this.el.addClass([this.fieldClass, this.cls]);
223 * Apply the behaviors of this component to an existing element. <b>This is used instead of render().</b>
224 * @param {String/HTMLElement/Element} el The id of the node, a DOM node or an existing Element
225 * @return {Roo.form.Field} this
227 applyTo : function(target){
228 this.allowDomMove = false;
229 this.el = Roo.get(target);
230 this.render(this.el.dom.parentNode);
235 initValue : function(){
236 if(this.value !== undefined){
237 this.setValue(this.value);
238 }else if(this.el.dom.value.length > 0){
239 this.setValue(this.el.dom.value);
244 * Returns true if this field has been changed since it was originally loaded and is not disabled.
246 isDirty : function() {
250 return String(this.getValue()) !== String(this.originalValue);
254 afterRender : function(){
255 Roo.form.Field.superclass.afterRender.call(this);
260 fireKey : function(e){
261 //Roo.log('field ' + e.getKey());
262 if(e.isNavKeyPress()){
263 this.fireEvent("specialkey", this, e);
268 * Resets the current field value to the originally loaded value and clears any validation messages
271 this.setValue(this.originalValue);
276 initEvents : function(){
277 // safari killled keypress - so keydown is now used..
278 this.el.on("keydown" , this.fireKey, this);
279 this.el.on("focus", this.onFocus, this);
280 this.el.on("blur", this.onBlur, this);
281 this.el.relayEvent('keyup', this);
283 // reference to original value for reset
284 this.originalValue = this.getValue();
288 onFocus : function(){
289 if(!Roo.isOpera && this.focusClass){ // don't touch in Opera
290 this.el.addClass(this.focusClass);
293 this.hasFocus = true;
294 this.startValue = this.getValue();
295 this.fireEvent("focus", this);
299 beforeBlur : Roo.emptyFn,
304 if(!Roo.isOpera && this.focusClass){ // don't touch in Opera
305 this.el.removeClass(this.focusClass);
307 this.hasFocus = false;
308 if(this.validationEvent !== false && this.validateOnBlur && this.validationEvent != "blur"){
311 var v = this.getValue();
312 if(String(v) !== String(this.startValue)){
313 this.fireEvent('change', this, v, this.startValue);
315 this.fireEvent("blur", this);
319 * Returns whether or not the field value is currently valid
320 * @param {Boolean} preventMark True to disable marking the field invalid
321 * @return {Boolean} True if the value is valid, else false
323 isValid : function(preventMark){
327 var restore = this.preventMark;
328 this.preventMark = preventMark === true;
329 var v = this.validateValue(this.processValue(this.getRawValue()));
330 this.preventMark = restore;
335 * Validates the field value
336 * @return {Boolean} True if the value is valid, else false
338 validate : function(){
339 if(this.disabled || this.validateValue(this.processValue(this.getRawValue()))){
346 processValue : function(value){
351 // Subclasses should provide the validation implementation by overriding this
352 validateValue : function(value){
357 * Mark this field as invalid
358 * @param {String} msg The validation message
360 markInvalid : function(msg){
361 if(!this.rendered || this.preventMark){ // not rendered
364 this.el.addClass(this.invalidClass);
365 msg = msg || this.invalidText;
366 switch(this.msgTarget){
368 this.el.dom.qtip = msg;
369 this.el.dom.qclass = 'x-form-invalid-tip';
370 if(Roo.QuickTips){ // fix for floating editors interacting with DND
371 Roo.QuickTips.enable();
375 this.el.dom.title = msg;
379 var elp = this.el.findParent('.x-form-element', 5, true);
380 this.errorEl = elp.createChild({cls:'x-form-invalid-msg'});
381 this.errorEl.setWidth(elp.getWidth(true)-20);
383 this.errorEl.update(msg);
384 Roo.form.Field.msgFx[this.msgFx].show(this.errorEl, this);
388 var elp = this.el.findParent('.x-form-element', 5, true);
389 this.errorIcon = elp.createChild({cls:'x-form-invalid-icon'});
391 this.alignErrorIcon();
392 this.errorIcon.dom.qtip = msg;
393 this.errorIcon.dom.qclass = 'x-form-invalid-tip';
394 this.errorIcon.show();
395 this.on('resize', this.alignErrorIcon, this);
398 var t = Roo.getDom(this.msgTarget);
400 t.style.display = this.msgDisplay;
403 this.fireEvent('invalid', this, msg);
407 alignErrorIcon : function(){
408 this.errorIcon.alignTo(this.el, 'tl-tr', [2, 0]);
412 * Clear any invalid styles/messages for this field
414 clearInvalid : function(){
415 if(!this.rendered || this.preventMark){ // not rendered
418 this.el.removeClass(this.invalidClass);
419 switch(this.msgTarget){
421 this.el.dom.qtip = '';
424 this.el.dom.title = '';
428 Roo.form.Field.msgFx[this.msgFx].hide(this.errorEl, this);
433 this.errorIcon.dom.qtip = '';
434 this.errorIcon.hide();
435 this.un('resize', this.alignErrorIcon, this);
439 var t = Roo.getDom(this.msgTarget);
441 t.style.display = 'none';
444 this.fireEvent('valid', this);
448 * Returns the raw data value which may or may not be a valid, defined value. To return a normalized value see {@link #getValue}.
449 * @return {Mixed} value The field value
451 getRawValue : function(){
452 var v = this.el.getValue();
458 * Returns the normalized data value (undefined or emptyText will be returned as ''). To return the raw value see {@link #getRawValue}.
459 * @return {Mixed} value The field value
461 getValue : function(){
462 var v = this.el.getValue();
468 * Sets the underlying DOM field's value directly, bypassing validation. To set the value with validation see {@link #setValue}.
469 * @param {Mixed} value The value to set
471 setRawValue : function(v){
472 return this.el.dom.value = (v === null || v === undefined ? '' : v);
476 * Sets a data value into the field and validates it. To set the value directly without validation see {@link #setRawValue}.
477 * @param {Mixed} value The value to set
479 setValue : function(v){
482 this.el.dom.value = (v === null || v === undefined ? '' : v);
487 adjustSize : function(w, h){
488 var s = Roo.form.Field.superclass.adjustSize.call(this, w, h);
489 s.width = this.adjustWidth(this.el.dom.tagName, s.width);
493 adjustWidth : function(tag, w){
494 tag = tag.toLowerCase();
495 if(typeof w == 'number' && Roo.isStrict && !Roo.isSafari){
496 if(Roo.isIE && (tag == 'input' || tag == 'textarea')){
500 if(tag = 'textarea'){
503 }else if(Roo.isOpera){
507 if(tag = 'textarea'){
517 // anything other than normal should be considered experimental
518 Roo.form.Field.msgFx = {
520 show: function(msgEl, f){
521 msgEl.setDisplayed('block');
524 hide : function(msgEl, f){
525 msgEl.setDisplayed(false).update('');
530 show: function(msgEl, f){
531 msgEl.slideIn('t', {stopFx:true});
534 hide : function(msgEl, f){
535 msgEl.slideOut('t', {stopFx:true,useDisplay:true});
540 show: function(msgEl, f){
542 msgEl.alignTo(f.el, 'tl-tr');
543 msgEl.slideIn('l', {stopFx:true});
546 hide : function(msgEl, f){
547 msgEl.slideOut('l', {stopFx:true,useDisplay:true});