3 * Copyright(c) 2006-2010 Ext JS, Inc.
5 * http://www.extjs.com/license
8 * @class Ext.form.CompositeField
9 * @extends Ext.form.Field
10 * Composite field allowing a number of form Fields to be rendered on the same row. The fields are rendered
11 * using an hbox layout internally, so all of the normal HBox layout config items are available. Example usage:
14 xtype: 'compositefield',
35 * In the example above the composite's fieldLabel will be set to 'Title, First, Last' as it groups the fieldLabels
36 * of each of its children. This can be overridden by setting a fieldLabel on the compositefield itself:
39 xtype: 'compositefield',
40 fieldLabel: 'Custom label',
44 * Any Ext.form.* component can be placed inside a composite field.
46 Ext.form.CompositeField = Ext.extend(Ext.form.Field, {
49 * @property defaultMargins
51 * The margins to apply by default to each field in the composite
53 defaultMargins: '0 5 0 0',
56 * @property skipLastItemMargin
58 * If true, the defaultMargins are not applied to the last item in the composite field set (defaults to true)
60 skipLastItemMargin: true,
63 * @property isComposite
65 * Signifies that this is a Composite field
70 * @property combineErrors
72 * True to combine errors from the individual fields into a single error message at the CompositeField level (defaults to true)
77 * @cfg {String} labelConnector The string to use when joining segments of the built label together (defaults to ', ')
82 //Builds the composite field label
83 initComponent: function() {
88 for (var i=0, j = items.length; i < j; i++) {
91 labels.push(item.fieldLabel);
94 Ext.apply(item, this.defaults);
96 //apply default margins to each item except the last
97 if (!(i == j - 1 && this.skipLastItemMargin)) {
98 Ext.applyIf(item, {margins: this.defaultMargins});
102 this.fieldLabel = this.fieldLabel || this.buildLabel(labels);
105 * @property fieldErrors
106 * @type Ext.util.MixedCollection
107 * MixedCollection of current errors on the Composite's subfields. This is used internally to track when
108 * to show and hide error messages at the Composite level. Listeners are attached to the MixedCollection's
109 * add, remove and replace events to update the error icon in the UI as errors are added or removed.
111 this.fieldErrors = new Ext.util.MixedCollection(true, function(item) {
115 this.fieldErrors.on({
117 add : this.updateInvalidMark,
118 remove : this.updateInvalidMark,
119 replace: this.updateInvalidMark
122 Ext.form.CompositeField.superclass.initComponent.apply(this, arguments);
127 * Creates an internal container using hbox and renders the fields to it
129 onRender: function(ct, position) {
133 * @type Ext.Container
134 * A container configured with hbox layout which is responsible for laying out the subfields
136 var innerCt = this.innerCt = new Ext.Container({
140 cls : 'x-form-composite',
141 defaultMargins: '0 3 0 0'
144 this.el = innerCt.getEl();
146 var fields = innerCt.findBy(function(c) {
147 return c.isFormField;
152 * @type Ext.util.MixedCollection
153 * Internal collection of all of the subfields in this Composite
155 this.items = new Ext.util.MixedCollection();
156 this.items.addAll(fields);
158 //if we're combining subfield errors into a single message, override the markInvalid and clearInvalid
159 //methods of each subfield and show them at the Composite level instead
160 if (this.combineErrors) {
161 this.eachItem(function(field) {
163 markInvalid : this.onFieldMarkInvalid.createDelegate(this, [field], 0),
164 clearInvalid: this.onFieldClearInvalid.createDelegate(this, [field], 0)
169 //set the label 'for' to the first item
170 var l = this.el.parent().parent().child('label', true);
172 l.setAttribute('for', this.items.items[0].id);
176 Ext.form.CompositeField.superclass.onRender.apply(this, arguments);
180 * Called if combineErrors is true and a subfield's markInvalid method is called.
181 * By default this just adds the subfield's error to the internal fieldErrors MixedCollection
182 * @param {Ext.form.Field} field The field that was marked invalid
183 * @param {String} message The error message
185 onFieldMarkInvalid: function(field, message) {
186 var name = field.getName(),
187 error = {field: name, error: message};
189 this.fieldErrors.replace(name, error);
191 field.el.addClass(field.invalidClass);
195 * Called if combineErrors is true and a subfield's clearInvalid method is called.
196 * By default this just updates the internal fieldErrors MixedCollection.
197 * @param {Ext.form.Field} field The field that was marked invalid
199 onFieldClearInvalid: function(field) {
200 this.fieldErrors.removeKey(field.getName());
202 field.el.removeClass(field.invalidClass);
207 * Called after a subfield is marked valid or invalid, this checks to see if any of the subfields are
208 * currently invalid. If any subfields are invalid it builds a combined error message marks the composite
209 * invalid, otherwise clearInvalid is called
211 updateInvalidMark: function() {
212 var ieStrict = Ext.isIE6 && Ext.isStrict;
214 if (this.fieldErrors.length == 0) {
217 //IE6 in strict mode has a layout bug when using 'under' as the error message target. This fixes it
219 this.clearInvalid.defer(50, this);
222 var message = this.buildCombinedErrorMessage(this.fieldErrors.items);
225 this.markInvalid(message);
227 //IE6 in strict mode has a layout bug when using 'under' as the error message target. This fixes it
229 this.markInvalid(message);
235 * Performs validation checks on each subfield and returns false if any of them fail validation.
236 * @return {Boolean} False if any subfield failed validation
238 validateValue: function() {
241 this.eachItem(function(field) {
242 if (!field.isValid()) valid = false;
249 * Takes an object containing error messages for contained fields, returning a combined error
250 * string (defaults to just placing each item on a new line). This can be overridden to provide
251 * custom combined error message handling.
252 * @param {Array} errors Array of errors in format: [{field: 'title', error: 'some error'}]
253 * @return {String} The combined error message
255 buildCombinedErrorMessage: function(errors) {
259 for (var i = 0, j = errors.length; i < j; i++) {
262 combined.push(String.format("{0}: {1}", error.field, error.error));
265 return combined.join("<br />");
269 * Sorts the internal fieldErrors MixedCollection by the order in which the fields are defined.
270 * This is called before displaying errors to ensure that the errors are presented in the expected order.
271 * This function can be overridden to provide a custom sorting order if needed.
273 sortErrors: function() {
274 var fields = this.items;
276 this.fieldErrors.sort("ASC", function(a, b) {
277 var findByName = function(key) {
278 return function(field) {
279 return field.getName() == key;
283 var aIndex = fields.findIndexBy(findByName(a.field)),
284 bIndex = fields.findIndexBy(findByName(b.field));
286 return aIndex < bIndex ? -1 : 1;
291 * Resets each field in the composite to their previous value
294 this.eachItem(function(item) {
298 // Defer the clearInvalid so if BaseForm's collection is being iterated it will be called AFTER it is complete.
299 // Important because reset is being called on both the group and the individual items.
306 * Calls clearInvalid on all child fields. This is a convenience function and should not often need to be called
307 * as fields usually take care of clearing themselves
309 clearInvalidChildren: function() {
310 this.eachItem(function(item) {
316 * Builds a label string from an array of subfield labels.
317 * By default this just joins the labels together with a comma
318 * @param {Array} segments Array of each of the labels in the composite field's subfields
319 * @return {String} The built label
321 buildLabel: function(segments) {
322 return Ext.clean(segments).join(this.labelConnector);
326 * Checks each field in the composite and returns true if any is dirty
327 * @return {Boolean} True if any field is dirty
330 //override the behaviour to check sub items.
331 if (this.disabled || !this.rendered) {
336 this.eachItem(function(item){
347 * Convenience function which passes the given function to every item in the composite
348 * @param {Function} fn The function to call
349 * @param {Object} scope Optional scope object
351 eachItem: function(fn, scope) {
352 if(this.items && this.items.each){
353 this.items.each(fn, scope || this);
359 * Passes the resize call through to the inner panel
361 onResize: function(adjWidth, adjHeight, rawWidth, rawHeight) {
362 var innerCt = this.innerCt;
364 if (this.rendered && innerCt.rendered) {
365 innerCt.setSize(adjWidth, adjHeight);
368 Ext.form.CompositeField.superclass.onResize.apply(this, arguments);
373 * Forces the internal container to be laid out again
375 doLayout: function(shallow, force) {
377 var innerCt = this.innerCt;
379 innerCt.forceLayout = this.ownerCt.forceLayout;
380 innerCt.doLayout(shallow, force);
387 beforeDestroy: function(){
388 Ext.destroy(this.innerCt);
390 Ext.form.CompositeField.superclass.beforeDestroy.call(this);
393 //override the behaviour to check sub items.
394 setReadOnly : function(readOnly) {
395 readOnly = readOnly || true;
398 this.eachItem(function(item){
399 item.setReadOnly(readOnly);
402 this.readOnly = readOnly;
405 onShow : function() {
406 Ext.form.CompositeField.superclass.onShow.call(this);
410 //override the behaviour to check sub items.
411 onDisable : function(){
412 this.eachItem(function(item){
417 //override the behaviour to check sub items.
418 onEnable : function(){
419 this.eachItem(function(item){
425 Ext.reg('compositefield', Ext.form.CompositeField);