Tutorial: Form messages

Form messages

Form messages are used within the client to trigger and handle specific events on forms. You can listen to messages and publish them.

Listeners should pass any errors through the error callback. Any validation error should specifically use the mendix/lib/ValidationError class. E.g, when the commit message is sent, you want to call storeSomeData, which handles errors through a callback passing some object indicating the error:

form.listen("commit", function(callback, error) {
    storeSomeData(callback, function(e) {
        if (e.type === "validation") {
            if (error) error(new ValidationError(e.message)).
        } else {
            if (error) error(new Error(e.message));
        }
    });
});

Publishers can then distinguish between validation and other errors like this:

form.publish("commit", function() {
    console.log("Form validated fine!");
}, function(err) {
    if (e instanceof ValidationError) {
        console.log("Validation failed");
    } else {
        console.log("Some other error occured");
    }
});

Form messages include validate, submit, commit and rollback, which are described in the following.

validate

Signal listeners that they should validate.

submit

Signal listeners that they should submit, e.g. this can be used to upload a file to the runtime. This event is published before committing or calling a microflow with a button.

commit

Signal listeners that they should commit, e.g. commit an object in the runtime.

rollback

Signal listeners that they should rollback, e.g. rollback an object in the runtime.