3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4 <title>The source code</title>
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <body onload="prettyPrint();">
9 <pre class="prettyprint lang-js">/*!
10 * Ext JS Library 3.2.1
11 * Copyright(c) 2006-2010 Ext JS, Inc.
13 * http://www.extjs.com/license
15 <div id="cls-Ext.form.CompositeField"></div>/**
16 * @class Ext.form.CompositeField
17 * @extends Ext.form.Field
18 * Composite field allowing a number of form Fields to be rendered on the same row. The fields are rendered
19 * using an hbox layout internally, so all of the normal HBox layout config items are available. Example usage:
22 xtype: 'compositefield',
43 * In the example above the composite's fieldLabel will be set to 'Title, First, Last' as it groups the fieldLabels
44 * of each of its children. This can be overridden by setting a fieldLabel on the compositefield itself:
47 xtype: 'compositefield',
48 fieldLabel: 'Custom label',
52 * Any Ext.form.* component can be placed inside a composite field.
54 Ext.form.CompositeField = Ext.extend(Ext.form.Field, {
56 <div id="prop-Ext.form.CompositeField-defaultMargins"></div>/**
57 * @property defaultMargins
59 * The margins to apply by default to each field in the composite
61 defaultMargins: '0 5 0 0',
63 <div id="prop-Ext.form.CompositeField-skipLastItemMargin"></div>/**
64 * @property skipLastItemMargin
66 * If true, the defaultMargins are not applied to the last item in the composite field set (defaults to true)
68 skipLastItemMargin: true,
70 <div id="prop-Ext.form.CompositeField-isComposite"></div>/**
71 * @property isComposite
73 * Signifies that this is a Composite field
77 <div id="prop-Ext.form.CompositeField-combineErrors"></div>/**
78 * @property combineErrors
80 * True to combine errors from the individual fields into a single error message at the CompositeField level (defaults to true)
85 //Builds the composite field label
86 initComponent: function() {
91 for (var i=0, j = items.length; i < j; i++) {
94 labels.push(item.fieldLabel);
97 Ext.apply(item, this.defaults);
99 //apply default margins to each item except the last
100 if (!(i == j - 1 && this.skipLastItemMargin)) {
101 Ext.applyIf(item, {margins: this.defaultMargins});
105 this.fieldLabel = this.fieldLabel || this.buildLabel(labels);
107 <div id="prop-Ext.form.CompositeField-fieldErrors"></div>/**
108 * @property fieldErrors
109 * @type Ext.util.MixedCollection
110 * MixedCollection of current errors on the Composite's subfields. This is used internally to track when
111 * to show and hide error messages at the Composite level. Listeners are attached to the MixedCollection's
112 * add, remove and replace events to update the error icon in the UI as errors are added or removed.
114 this.fieldErrors = new Ext.util.MixedCollection(true, function(item) {
118 this.fieldErrors.on({
120 add : this.updateInvalidMark,
121 remove : this.updateInvalidMark,
122 replace: this.updateInvalidMark
125 Ext.form.CompositeField.superclass.initComponent.apply(this, arguments);
130 * Creates an internal container using hbox and renders the fields to it
132 onRender: function(ct, position) {
134 <div id="prop-Ext.form.CompositeField-innerCt"></div>/**
136 * @type Ext.Container
137 * A container configured with hbox layout which is responsible for laying out the subfields
139 var innerCt = this.innerCt = new Ext.Container({
143 cls : 'x-form-composite',
144 defaultMargins: '0 3 0 0'
147 this.el = innerCt.getEl();
149 var fields = innerCt.findBy(function(c) {
150 return c.isFormField;
153 <div id="prop-Ext.form.CompositeField-items"></div>/**
155 * @type Ext.util.MixedCollection
156 * Internal collection of all of the subfields in this Composite
158 this.items = new Ext.util.MixedCollection();
159 this.items.addAll(fields);
161 //if we're combining subfield errors into a single message, override the markInvalid and clearInvalid
162 //methods of each subfield and show them at the Composite level instead
163 if (this.combineErrors) {
164 this.eachItem(function(field) {
166 markInvalid : this.onFieldMarkInvalid.createDelegate(this, [field], 0),
167 clearInvalid: this.onFieldClearInvalid.createDelegate(this, [field], 0)
172 //set the label 'for' to the first item
173 var l = this.el.parent().parent().child('label', true);
175 l.setAttribute('for', this.items.items[0].id);
179 Ext.form.CompositeField.superclass.onRender.apply(this, arguments);
182 <div id="method-Ext.form.CompositeField-onFieldMarkInvalid"></div>/**
183 * Called if combineErrors is true and a subfield's markInvalid method is called.
184 * By default this just adds the subfield's error to the internal fieldErrors MixedCollection
185 * @param {Ext.form.Field} field The field that was marked invalid
186 * @param {String} message The error message
188 onFieldMarkInvalid: function(field, message) {
189 var name = field.getName(),
190 error = {field: name, error: message};
192 this.fieldErrors.replace(name, error);
194 field.el.addClass(field.invalidClass);
197 <div id="method-Ext.form.CompositeField-onFieldClearInvalid"></div>/**
198 * Called if combineErrors is true and a subfield's clearInvalid method is called.
199 * By default this just updates the internal fieldErrors MixedCollection.
200 * @param {Ext.form.Field} field The field that was marked invalid
202 onFieldClearInvalid: function(field) {
203 this.fieldErrors.removeKey(field.getName());
205 field.el.removeClass(field.invalidClass);
210 * Called after a subfield is marked valid or invalid, this checks to see if any of the subfields are
211 * currently invalid. If any subfields are invalid it builds a combined error message marks the composite
212 * invalid, otherwise clearInvalid is called
214 updateInvalidMark: function() {
215 var ieStrict = Ext.isIE6 && Ext.isStrict;
217 if (this.fieldErrors.length == 0) {
220 //IE6 in strict mode has a layout bug when using 'under' as the error message target. This fixes it
222 this.clearInvalid.defer(50, this);
225 var message = this.buildCombinedErrorMessage(this.fieldErrors.items);
228 this.markInvalid(message);
230 //IE6 in strict mode has a layout bug when using 'under' as the error message target. This fixes it
232 this.markInvalid(message);
237 <div id="method-Ext.form.CompositeField-validateValue"></div>/**
238 * Performs validation checks on each subfield and returns false if any of them fail validation.
239 * @return {Boolean} False if any subfield failed validation
241 validateValue: function() {
244 this.eachItem(function(field) {
245 if (!field.isValid()) valid = false;
251 <div id="method-Ext.form.CompositeField-buildCombinedErrorMessage"></div>/**
252 * Takes an object containing error messages for contained fields, returning a combined error
253 * string (defaults to just placing each item on a new line). This can be overridden to provide
254 * custom combined error message handling.
255 * @param {Array} errors Array of errors in format: [{field: 'title', error: 'some error'}]
256 * @return {String} The combined error message
258 buildCombinedErrorMessage: function(errors) {
262 for (var i = 0, j = errors.length; i < j; i++) {
265 combined.push(String.format("{0}: {1}", error.field, error.error));
268 return combined.join("<br />");
271 <div id="method-Ext.form.CompositeField-sortErrors"></div>/**
272 * Sorts the internal fieldErrors MixedCollection by the order in which the fields are defined.
273 * This is called before displaying errors to ensure that the errors are presented in the expected order.
274 * This function can be overridden to provide a custom sorting order if needed.
276 sortErrors: function() {
277 var fields = this.items;
279 this.fieldErrors.sort("ASC", function(a, b) {
280 var findByName = function(key) {
281 return function(field) {
282 return field.getName() == key;
286 var aIndex = fields.findIndexBy(findByName(a.field)),
287 bIndex = fields.findIndexBy(findByName(b.field));
289 return aIndex < bIndex ? -1 : 1;
293 <div id="method-Ext.form.CompositeField-reset"></div>/**
294 * Resets each field in the composite to their previous value
297 this.eachItem(function(item) {
301 // Defer the clearInvalid so if BaseForm's collection is being iterated it will be called AFTER it is complete.
302 // Important because reset is being called on both the group and the individual items.
308 <div id="method-Ext.form.CompositeField-clearInvalidChildren"></div>/**
309 * Calls clearInvalid on all child fields. This is a convenience function and should not often need to be called
310 * as fields usually take care of clearing themselves
312 clearInvalidChildren: function() {
313 this.eachItem(function(item) {
318 <div id="method-Ext.form.CompositeField-buildLabel"></div>/**
319 * Builds a label string from an array of subfield labels.
320 * By default this just joins the labels together with a comma
321 * @param {Array} segments Array of each of the labels in the composite field's subfields
322 * @return {String} The built label
324 buildLabel: function(segments) {
325 return segments.join(", ");
328 <div id="method-Ext.form.CompositeField-isDirty"></div>/**
329 * Checks each field in the composite and returns true if any is dirty
330 * @return {Boolean} True if any field is dirty
333 //override the behaviour to check sub items.
334 if (this.disabled || !this.rendered) {
339 this.eachItem(function(item){
350 * Convenience function which passes the given function to every item in the composite
351 * @param {Function} fn The function to call
352 * @param {Object} scope Optional scope object
354 eachItem: function(fn, scope) {
355 if(this.items && this.items.each){
356 this.items.each(fn, scope || this);
362 * Passes the resize call through to the inner panel
364 onResize: function(adjWidth, adjHeight, rawWidth, rawHeight) {
365 var innerCt = this.innerCt;
367 if (this.rendered && innerCt.rendered) {
368 innerCt.setSize(adjWidth, adjHeight);
371 Ext.form.CompositeField.superclass.onResize.apply(this, arguments);
376 * Forces the internal container to be laid out again
378 doLayout: function(shallow, force) {
380 var innerCt = this.innerCt;
382 innerCt.forceLayout = this.ownerCt.forceLayout;
383 innerCt.doLayout(shallow, force);
390 beforeDestroy: function(){
391 Ext.destroy(this.innerCt);
393 Ext.form.CompositeField.superclass.beforeDestroy.call(this);
396 //override the behaviour to check sub items.
397 setReadOnly : function(readOnly) {
398 readOnly = readOnly || true;
401 this.eachItem(function(item){
402 item.setReadOnly(readOnly);
405 this.readOnly = readOnly;
408 onShow : function() {
409 Ext.form.CompositeField.superclass.onShow.call(this);
413 //override the behaviour to check sub items.
414 onDisable : function(){
415 this.eachItem(function(item){
420 //override the behaviour to check sub items.
421 onEnable : function(){
422 this.eachItem(function(item){
428 Ext.reg('compositefield', Ext.form.CompositeField);