ludo.form.Manager

ludo.form.


new ludo.form.Manager(config)

Form management class. An instance of this class is automatically created for a view if the config.form property is set.

new ludo.View({
form:{ ... }

You'll get access to the methods of this class using view.getForm().

For demo, see form-components.php

Parameters:
Name Type Description
config Object
Properties
Name Type Description
submit Object

Submit configuration object

Properties
Name Type Description
listeners Object

Submit listeners

read Object

Read data from server configuration object

Properties
Name Type Description
autoload Boolean

True to autoload form data when rendered.

populate Boolean

True to automatically populate form fields with JSON from server.

listeners Object

Read data from server listeners.

listeners Object

The form fires events when something is changed with one of the child form views(recursive).
It is convenient to place event handlers here instead of adding them to the individual form views.
Example: create a form.change event to listen to all changes to child form views.

hiddenFields Array

Array with name of form fields. example: hiddenFields:["id"]. There are noe <input type="hidden">
in ludoJS. Instead, you define hiddenFields which later can be populated using view.getForm().val(key, value).

Source:
Fires:
  • ludo.form.Manager#change Event fired when the value of one of the child form view is changed(recursive)., arguments: 1) name of changed element, 2) Value of changed element, 3) form.Manager and 4) form.Element(the element changed)
  • ludo.form.Manager#event:valid Event fired when all child form views have a valid value.
  • ludo.form.Manager#event:invalid Event fired when one or more child form views have invalid value.
  • ludo.form.Manager#clean A form is considered clean when none of it's values has changed from it's original. Otherwise it's considered dirty. The clean event is fired when the form is clean.event:
  • ludo.form.Manager#event:dirty Fired when the form is dirty.
  • ludo.form.Manager#submit.init Fired before form is submitted. The submit. listeners are added via the "submit" object.event: See example below.
  • ludo.form.Manager#submit.event:success Fired after successful form submission.
  • ludo.form.Manager#submit.event:fail Fired after failed form submission.
  • ludo.form.Manager#read.init Fired before loading form data from server. the read. listeners are added via the "read" object.event: See example below.
  • ludo.form.Manager#read.event:success Fired after successfully loading data from server
  • ludo.form.Manager#read.event:fail Fired when loading data from server failed.
Example
var view = new ludo.View({
    form:{
            hiddenFields: ['id'], // Hidden fields which could be populated using ludo.get('myWindow).getForm().set('id', 100);
            submit:{
                url: '../controller.php',
                data: {
                    submit:1
                },
                listeners:{
                    'success': function(json, form){
                        new ludo.dialog.Alert({
                            title:'Thank you!',
                            resizable:true,
                            html:'Thank you. Your data has been saved..'
                        });

                        form.commit();
                    },
                    'fail': function(text, error, form){
                        // do something on failure
                    }
                }
            },

            read: {
                autoload:true,  // autoload data on create
                url: 'form-data.json', // read url
                keys:['id'], // array of form values to add to the view request
                listeners:{
                    'success': function(json, form){
                        form.clear();
                        form.populate(json);
                    },
                    'fail': function(text, error, form){
                        // do something on failure
                    }
                }
            }
    }
	children:[
		{ type:'form.Text', label:'First name', name:'firstname' },
		{ type:'form.Text', label:'Last name', name:'lastname' }
	]
});

view.getForm().val("firstname", "John"); // update all form views with name "firstname" with the value "John"
view.getForm().val("lastname", "Doe"); // update all form veiws with name "lastname" with the value "Doe"

// Return form values as JSON { "firstname": "John", "lastname": "Doe" }
var json = view.getForm().values();

Members


view

Source:

Methods


clear()

Clear value of all child form views back to blank or default view value

Source:

commit()

Commit all form Views. This will reset the dirty flag. The dirty flag is true when on form view has been updated.
A later call to reset will reset all form Views back to the value it had when commit was called.
with a new value.

Source:

get(name) → {String|Number|Object}

Return value of form element.

Parameters:
Name Type Description
name String

Name of form element

Source:
Returns:
Type
String | Number | Object

getFormElements()

Get all form elements, store them in an array and add valid and invalid events to them

Source:

isDirty()

Returns true if a form View has been updated with a new value. This is useful for handling disabling/enabling of buttons
based on changes made to the form. The dirty flag can be reset by calling the ludo.form.Manager#commit method. This will
call commit on all form views.

Source:

isValid()

Returns true when all child form views are valid

Source:

populate(json)

Populate form fields with data from JSON object

Parameters:
Name Type Description
json Object

JSON object

Source:
Example
var view = new ludo.View({
	renderTo:document.body,
	layout:{ type:'linear', orientation:'vertical', width:'matchParent', height:'matchParent' },
	children:[
		{ type:'form.Text', name:'firstname' },
		{ type:'form.Text', name:'lastname' }
	]
});
// Update form views firstname and lastname with values from JSON
view.getForm.populate({
	"firstname" : "Jane", "lastname": "Anderson"
});

read()

Read form values from the server. This method triggers the events read.init and read.success|read.fail.
This method will be called during view creation if read.autoload is set to true.

Source:
Example
var v = new ludo.View({
         form:{
            hiddenFields: ['id'],
            read: {
                url: 'form-data.json', // read url
                keys:['id'], // array of form values to add to the view request
                listeners:{
                    'success': function(json, form){
                        form.clear();
                        form.populate(json);
                    },
                    'fail': function(text, error, form){
                        // do something on failure
                    }
                }
            }
        }
     });
     // load data from server and trigger the listeners above.
     v.getForm().read();

reset()

Reset value of all form Views back to it's commited value.

Source:

rollback()

Alias to reset

Source:

set(name, value)

Set value of a form element

Parameters:
Name Type Description
name String

name of form element

value String | Number | Object
Source:

submit()

Submit form to server

Source:

val(key, value)

Set OR get value of form component.
Called with two arguments(key and value), a value will be set. Called with one argument(key), value will be returned.

Parameters:
Name Type Description
key
value
Source:
Example
view.getForm().val('firstname', 'Hannah');
var firstname = view.getForm().val('firstname');

validate()

Validate form and fire "invalid" or "valid" event

Source:
Returns:

void


values() → {Object}

Returns values of all form elements in JSON format.
This method can be called on all views. It will return a JSON containing key-value pairs for all the views form elements(nested, i.e. children, grand children etc)

Source:
Returns:
Type
Object