Model-Driven-Apps: How to use setIsValid method

When dealing with client script customization, most of the time we are dealing with data validation where the common steps are like the below snippet:

var blog = blog || {};

(function() {
  this.formOnLoad = function(executionContext){
    var formContext = executionContext.getFormContext();
    formContext.getAttribute("tmy_transactiondate").addOnChange(validateOnChangeTransactionDate);
    formContext.getAttribute("tmy_dateonly").addOnChange(validateOnChangeDateOnly);
  }

  var validateOnChangeDateOnly = function(executionContext){
    var formContext = executionContext.getFormContext();
    var date = formContext.getAttribute("tmy_dateonly").getValue();
    var valid = date > new Date();
    if(valid) return;

    formContext.ui.setFormNotification("Date only must be bigger than today's date", "ERROR", "tmy_dateonly");
    formContext.getAttribute("tmy_dateonly").setValue(null);
  };

  var validateOnChangeTransactionDate = function(executionContext){
    var formContext = executionContext.getFormContext();
    var transactionDate = formContext.getAttribute("tmy_transactiondate").getValue();
    var valid = transactionDate <= new Date();
    if(valid) return;

    formContext.ui.setFormNotification("Transaction date cannot be in the future", "ERROR", "tmy_transactiondate");
    formContext.getAttribute("tmy_transactiondate").setValue(null);
  };
}).apply(blog);

And here is the result in the form let’s say I fill wrong data in both attributes:

Form notifications shown and the attributes that are not valid will be null

There is an alternative method that we can use to show the error (without nullifying the attribute) and will be executed on the formOnSave event. With the setIsValid method, we can change the above code to the below code:

var blog = blog || {};

(function() {
  this.formOnLoad = function(executionContext){
    var formContext = executionContext.getFormContext();
    formContext.getAttribute("tmy_transactiondate").addOnChange(validateOnChangeTransactionDate);
    formContext.getAttribute("tmy_dateonly").addOnChange(validateOnChangeDateOnly);
  }

  var validateOnChangeDateOnly = function(executionContext){
    var formContext = executionContext.getFormContext();
    var date = formContext.getAttribute("tmy_dateonly").getValue();
    var valid = date > new Date();
    formContext.getAttribute("tmy_dateonly").setIsValid(valid, "Date only must be bigger than today's date");
  };

  var validateOnChangeTransactionDate = function(executionContext){
    var formContext = executionContext.getFormContext();
    var transactionDate = formContext.getAttribute("tmy_transactiondate").getValue();
    var valid = transactionDate <= new Date();
    formContext.getAttribute("tmy_transactiondate").setIsValid(valid, "Transaction date cannot be in the future");
  };
}).apply(blog);

Here is the demo for the above code:

Demo result

Happy CRM-ing! 😎

Advertisement

3 thoughts on “Model-Driven-Apps: How to use setIsValid method

  1. I’m assuming this could simplify your code base?
    // REPLACE THIS:
    if(valid) {
    formContext.getAttribute(“tmy_dateonly”).setIsValid(true);
    return;
    }
    formContext.getAttribute(“tmy_dateonly”).setIsValid(false, “Date only must be bigger than today’s date”);

    // WITH THIS?:
    formContext.getAttribute(“tmy_dateonly”).setIsValid(valid, “Date only must be bigger than today’s date”);

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.