Page 1 of 1

Make form read only

Posted: 26 Apr 2018
by TWendt
Hello all,

i created an form for a Process Change Request. In the form is an dropdown field for the status. When the status is "Completed", the complete form should be read only. Please help.

Best wishes
Tom

Re: Make form read only

Posted: 26 Apr 2018
by Nikita Kurguzov
Dear Tom,
There are at least two options here:
1) Redirect users to Display Form automatically after Status changes to "Completed". This should be easy to do, but might not be perfect as it would be difficult to change Status back, if it was set to "Completed" by mistake.
2) Second option is to disable all fields if Status changes to "Completed". You can find a similar example in this article - https://spform.com/javascript-framework ... ynamically

Let me know if you need any help or have any questions!

Re: Make form read only

Posted: 26 Apr 2018
by TWendt
Hi,

many thanks for your fast answer. Do I have to enter each field individually. Because I have too many fields in my form.

Best wishes
Tom

Re: Make form read only

Posted: 26 Apr 2018
by Nikita Kurguzov
Dear Tom,
Please, try to run the following code to disable all fields:

Code: Select all

var fields = $('.fd_field');
var names = [];
for(var i = 0; i < fields.length; i++){
	console.log($(fields[i]).attr('fd_name'));
	var name = $(fields[i]).attr('fd_name');
	names.push(name);
};

for (var i = 0; i < names.length; i++){
	fd.field(names[i]).readonly(true);  
}
PS. One thing to note, you also need to turn all fields editable again prior to saving a form, otherwise the values would disappear.
So I would use the following code:

Code: Select all

var fields = $('.fd_field');
var names = [];
for(var i = 0; i < fields.length; i++){
	console.log($(fields[i]).attr('fd_name'));
	var name = $(fields[i]).attr('fd_name');
	names.push(name);
};

function disableFieldsIfCompleted() {
  if (fd.field('Status').value() == 'Completed') {
        for (var i = 0; i < names.length; i++){
	    fd.field(names[i]).readonly(true);  
        }
    } else {
        for (var i = 0; i < names.length; i++){
	    fd.field(names[i]).readonly(false);  
        }
  }
}
 
// Calling function when the user changes the status
fd.field('Status').change(disableFieldsIfCompleted);
 
// Calling function on form loading
disableFieldsIfCompleted();
 
// Enabling fields before the submission
fd.onsubmit(function () {
  for (var i = 0; i < names.length; i++){
	    fd.field(names[i]).readonly(false);  
  }
  return true;
});