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)
100 * Opens a new {@link Roo.form.Column} container in the layout stack. If fields are passed after the config, the
101 * fields are added and the column is closed. If no fields are passed the column remains open
102 * until end() is called.
103 * @param {Object} config The config to pass to the column
104 * @param {Field} field1 (optional)
105 * @param {Field} field2 (optional)
106 * @param {Field} etc (optional)
107 * @return Column The column container object
109 column : function(c){
110 var col = new Roo.form.Column(c);
112 if(arguments.length > 1){ // duplicate code required because of Opera
113 this.add.apply(this, Array.prototype.slice.call(arguments, 1));
120 * Opens a new {@link Roo.form.FieldSet} container in the layout stack. If fields are passed after the config, the
121 * fields are added and the fieldset is closed. If no fields are passed the fieldset remains open
122 * until end() is called.
123 * @param {Object} config The config to pass to the fieldset
124 * @param {Field} field1 (optional)
125 * @param {Field} field2 (optional)
126 * @param {Field} etc (optional)
127 * @return FieldSet The fieldset container object
129 fieldset : function(c){
130 var fs = new Roo.form.FieldSet(c);
132 if(arguments.length > 1){ // duplicate code required because of Opera
133 this.add.apply(this, Array.prototype.slice.call(arguments, 1));
140 * Opens a new {@link Roo.form.Layout} container in the layout stack. If fields are passed after the config, the
141 * fields are added and the container is closed. If no fields are passed the container remains open
142 * until end() is called.
143 * @param {Object} config The config to pass to the Layout
144 * @param {Field} field1 (optional)
145 * @param {Field} field2 (optional)
146 * @param {Field} etc (optional)
147 * @return Layout The container object
149 container : function(c){
150 var l = new Roo.form.Layout(c);
152 if(arguments.length > 1){ // duplicate code required because of Opera
153 this.add.apply(this, Array.prototype.slice.call(arguments, 1));
160 * Opens the passed container in the layout stack. The container can be any {@link Roo.form.Layout} or subclass.
161 * @param {Object} container A Roo.form.Layout or subclass of Layout
162 * @return {Form} this
165 // cascade label info
166 Roo.applyIf(c, {'labelAlign': this.active.labelAlign, 'labelWidth': this.active.labelWidth, 'itemCls': this.active.itemCls});
167 this.active.stack.push(c);
168 c.ownerCt = this.active;
174 * Closes the current open container
175 * @return {Form} this
178 if(this.active == this.root){
181 this.active = this.active.ownerCt;
186 * Add Roo.form components to the current open container (e.g. column, fieldset, etc.). Fields added via this method
187 * can also be passed with an additional property of fieldLabel, which if supplied, will provide the text to display
188 * as the label of the field.
189 * @param {Field} field1
190 * @param {Field} field2 (optional)
191 * @param {Field} etc. (optional)
192 * @return {Form} this
195 this.active.stack.push.apply(this.active.stack, arguments);
196 this.allItems.push.apply(this.allItems,arguments);
198 for(var i = 0, a = arguments, len = a.length; i < len; i++) {
199 if(a[i].isFormField){
204 Roo.form.Form.superclass.add.apply(this, r);
214 * Find any element that has been added to a form, using it's ID or name
215 * This can include framesets, columns etc. along with regular fields..
216 * @param {String} id - id or name to find.
218 * @return {Element} e - or false if nothing found.
220 findbyId : function(id)
226 Ext.each(this.allItems, function(f){
227 if (f.id == id || f.name == id ){
238 * Render this form into the passed container. This should only be called once!
239 * @param {String/HTMLElement/Element} container The element this component should be rendered into
240 * @return {Form} this
242 render : function(ct){
244 var o = this.autoCreate || {
246 method : this.method || 'POST',
247 id : this.id || Roo.id()
249 this.initEl(ct.createChild(o));
251 this.root.render(this.el);
253 if (this.progressUrl && !this.form.findField( 'UPLOAD_IDENTIFIER')) {
254 // push a hidden field onto the list of fields..
255 this.items.unshift(0, Roo.factory( {
258 name : 'UPLOAD_IDENTIFIER'
264 this.items.each(function(f){
265 f.render('x-form-el-'+f.id);
268 if(this.buttons.length > 0){
269 // tables are required to maintain order and for correct IE layout
270 var tb = this.el.createChild({cls:'x-form-btns-ct', cn: {
271 cls:"x-form-btns x-form-btns-"+this.buttonAlign,
272 html:'<table cellspacing="0"><tbody><tr></tr></tbody></table><div class="x-clear"></div>'
274 var tr = tb.getElementsByTagName('tr')[0];
275 for(var i = 0, len = this.buttons.length; i < len; i++) {
276 var b = this.buttons[i];
277 var td = document.createElement('td');
278 td.className = 'x-form-btn-td';
279 b.render(tr.appendChild(td));
282 if(this.monitorValid){ // initialize after render
283 this.startMonitoring();
285 this.fireEvent('rendered', this);
290 * Adds a button to the footer of the form - this <b>must</b> be called before the form is rendered.
291 * @param {String/Object} config A string becomes the button text, an object can either be a Button config
292 * object or a valid Roo.DomHelper element config
293 * @param {Function} handler The function called when the button is clicked
294 * @param {Object} scope (optional) The scope of the handler function
295 * @return {Roo.Button}
297 addButton : function(config, handler, scope){
301 minWidth: this.minButtonWidth,
304 if(typeof config == "string"){
307 Roo.apply(bc, config);
309 var btn = new Roo.Button(null, bc);
310 this.buttons.push(btn);
315 * Adds a series of form elements (using the xtype property as the factory method.
316 * Valid xtypes are: TextField, TextArea .... Button, Layout, FieldSet, Column, (and 'end' to close a block)
317 * @param {Object} config
320 addxtype : function()
322 var ar = Array.prototype.slice.call(arguments, 0);
324 for(var i = 0; i < ar.length; i++) {
326 continue; // skip -- if this happends something invalid got sent, we
327 // should ignore it, as basically that interface element will not show up
328 // and that should be pretty obvious!!
331 if (Roo.form[ar[i].xtype]) {
333 var fe = Roo.factory(ar[i], Roo.form);
339 fe.store.form = this;
344 this.allItems.push(fe);
345 if (fe.items && fe.addxtype) {
346 fe.addxtype.apply(fe, fe.items);
356 // console.log('adding ' + ar[i].xtype);
358 if (ar[i].xtype == 'Button') {
359 //console.log('adding button');
360 //console.log(ar[i]);
361 this.addButton(ar[i]);
362 this.allItems.push(fe);
366 if (ar[i].xtype == 'end') { // so we can add fieldsets... / layout etc.
367 alert('end is not supported on xtype any more, use items');
369 // //console.log('adding end');
377 * Starts monitoring of the valid state of this form. Usually this is done by passing the config
378 * option "monitorValid"
380 startMonitoring : function(){
384 run : this.bindHandler,
385 interval : this.monitorPoll || 200,
392 * Stops monitoring of the valid state of this form
394 stopMonitoring : function(){
399 bindHandler : function(){
401 return false; // stops binding
404 this.items.each(function(f){
405 if(!f.isValid(true)){
410 for(var i = 0, len = this.buttons.length; i < len; i++){
411 var btn = this.buttons[i];
412 if(btn.formBind === true && btn.disabled === valid){
413 btn.setDisabled(!valid);
416 this.fireEvent('clientvalidation', this, valid);
430 Roo.Form = Roo.form.Form;