diff --git a/examples/attribute-form.html b/examples/attribute-form.html index 7c03f827..a62843c5 100644 --- a/examples/attribute-form.html +++ b/examples/attribute-form.html @@ -23,6 +23,9 @@

Attribute Form

information read from the DescribeFeatureType document. For example the "STATE_NAME", "STATE_FIPS", "SUB_REGION", and "STATE_ABBR" text fields indicate errors if more than 5 characters are entered.

+ +

Field labels have been formatted with templates, so that a star is + displayed when the field is required.

Note that the js is not minified so it is readable. See attribute-form.js.

diff --git a/examples/attribute-form.js b/examples/attribute-form.js index 98bb25df..49c0f4b5 100644 --- a/examples/attribute-form.js +++ b/examples/attribute-form.js @@ -35,7 +35,18 @@ Ext.onReady(function() { }, plugins: [ new GeoExt.plugins.AttributeForm({ - attributeStore: attributeStore + attributeStore: attributeStore, + recordToFieldOptions: { + labelTpl: new Ext.XTemplate( + '{name}{[this.getStar(values)]}', { + compiled: true, + disableFormats: true, + getStar: function(v) { + return v.nillable ? '' : ' *'; + } + } + ) + } }) ] }); diff --git a/examples/data/describe_feature_type.xml b/examples/data/describe_feature_type.xml index ef8c3100..cc16ab96 100644 --- a/examples/data/describe_feature_type.xml +++ b/examples/data/describe_feature_type.xml @@ -1 +1 @@ - + diff --git a/lib/GeoExt/widgets/form.js b/lib/GeoExt/widgets/form.js index c95c7d42..afdc5934 100644 --- a/lib/GeoExt/widgets/form.js +++ b/lib/GeoExt/widgets/form.js @@ -101,6 +101,38 @@ GeoExt.form.CONTAINS = 3; * to apply to the field label if the field is not nillable (that is, * the corresponding record has the "nillable" attribute set to ``false``). * Default is ``"font-weigth: bold;"``. + * * labelTpl - ``Ext.Template`` or ``String`` or ``Array`` If set, + * the field label is obtained by applying the record's data hash to this + * template. This allows for very customizable field labels. + * See for instance : + * + * .. code-block:: javascript + * + * var formPanel = new Ext.form.FormPanel({ + * autoScroll: true, + * plugins: [ + * new GeoExt.plugins.AttributeForm({ + * attributeStore: store, + * recordToFieldOptions: { + * mandatoryFieldLabelStyle: 'font-style:italic;', + * labelTpl: new Ext.XTemplate( + * '{name}', { + * compiled: true, + * disableFormats: true, + * getTip: function(v) { + * if (!v.type) { + * return ''; + * } + * var type = v.type.split(":").pop(); + * return OpenLayers.i18n(type) + + * (v.nillable ? '' : ' (required)'); + * } + * } + * ) + * } + * }) + * ] + * }); * * :return: ``Object`` An object literal with a xtype property, use * ``Ext.ComponentMgr.create`` (or ``Ext.create`` in Ext 3) to create @@ -120,19 +152,24 @@ GeoExt.form.recordToField = function(record, options) { // field, just return it return type; } - + type = type.split(":").pop(); // remove ns prefix + var field; var name = record.get("name"); - var label = record.get("label"); var restriction = record.get("restriction") || {}; var nillable = record.get("nillable") || false; - // use name for label if label isn't defined in the record - if(label == null) { + var label = record.get("label"); + var labelTpl = options.labelTpl; + if (labelTpl) { + var tpl = (labelTpl instanceof Ext.Template) ? + labelTpl : + new Ext.XTemplate(labelTpl); + label = tpl.apply(record.data); + } else if (label == null) { + // use name for label if label isn't defined in the record label = name; } - - type = type.split(":").pop(); // remove ns prefix var baseOptions = { name: name, diff --git a/tests/lib/GeoExt/widgets/form.html b/tests/lib/GeoExt/widgets/form.html index eb146154..b6731218 100644 --- a/tests/lib/GeoExt/widgets/form.html +++ b/tests/lib/GeoExt/widgets/form.html @@ -210,7 +210,7 @@ } function test_recordToField(t) { - t.plan(26); + t.plan(28); // set up @@ -270,6 +270,23 @@ field = Ext.ComponentMgr.create(field); t.eq(field.labelStyle, 'font-style:italic;', "[label] non nillable field custom labelStyle is correct"); + // label tip + field = GeoExt.form.recordToField(store.getAt(0), { + labelTpl: new Ext.XTemplate('{name}{[this.getStar(values)]}', { + getStar: function(v) { + return (v.nillable ? '':' *'); + } + }) + }); + field = Ext.ComponentMgr.create(field); + t.eq(field.fieldLabel, 'STATE_NAME *', "[label template] template is applied successfully"); + + field = GeoExt.form.recordToField(store.getAt(0), { + labelTpl: '{name} foo' + }); + field = Ext.ComponentMgr.create(field); + t.eq(field.fieldLabel, 'STATE_NAME foo', "[label template] labelTpl accepts strings too"); + // txt field = GeoExt.form.recordToField(store.getAt(0)); field = Ext.ComponentMgr.create(field);