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.BasicForm
14 * @extends Roo.util.Observable
15 * Supplies the functionality to do "actions" on forms and initialize Roo.form.Field types on existing markup.
17 * @param {String/HTMLElement/Roo.Element} el The form element or its id
18 * @param {Object} config Configuration options
20 Roo.form.BasicForm = function(el, config){
23 Roo.apply(this, config);
25 * The Roo.form.Field items in this form.
26 * @type MixedCollection
30 this.items = new Roo.util.MixedCollection(false, function(o){
31 return o.id || (o.id = Roo.id());
36 * Fires before any action is performed. Return false to cancel the action.
38 * @param {Action} action The action to be performed
43 * Fires when an action fails.
45 * @param {Action} action The action that failed
49 * @event actioncomplete
50 * Fires when an action is completed.
52 * @param {Action} action The action that completed
59 Roo.form.BasicForm.superclass.constructor.call(this);
62 Roo.extend(Roo.form.BasicForm, Roo.util.Observable, {
64 * @cfg {String} method
65 * The request method to use (GET or POST) for form actions if one isn't supplied in the action options.
68 * @cfg {DataReader} reader
69 * An Roo.data.DataReader (e.g. {@link Roo.data.XmlReader}) to be used to read data when executing "load" actions.
70 * This is optional as there is built-in support for processing JSON.
73 * @cfg {DataReader} errorReader
74 * An Roo.data.DataReader (e.g. {@link Roo.data.XmlReader}) to be used to read data when reading validation errors on "submit" actions.
75 * This is completely optional as there is built-in support for processing JSON.
79 * The URL to use for form actions if one isn't supplied in the action options.
82 * @cfg {Boolean} fileUpload
83 * Set to true if this form is a file upload.
87 * @cfg {Object} baseParams
88 * Parameters to pass with all requests. e.g. baseParams: {id: '123', foo: 'bar'}.
93 * @cfg {Number} timeout Timeout for form actions in seconds (default is 30 seconds).
101 * @cfg {Boolean} trackResetOnLoad If set to true, form.reset() resets to the last loaded
102 * or setValues() data instead of when the form was first created.
104 trackResetOnLoad : false,
108 * childForms - used for multi-tab forms
114 * allItems - full list of fields.
120 * By default wait messages are displayed with Roo.MessageBox.wait. You can target a specific
121 * element by passing it or its id or mask the form itself by passing in true.
124 waitMsgTarget : false,
127 initEl : function(el){
128 this.el = Roo.get(el);
129 this.id = this.el.id || Roo.id();
130 this.el.on('submit', this.onSubmit, this);
131 this.el.addClass('x-form');
135 onSubmit : function(e){
140 * Returns true if client-side validation on the form is successful.
143 isValid : function(){
145 this.items.each(function(f){
154 * Returns true if any fields in this form have changed since their original load.
157 isDirty : function(){
159 this.items.each(function(f){
169 * Performs a predefined action (submit or load) or custom actions you define on this form.
170 * @param {String} actionName The name of the action type
171 * @param {Object} options (optional) The options to pass to the action. All of the config options listed
172 * below are supported by both the submit and load actions unless otherwise noted (custom actions could also
173 * accept other config options):
175 Property Type Description
176 ---------------- --------------- ----------------------------------------------------------------------------------
177 url String The url for the action (defaults to the form's url)
178 method String The form method to use (defaults to the form's method, or POST if not defined)
179 params String/Object The params to pass (defaults to the form's baseParams, or none if not defined)
180 clientValidation Boolean Applies to submit only. Pass true to call form.isValid() prior to posting to
181 validate the form on the client (defaults to false)
183 * @return {BasicForm} this
185 doAction : function(action, options){
186 if(typeof action == 'string'){
187 action = new Roo.form.Action.ACTION_TYPES[action](this, options);
189 if(this.fireEvent('beforeaction', this, action) !== false){
190 this.beforeAction(action);
191 action.run.defer(100, action);
197 * Shortcut to do a submit action.
198 * @param {Object} options The options to pass to the action (see {@link #doAction} for details)
199 * @return {BasicForm} this
201 submit : function(options){
202 this.doAction('submit', options);
207 * Shortcut to do a load action.
208 * @param {Object} options The options to pass to the action (see {@link #doAction} for details)
209 * @return {BasicForm} this
211 load : function(options){
212 this.doAction('load', options);
217 * Persists the values in this form into the passed Roo.data.Record object in a beginEdit/endEdit block.
218 * @param {Record} record The record to edit
219 * @return {BasicForm} this
221 updateRecord : function(record){
223 var fs = record.fields;
225 var field = this.findField(f.name);
227 record.set(f.name, field.getValue());
235 * Loads an Roo.data.Record into this form.
236 * @param {Record} record The record to load
237 * @return {BasicForm} this
239 loadRecord : function(record){
240 this.setValues(record.data);
245 beforeAction : function(action){
246 var o = action.options;
249 if(this.waitMsgTarget === true){
250 this.el.mask(o.waitMsg || "Sending", 'x-mask-loading');
251 }else if(this.waitMsgTarget){
252 this.waitMsgTarget = Roo.get(this.waitMsgTarget);
253 this.waitMsgTarget.mask(o.waitMsg || "Sending", 'x-mask-loading');
255 Roo.MessageBox.wait(o.waitMsg || "Sending", o.waitTitle || this.waitTitle || 'Please Wait...');
261 afterAction : function(action, success){
262 this.activeAction = null;
263 var o = action.options;
265 if(this.waitMsgTarget === true){
267 }else if(this.waitMsgTarget){
268 this.waitMsgTarget.unmask();
270 Roo.MessageBox.updateProgress(1);
271 Roo.MessageBox.hide();
278 Roo.callback(o.success, o.scope, [this, action]);
279 this.fireEvent('actioncomplete', this, action);
283 // failure condition..
284 // we have a scenario where updates need confirming.
285 // eg. if a locking scenario exists..
286 // we look for { errors : { needs_confirm : true }} in the response.
287 if (typeof(action.result.errors.needs_confirm) != 'undefined') {
289 Roo.MessageBox.confirm(
290 "Change requires confirmation",
291 action.result.errorMsg,
296 _t.doAction('submit', { params : { _submit_confirmed : 1 } } );
306 Roo.callback(o.failure, o.scope, [this, action]);
307 // show an error message if no failed handler is set..
308 if (!this.hasListener('actionfailed')) {
309 Roo.MessageBox.alert("Error",
310 (typeof(action.result) != 'undefined' && typeof(action.result.errorMsg) != 'undefined') ?
311 action.result.errorMsg :
312 "Saving Failed, please check your entries"
316 this.fireEvent('actionfailed', this, action);
322 * Find a Roo.form.Field in this form by id, dataIndex, name or hiddenName
323 * @param {String} id The value to search for
326 findField : function(id){
327 var field = this.items.get(id);
329 this.items.each(function(f){
330 if(f.isFormField && (f.dataIndex == id || f.id == id || f.getName() == id)){
336 return field || null;
340 * Add a secondary form to this one,
341 * Used to provide tabbed forms. One form is primary, with hidden values
342 * which mirror the elements from the other forms.
344 * @param {Roo.form.Form} form to add.
347 addForm : function(form)
350 if (this.childForms.indexOf(form) > -1) {
354 this.childForms.push(form);
356 Roo.each(form.allItems, function (fe) {
358 n = typeof(fe.getName) == 'undefined' ? fe.name : fe.getName();
359 if (this.findField(n)) { // already added..
362 var add = new Roo.form.Hidden({
372 * Mark fields in this form invalid in bulk.
373 * @param {Array/Object} errors Either an array in the form [{id:'fieldId', msg:'The message'},...] or an object hash of {id: msg, id2: msg2}
374 * @return {BasicForm} this
376 markInvalid : function(errors){
377 if(errors instanceof Array){
378 for(var i = 0, len = errors.length; i < len; i++){
379 var fieldError = errors[i];
380 var f = this.findField(fieldError.id);
382 f.markInvalid(fieldError.msg);
388 if(typeof errors[id] != 'function' && (field = this.findField(id))){
389 field.markInvalid(errors[id]);
393 Roo.each(this.childForms || [], function (f) {
394 f.markInvalid(errors);
401 * Set values for fields in this form in bulk.
402 * @param {Array/Object} values Either an array in the form [{id:'fieldId', value:'foo'},...] or an object hash of {id: value, id2: value2}
403 * @return {BasicForm} this
405 setValues : function(values){
406 if(values instanceof Array){ // array of objects
407 for(var i = 0, len = values.length; i < len; i++){
409 var f = this.findField(v.id);
412 if(this.trackResetOnLoad){
413 f.originalValue = f.getValue();
417 }else{ // object hash
420 if(typeof values[id] != 'function' && (field = this.findField(id))){
422 if (field.setFromData &&
424 field.displayField &&
425 // combos' with local stores can
426 // be queried via setValue()
427 // to set their value..
428 (field.store && !field.store.isLocal)
432 sd[field.valueField] = typeof(values[field.hiddenName]) == 'undefined' ? '' : values[field.hiddenName];
433 sd[field.displayField] = typeof(values[field.name]) == 'undefined' ? '' : values[field.name];
434 field.setFromData(sd);
437 field.setValue(values[id]);
441 if(this.trackResetOnLoad){
442 field.originalValue = field.getValue();
448 Roo.each(this.childForms || [], function (f) {
456 * Returns the fields in this form as an object with key/value pairs. If multiple fields exist with the same name
457 * they are returned as an array.
458 * @param {Boolean} asString
461 getValues : function(asString){
462 if (this.childForms) {
463 // copy values from the child forms
464 Roo.each(this.childForms, function (f) {
465 this.setValues(f.getValues());
471 var fs = Roo.lib.Ajax.serializeForm(this.el.dom);
472 if(asString === true){
475 return Roo.urlDecode(fs);
479 * Returns the fields in this form as an object with key/value pairs.
480 * This differs from getValues as it calls getValue on each child item, rather than using dom data.
483 getFieldValues : function(with_hidden)
485 if (this.childForms) {
486 // copy values from the child forms
487 // should this call getFieldValues - probably not as we do not currently copy
488 // hidden fields when we generate..
489 Roo.each(this.childForms, function (f) {
490 this.setValues(f.getValues());
495 this.items.each(function(f){
499 var v = f.getValue();
500 // not sure if this supported any more..
501 if ((typeof(v) == 'object') && f.getRawValue) {
502 v = f.getRawValue() ; // dates..
504 // combo boxes where name != hiddenName...
505 if (f.name != f.getName()) {
506 ret[f.name] = f.getRawValue();
508 ret[f.getName()] = v;
515 * Clears all invalid messages in this form.
516 * @return {BasicForm} this
518 clearInvalid : function(){
519 this.items.each(function(f){
523 Roo.each(this.childForms || [], function (f) {
533 * @return {BasicForm} this
536 this.items.each(function(f){
540 Roo.each(this.childForms || [], function (f) {
549 * Add Roo.form components to this form.
550 * @param {Field} field1
551 * @param {Field} field2 (optional)
552 * @param {Field} etc (optional)
553 * @return {BasicForm} this
556 this.items.addAll(Array.prototype.slice.call(arguments, 0));
562 * Removes a field from the items collection (does NOT remove its markup).
563 * @param {Field} field
564 * @return {BasicForm} this
566 remove : function(field){
567 this.items.remove(field);
572 * Looks at the fields in this form, checks them for an id attribute,
573 * and calls applyTo on the existing dom element with that id.
574 * @return {BasicForm} this
577 this.items.each(function(f){
578 if(f.isFormField && !f.rendered && document.getElementById(f.id)){ // if the element exists
586 * Calls {@link Ext#apply} for all fields in this form with the passed object.
587 * @param {Object} values
588 * @return {BasicForm} this
590 applyToFields : function(o){
591 this.items.each(function(f){
598 * Calls {@link Ext#applyIf} for all field in this form with the passed object.
599 * @param {Object} values
600 * @return {BasicForm} this
602 applyIfToFields : function(o){
603 this.items.each(function(f){
611 Roo.BasicForm = Roo.form.BasicForm;