Field-level real-time custom validation in a table

  • 1
  • Question
  • Updated 4 years ago
  • Answered
I have a table for which I need some custom validation when fields are edited. I want to create something like:

Action:
When a specific field on a row in model is updated, run a snippet

Snippet:
If field X (which is the one that was updated) is not empty and field Y (in the same row) is not empty, revert the value of field X back to its original value and show a message (similar to an issue from awhile back would be great), or even remove the value from field Y.


I may be able to figure this out with some of the existing examples, but I can't determine what arguments are passed into the snippet in this case. How do I refer to the record/context of the current row that contains the field that was changed? I can see that "arguments[0]" in my snippet contains an object, but what object is it? Every attribute I try to access (index, value, id, row, etc.) of that object is undefined.
Photo of Peter Bender

Peter Bender, Champion

  • 6,246 Points 5k badge 2x thumb

Posted 4 years ago

  • 1
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
The area of documentation that's relevant when using Snippets with Model Actions is the skuid.events API --- if you're using a Model Action with Initiating Event = "Row in Model updated", then the Event that you are listening to is the "row.updated" event, so search for this in the skuid.events API doc page and you can find the list of the parameters that will be passed to your snippet, but I'll include them here just to help clarify what you could do:

modelId ---> the Id of the Model that the update occurred on
row
--> the row/record that was updated
updates --> an object containing the fields that were updated, and the new values for those fields
Photo of Peter Bender

Peter Bender, Champion

  • 6,246 Points 5k badge 2x thumb
Thanks for the pointer, Zach. I'm happy to report that I've gotten this to work well. I have two fields in my table which are mutually exclusive, so the snippet blanks one out whenever you edit the other. This was a bit tricky, because the action of blanking out the field (which actually is updating it to a null value) causes the snippet to run a second time. Thus I had to make it specific enough so that action is only taken when both fields are not blank, so that the script triggered when the blanked out field is updated doesn't result in any action taken. I also had to blank out a couple other fields in one case. Those fields are formulas which would blank out once a save occurs anyway, but you ideally want to see that happen instantly. I could have done that purely with Skuid's native actions, I suppose, but it was cleaner to just do it all in one place. For other's reference, here's the javascript snippet that is called when either one of the fields is changed:
var params = arguments[0],
    $ = skuid.$,
    enrModel = skuid.model.getModel(params.modelId),
    enrRow = params.row;

if(params.updates.Beginning_RL_PK__c !== undefined && params.updates.Beginning_RL_PK__c !== null && enrRow.Beginning_Session_Reading_Level_Scale__c !== null
    ){
        enrModel.updateRow(
            enrRow,
            {
                Beginning_Session_Reading_Level_Scale__c: null, 
                Baseline_Sight_Word_Knowledge__c: null,
                Beginning_QSI__c: null
            }
        );
} else 
if(params.updates.Beginning_Session_Reading_Level_Scale__c !== undefined && params.updates.Beginning_Session_Reading_Level_Scale__c !== null && enrRow.Beginning_RL_PK__c !== null
    ){
        enrModel.updateRow(enrRow,'Beginning_RL_PK__c',null);
}