Search code examples
jsviews

Can validate and converter be combined in jsViews data-link?


I can ensure data exists in an input element by including validate and required in jsViews' data-link attribute like this:

<input type="text" data-link="{validate activityCode required=true}">

And by following Boris Moore's example I can ensure the data returned to the model is cast as an integer instead of input's default type of string:

$.views.converters({
    toInt: function(value) {
        return parseInt(value);  // simple example, without error checking
    }
});

<input type="text" data-link="{:activityCode:toInt}">

Where I'm having a problem is combining both validation and converter. Nothing binds to the input element using this:

<input type="text" data-link="{validate activityCode:toInt required=true}">

Does anyone know of syntax that allows both validation and convertBack functionality to exist in the same data-link attribute?


Solution

  • You can use the syntax convert=... convertBack=... on any tag.

    See two-way binding - convert and convertBack and using converters with other tags. (The second docs reference is for JsRender, so concerns convert only. But if using JsViews data-linking, then convertBack=... works in just the same way.)

    Search for "convertBack=" and you'll find some examples, including this with radiogroup and this one with validate.