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.Form
14 * @extends Roo.form.BasicForm
15 * Adds the ability to dynamically render forms with JavaScript to {@link Roo.form.BasicForm}.
17 * @param {Object} config Configuration options
19 Roo.form.Form = function(config){
22 xitems = config.items;
27 Roo.form.Form.superclass.constructor.call(this, null, config);
28 this.url = this.url || this.action;
30 this.root = new Roo.form.Layout(Roo.applyIf({
34 this.active = this.root;
36 * Array of all the buttons that have been added to this form via {@link addButton}
43 * @event clientvalidation
44 * If the monitorValid config option is true, this event fires repetitively to notify of valid state
46 * @param {Boolean} valid true if the form has passed client-side validation
48 clientvalidation: true,
51 * Fires when the form is rendered
52 * @param {Roo.form.Form} form
57 Roo.each(xitems, this.addxtype, this);
63 Roo.extend(Roo.form.Form, Roo.form.BasicForm, {
65 * @cfg {Number} labelWidth The width of labels. This property cascades to child containers.
68 * @cfg {String} itemCls A css class to apply to the x-form-item of fields. This property cascades to child containers.
71 * @cfg {String} buttonAlign Valid values are "left," "center" and "right" (defaults to "center")
76 * @cfg {Number} minButtonWidth Minimum width of all buttons in pixels (defaults to 75)
81 * @cfg {String} labelAlign Valid values are "left," "top" and "right" (defaults to "left").
82 * This property cascades to child containers if not set.
87 * @cfg {Boolean} monitorValid If true the form monitors its valid state <b>client-side</b> and
88 * fires a looping event with that state. This is required to bind buttons to the valid
89 * state using the config value formBind:true on the button.
94 * @cfg {Number} monitorPoll The milliseconds to poll valid state, ignored if monitorValid is not true (defaults to 200)
99 * childForms - used for multi-tab forms
105 * Opens a new {@link Roo.form.Column} container in the layout stack. If fields are passed after the config, the
106 * fields are added and the column is closed. If no fields are passed the column remains open
107 * until end() is called.
108 * @param {Object} config The config to pass to the column
109 * @param {Field} field1 (optional)
110 * @param {Field} field2 (optional)
111 * @param {Field} etc (optional)
112 * @return Column The column container object
114 column : function(c){
115 var col = new Roo.form.Column(c);
117 if(arguments.length > 1){ // duplicate code required because of Opera
118 this.add.apply(this, Array.prototype.slice.call(arguments, 1));
125 * Opens a new {@link Roo.form.FieldSet} container in the layout stack. If fields are passed after the config, the
126 * fields are added and the fieldset is closed. If no fields are passed the fieldset remains open
127 * until end() is called.
128 * @param {Object} config The config to pass to the fieldset
129 * @param {Field} field1 (optional)
130 * @param {Field} field2 (optional)
131 * @param {Field} etc (optional)
132 * @return FieldSet The fieldset container object
134 fieldset : function(c){
135 var fs = new Roo.form.FieldSet(c);
137 if(arguments.length > 1){ // duplicate code required because of Opera
138 this.add.apply(this, Array.prototype.slice.call(arguments, 1));
145 * Opens a new {@link Roo.form.Layout} container in the layout stack. If fields are passed after the config, the
146 * fields are added and the container is closed. If no fields are passed the container remains open
147 * until end() is called.
148 * @param {Object} config The config to pass to the Layout
149 * @param {Field} field1 (optional)
150 * @param {Field} field2 (optional)
151 * @param {Field} etc (optional)
152 * @return Layout The container object
154 container : function(c){
155 var l = new Roo.form.Layout(c);
157 if(arguments.length > 1){ // duplicate code required because of Opera
158 this.add.apply(this, Array.prototype.slice.call(arguments, 1));
165 * Opens the passed container in the layout stack. The container can be any {@link Roo.form.Layout} or subclass.
166 * @param {Object} container A Roo.form.Layout or subclass of Layout
167 * @return {Form} this
170 // cascade label info
171 Roo.applyIf(c, {'labelAlign': this.active.labelAlign, 'labelWidth': this.active.labelWidth, 'itemCls': this.active.itemCls});
172 this.active.stack.push(c);
173 c.ownerCt = this.active;
179 * Closes the current open container
180 * @return {Form} this
183 if(this.active == this.root){
186 this.active = this.active.ownerCt;
191 * Add Roo.form components to the current open container (e.g. column, fieldset, etc.). Fields added via this method
192 * can also be passed with an additional property of fieldLabel, which if supplied, will provide the text to display
193 * as the label of the field.
194 * @param {Field} field1
195 * @param {Field} field2 (optional)
196 * @param {Field} etc. (optional)
197 * @return {Form} this
200 this.active.stack.push.apply(this.active.stack, arguments);
201 this.allItems.push.apply(this.allItems,arguments);
203 for(var i = 0, a = arguments, len = a.length; i < len; i++) {
204 if(a[i].isFormField){
209 Roo.form.Form.superclass.add.apply(this, r);
215 * Add a secondary form to this one,
216 * Used to provide tabbed forms. One form is primary, with hidden values
217 * which mirror the elements from the other forms.
219 * @param {Roo.form.Form} form to add.
224 addForm : function(form){
226 this.childForms.push(form);
227 form.allItems.each(function (fe) {
229 if (this.findField(fe.name)) { // already added..
232 this.add( new Roo.form.Hidden({
243 * Find any element that has been added to a form, using it's ID or name
244 * This can include framesets, columns etc. along with regular fields..
245 * @param {String} id - id or name to find.
247 * @return {Element} e - or false if nothing found.
249 findbyId : function(id)
255 Ext.each(this.allItems, function(f){
256 if (f.id == id || f.name == id ){
267 * Render this form into the passed container. This should only be called once!
268 * @param {String/HTMLElement/Element} container The element this component should be rendered into
269 * @return {Form} this
271 render : function(ct){
273 var o = this.autoCreate || {
275 method : this.method || 'POST',
276 id : this.id || Roo.id()
278 this.initEl(ct.createChild(o));
280 this.root.render(this.el);
282 this.items.each(function(f){
283 f.render('x-form-el-'+f.id);
286 if(this.buttons.length > 0){
287 // tables are required to maintain order and for correct IE layout
288 var tb = this.el.createChild({cls:'x-form-btns-ct', cn: {
289 cls:"x-form-btns x-form-btns-"+this.buttonAlign,
290 html:'<table cellspacing="0"><tbody><tr></tr></tbody></table><div class="x-clear"></div>'
292 var tr = tb.getElementsByTagName('tr')[0];
293 for(var i = 0, len = this.buttons.length; i < len; i++) {
294 var b = this.buttons[i];
295 var td = document.createElement('td');
296 td.className = 'x-form-btn-td';
297 b.render(tr.appendChild(td));
300 if(this.monitorValid){ // initialize after render
301 this.startMonitoring();
303 this.fireEvent('rendered', this);
308 * Adds a button to the footer of the form - this <b>must</b> be called before the form is rendered.
309 * @param {String/Object} config A string becomes the button text, an object can either be a Button config
310 * object or a valid Roo.DomHelper element config
311 * @param {Function} handler The function called when the button is clicked
312 * @param {Object} scope (optional) The scope of the handler function
313 * @return {Roo.Button}
315 addButton : function(config, handler, scope){
319 minWidth: this.minButtonWidth,
322 if(typeof config == "string"){
325 Roo.apply(bc, config);
327 var btn = new Roo.Button(null, bc);
328 this.buttons.push(btn);
333 * Adds a series of form elements (using the xtype property as the factory method.
334 * Valid xtypes are: TextField, TextArea .... Button, Layout, FieldSet, Column, (and 'end' to close a block)
335 * @param {Object} config
338 addxtype : function()
340 var ar = Array.prototype.slice.call(arguments, 0);
342 for(var i = 0; i < ar.length; i++) {
344 continue; // skip -- if this happends something invalid got sent, we
345 // should ignore it, as basically that interface element will not show up
346 // and that should be pretty obvious!!
349 if (Roo.form[ar[i].xtype]) {
351 var fe = Roo.factory(ar[i], Roo.form);
357 fe.store.form = this;
362 this.allItems.push(fe);
363 if (fe.items && fe.addxtype) {
364 fe.addxtype.apply(fe, fe.items);
374 // console.log('adding ' + ar[i].xtype);
376 if (ar[i].xtype == 'Button') {
377 //console.log('adding button');
378 //console.log(ar[i]);
379 this.addButton(ar[i]);
380 this.allItems.push(fe);
384 if (ar[i].xtype == 'end') { // so we can add fieldsets... / layout etc.
385 alert('end is not supported on xtype any more, use items');
387 // //console.log('adding end');
395 * Starts monitoring of the valid state of this form. Usually this is done by passing the config
396 * option "monitorValid"
398 startMonitoring : function(){
402 run : this.bindHandler,
403 interval : this.monitorPoll || 200,
410 * Stops monitoring of the valid state of this form
412 stopMonitoring : function(){
417 bindHandler : function(){
419 return false; // stops binding
422 this.items.each(function(f){
423 if(!f.isValid(true)){
428 for(var i = 0, len = this.buttons.length; i < len; i++){
429 var btn = this.buttons[i];
430 if(btn.formBind === true && btn.disabled === valid){
431 btn.setDisabled(!valid);
434 this.fireEvent('clientvalidation', this, valid);
448 Roo.Form = Roo.form.Form;