Skip to content

Passing parameters

Roberto Prevato edited this page Mar 12, 2016 · 5 revisions

Every validation and formatting rule can receive custom parameters, defined in the dataentry schema. To do so, define the validation or formatting rules using objects, instead of strings. This example shows how to configure a validation rule to receive a custom parameter:

    var dataentry = new Forms.DataEntry({
      element: document.getElementById("test-form"),
      schema: {
        name: {
          validation: [{ name: "required", params: { foo: 222 } }]// the `required` validation function is called with the given option
        },
        "favored-food": {
          validation: ["required"]// the `required` validation function is called without parameters
        },

Similarly, it is possible to pass parameters to formatting rules, for example:

    var setValue = Forms.Formatting.SetValue;

    Forms.Formatting.Rules["zero-fill"] = {
      fn: function (field, value, length) {
        //NB: the length parameter comes from the dataentry schema (see below)
        if (!value) return;
        var original = value;
        while (value.length < length) {
          value = "0" + value;
        }
        if (original !== value) {
          setValue(field, value);
        }
      }
    };

    //declare the dataentry
    var dataentry = new Forms.DataEntry({
      element: document.getElementById("example-form"),
      schema: {
        zero: {
          validation: ["required"],
          format: [{ name: "zero-fill", params: 10 }]
        }
      }
    });

Clone this wiki locally