How to block the form from saving

Hi,
I have a requirement to show the error message and not to save the form if the list and library control does not have any record.
How to block the form saving based on fd.control("SPDataTable2").widget.dataItems().length == 0?

Thank you in advance.

Hello @annesha_adhikari,

The following code checks the entries in the 'List or Library' and not allows a user to save the record if the control has no items.

Please use it in the 'JavaScript' Editor.

image

fd.spRendered(function() {
	
	fd.validators;

	fd.validators.push({
		name: 'SPDataTable validator',
	    error: 'Error message',
	    validate: function() {
	        if (fd.control("SPDataTable0").widget.dataItems().length == 0) {
	            this.error = "Add at least one record to the table";
	            return false;
	        }

	        return true;
	    }
	});

});
1 Like

Thank you very much. It works for me.

1 Like

Hello,
I am trying to block the user to save the form if the hours is more than 30,
i am writing this code in child form of list or library control.

window.fd = fd;
fd.spRendered(function() {
var parentForm = window.top.fd;

    if (parentForm) {

        //Set field values with the values from the parent on form load
        fd.field('myLogin').value = parentForm.field('myLogin').value;
      
    }
    
});
fd.spBeforeSave(function(spForm) {
   var loginPerson=fd.field('myLogin').value;
 
pnp.sp.web.lists.getByTitle("Contracts").items.select("Salary,myLogin").filter(`myLogin eq '${loginPerson}'`).getAll().then(function(allItems){
console.log(allItems,"Allitem");
var finalArray=[];
for(var i=0; i<allItems.length; i++){
finalArray.push(allItems[i].Salary);

}

var sum = finalArray.reduce(function(a, b){
        return a + b;
    }, 0);
   
    if(fd.field('Salary').value > 30)
    {
     alert("Enter 30 or below");
**//Here i want to block user to saveing form.**
    }
    if(sum + fd.field('Salary').value>30){
    alert("You are not allowed");
**//Here i want to block user to saveing form.**
    }
    
    
});
});

Also, is there any way to show customize error box rather than windows ALERT box?
can we use like 'sweet alert' type any third party package?

Thanks in advance.

Hello @harshp924,

You can add custom validation to the form. See the example in the Manipulate field properties and appearance article.

Apart from the alert box, you can try out Confirm and Prompt boxes. Please find more information about it here.

Also, you can reference to any third party javascript libraries outside the form using jQuery $.getScript() method or RequireJS library.

Hello @mnikitina,
That completely work for me,
Here is My code,

 fd.validators.push({
    name: 'SPDataTable validator',
    error: 'Error message',
    validate: function () {
      if (fd.field('Hours').value > 20) {
        this.error = "You can not Enter hours more than 20 Per Week...!";
        return false;
      }
      if (sum + fd.field('Hours').value > 20) {
        this.error = "Not allowed to enter total hours more than 20 per week..!";
        return false;
      }

      return true;
    }
  });
1 Like