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 * Opens a new {@link Roo.form.Column} container in the layout stack. If fields are passed after the config, the
100 * fields are added and the column is closed. If no fields are passed the column remains open
101 * until end() is called.
102 * @param {Object} config The config to pass to the column
103 * @param {Field} field1 (optional)
104 * @param {Field} field2 (optional)
105 * @param {Field} etc (optional)
106 * @return Column The column container object
108 column : function(c){
109 var col = new Roo.form.Column(c);
111 if(arguments.length > 1){ // duplicate code required because of Opera
112 this.add.apply(this, Array.prototype.slice.call(arguments, 1));
119 * Opens a new {@link Roo.form.FieldSet} container in the layout stack. If fields are passed after the config, the
120 * fields are added and the fieldset is closed. If no fields are passed the fieldset remains open
121 * until end() is called.
122 * @param {Object} config The config to pass to the fieldset
123 * @param {Field} field1 (optional)
124 * @param {Field} field2 (optional)
125 * @param {Field} etc (optional)
126 * @return FieldSet The fieldset container object
128 fieldset : function(c){
129 var fs = new Roo.form.FieldSet(c);
131 if(arguments.length > 1){ // duplicate code required because of Opera
132 this.add.apply(this, Array.prototype.slice.call(arguments, 1));
139 * Opens a new {@link Roo.form.Layout} container in the layout stack. If fields are passed after the config, the
140 * fields are added and the container is closed. If no fields are passed the container remains open
141 * until end() is called.
142 * @param {Object} config The config to pass to the Layout
143 * @param {Field} field1 (optional)
144 * @param {Field} field2 (optional)
145 * @param {Field} etc (optional)
146 * @return Layout The container object
148 container : function(c){
149 var l = new Roo.form.Layout(c);
151 if(arguments.length > 1){ // duplicate code required because of Opera
152 this.add.apply(this, Array.prototype.slice.call(arguments, 1));
159 * Opens the passed container in the layout stack. The container can be any {@link Roo.form.Layout} or subclass.
160 * @param {Object} container A Roo.form.Layout or subclass of Layout
161 * @return {Form} this
164 // cascade label info
165 Roo.applyIf(c, {'labelAlign': this.active.labelAlign, 'labelWidth': this.active.labelWidth, 'itemCls': this.active.itemCls});
166 this.active.stack.push(c);
167 c.ownerCt = this.active;
173 * Closes the current open container
174 * @return {Form} this
177 if(this.active == this.root){
180 this.active = this.active.ownerCt;
185 * Add Roo.form components to the current open container (e.g. column, fieldset, etc.). Fields added via this method
186 * can also be passed with an additional property of fieldLabel, which if supplied, will provide the text to display
187 * as the label of the field.
188 * @param {Field} field1
189 * @param {Field} field2 (optional)
190 * @param {Field} etc. (optional)
191 * @return {Form} this
194 this.active.stack.push.apply(this.active.stack, arguments);
195 this.allItems.push.apply(this.allItems,arguments);
197 for(var i = 0, a = arguments, len = a.length; i < len; i++) {
198 if(a[i].isFormField){
203 Roo.form.Form.superclass.add.apply(this, r);
209 * Add a secondary form to this one,
210 * Used to provide tabbed forms. One form is primary, with hidden values
211 * which mirror the elements from the other forms.
217 addForm : function(form){
219 this.forms.push(form);
220 form.allItems.each(function (fe) {
222 if (this.findField(fe.name)) { // already added..
225 this.add( new Roo.form.Hidden({
237 * Find any element that has been added to a form, using it's ID or name
238 * This can include framesets, columns etc. along with regular fields..
239 * @param {String} id - id or name to find.
241 * @return {Element} e - or false if nothing found.
243 findbyId : function(id)
249 Ext.each(this.allItems, function(f){
250 if (f.id == id || f.name == id ){
261 * Render this form into the passed container. This should only be called once!
262 * @param {String/HTMLElement/Element} container The element this component should be rendered into
263 * @return {Form} this
265 render : function(ct){
267 var o = this.autoCreate || {
269 method : this.method || 'POST',
270 id : this.id || Roo.id()
272 this.initEl(ct.createChild(o));
274 this.root.render(this.el);
276 this.items.each(function(f){
277 f.render('x-form-el-'+f.id);
280 if(this.buttons.length > 0){
281 // tables are required to maintain order and for correct IE layout
282 var tb = this.el.createChild({cls:'x-form-btns-ct', cn: {
283 cls:"x-form-btns x-form-btns-"+this.buttonAlign,
284 html:'<table cellspacing="0"><tbody><tr></tr></tbody></table><div class="x-clear"></div>'
286 var tr = tb.getElementsByTagName('tr')[0];
287 for(var i = 0, len = this.buttons.length; i < len; i++) {
288 var b = this.buttons[i];
289 var td = document.createElement('td');
290 td.className = 'x-form-btn-td';
291 b.render(tr.appendChild(td));
294 if(this.monitorValid){ // initialize after render
295 this.startMonitoring();
297 this.fireEvent('rendered', this);
302 * Adds a button to the footer of the form - this <b>must</b> be called before the form is rendered.
303 * @param {String/Object} config A string becomes the button text, an object can either be a Button config
304 * object or a valid Roo.DomHelper element config
305 * @param {Function} handler The function called when the button is clicked
306 * @param {Object} scope (optional) The scope of the handler function
307 * @return {Roo.Button}
309 addButton : function(config, handler, scope){
313 minWidth: this.minButtonWidth,
316 if(typeof config == "string"){
319 Roo.apply(bc, config);
321 var btn = new Roo.Button(null, bc);
322 this.buttons.push(btn);
327 * Adds a series of form elements (using the xtype property as the factory method.
328 * Valid xtypes are: TextField, TextArea .... Button, Layout, FieldSet, Column, (and 'end' to close a block)
329 * @param {Object} config
332 addxtype : function()
334 var ar = Array.prototype.slice.call(arguments, 0);
336 for(var i = 0; i < ar.length; i++) {
338 continue; // skip -- if this happends something invalid got sent, we
339 // should ignore it, as basically that interface element will not show up
340 // and that should be pretty obvious!!
343 if (Roo.form[ar[i].xtype]) {
345 var fe = Roo.factory(ar[i], Roo.form);
351 fe.store.form = this;
356 this.allItems.push(fe);
357 if (fe.items && fe.addxtype) {
358 fe.addxtype.apply(fe, fe.items);
368 // console.log('adding ' + ar[i].xtype);
370 if (ar[i].xtype == 'Button') {
371 //console.log('adding button');
372 //console.log(ar[i]);
373 this.addButton(ar[i]);
374 this.allItems.push(fe);
378 if (ar[i].xtype == 'end') { // so we can add fieldsets... / layout etc.
379 alert('end is not supported on xtype any more, use items');
381 // //console.log('adding end');
389 * Starts monitoring of the valid state of this form. Usually this is done by passing the config
390 * option "monitorValid"
392 startMonitoring : function(){
396 run : this.bindHandler,
397 interval : this.monitorPoll || 200,
404 * Stops monitoring of the valid state of this form
406 stopMonitoring : function(){
411 bindHandler : function(){
413 return false; // stops binding
416 this.items.each(function(f){
417 if(!f.isValid(true)){
422 for(var i = 0, len = this.buttons.length; i < len; i++){
423 var btn = this.buttons[i];
424 if(btn.formBind === true && btn.disabled === valid){
425 btn.setDisabled(!valid);
428 this.fireEvent('clientvalidation', this, valid);
442 Roo.Form = Roo.form.Form;