Actions button bypasses required fields

  • 1
  • Question
  • Updated 4 years ago
I have a Button with multiple actions and when the page has no changes detected the button still saves and continues through all the actions even though the first action is Save Model(s) with the Roll Back entire save on any error checked. The next actions need to not run, which completes the section flag.

I am trying to have a complete button that validates all required fields entered, then updates a completed flag on the model.

Ideas on making this behave correctly or rendering a button only if all required fields are entered?
Photo of Jarrod Hinson

Jarrod Hinson

  • 2,310 Points 2k badge 2x thumb

Posted 4 years ago

  • 1
Photo of Jarrod Hinson

Jarrod Hinson

  • 2,310 Points 2k badge 2x thumb
I have also tried something like this to validate the page with JS snippet in an Action but I am not getting any messages back?

var $ = skuid.$;var pageTitle = $('#ROSPTitle');
var editor = pageTitle.data('object').editor;
var messages = [];
var models = [
    skuid.model.getModel("Case")
];
$.each(models,function(i,model){
   $.each(model.registeredLists,function(j,list){
       var listMessages = list.validateRequiredFields();
       if(listMessages && listMessages.length) {
           $.each(listMessages,function(){
              messages.push(this); 
           });
       }
   });
});
// If we have warning messages, do NOT proceed
if (messages.length) {
    // Have our step's editor handle the messages
    editor.handleMessages(messages);
    //stepEditor.handleMessages(messages);
    return false;
}
(Edited)
Photo of Menachem Shanowitz

Menachem Shanowitz

  • 1,074 Points 1k badge 2x thumb
A simple resolution to this may be to run the Javascript snippet below before the Save action.

It seems like that the issue here is that it doesn't even attempt a save if there are no changes and just continues with the rest of the actions, so all you need to do is add a snippet that validates that there are changes and the rest of the save validation you can leave up to Skuid.

var model = skuid.$M("YourModel");
//If no changes were made
if(skuid.$.isEmptyObject(model.changes))
{
//you can do an action here, or you can add an on error action to the snippet //don't continue and save
return false;
}
(Edited)
Photo of Jarrod Hinson

Jarrod Hinson

  • 2,310 Points 2k badge 2x thumb
I think this is close. If they save their changes and then come back later to complete the page then this would block them since no changes would have been made. I need a combo of this and a way to check if the current page has any uncompleted required fields.
Photo of Menachem Shanowitz

Menachem Shanowitz

  • 1,074 Points 1k badge 2x thumb
If that's the case I would go with your original code, but because validateRequiredFields() only validates fields that have changed I would mimic the skuid error.

Replace
 var listMessages = list.validateRequiredFields();
       if(listMessages && listMessages.length) {
           $.each(listMessages,function(){
              messages.push(this); 
           });
       }

with this 

$.each(list.requiredFields, function(k, requiredField) {    if (!list.model.data[0].hasOwnProperty(requiredField.id) || list.model.data[0][requiredField.id] === null || list.model.data[0][requiredField.id] === '')       messages.push(mimicSkuidError(requiredField.label));
});
and add this function

function mimicSkuidError(fieldLabel){
return {
      fields : [fieldLabel],
      message : "Required Fields have no Value [" +  fieldLabel + "]",
      severity : "ERROR",
      status : "REQUIRED_FIELD_MISSING"
    }
}
(Edited)
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,704 Points 20k badge 2x thumb
How about this then. 

1. Update the field Completed to True
2. Save the Model
  b) On Error action to update the field Completed to False

This is assuming that you have no other actions that you'd like to not have run after the Save Model action.
This is also assuming the button is meant to save changes to the model.

What is the intended purpose of the button. Video would be helpful to get the context of what you are trying to achieve.