Field value (any type) gets lost with readonly()

Discussions about Forms Designer for SharePoint 2013 / 2016 and Office 365.
Locked
urbanal
Posts: 4
Joined: Tue Nov 07, 2017

07 Nov 2017

I have a strange problem with using the readonly function. In my specific case i need to autofill and disable a field under certain circumstances. a lot of code has already been built into this form and to make sure that my issue isn't related to my code i made a new simple testform and could reproduce the issue.

It seems that if I'm using the readonly function on any field, that the current value of the field gets lost when saving. i've tried several field types and i tried it on the New- and Edit-Forms. Of course my code example here makes no sense at all, but it demonstrates exactly whats the issue.

Here is my code example:

Code: Select all

fd.field('Title').value('Hello World!'); // set the value automatically
fd.field('Title').readonly(true); // make the field readonly

fd.onsubmit(function () {
	var value = fd.field('Title').value();
	console.log(value); // This logs "Hello World!" as expected.
	return true;
});
i also tried to disable the field with jquery alone - but it was exactly the same behaviour!

thx for help

User avatar
Nikita Kurguzov
Posts: 889
Joined: Mon Jul 03, 2017

07 Nov 2017

Hello, urbanal!
Yes, unfortunately that's what happens when form is submitted with disabled fields, SharePoint is unable to read values of these fields. Solution is pretty straightforward, set readonly to false in onsubmit block, so the values are properly read.

This code should work just fine:

Code: Select all

fd.field('Title').value('Hello World!'); // set the value automatically
fd.field('Title').readonly(true); // make the field readonly

fd.onsubmit(function () {
	fd.field('Title').readonly(false);
	return true;
});
Cheers

urbanal
Posts: 4
Joined: Tue Nov 07, 2017

07 Nov 2017

Hello Nikita,

thx for your reply. as far as i can tell this is not an sharepoint issue.

for example in the browser console on a SP-standard-form:

Code: Select all

document.getElementById('FieldID').value = 'Hello World!';
document.getElementById('FieldID').disabled = true;
the value of the field gets saved perfectly fine here.

if the value of field gets lost with the readonly function in the designer (also the already saved values in Edit) - this function makes no sense at all.

your workarround is working of course, but to me thats not a beautiful solution because the fields get enabled on the client for as long as it takes the form to save.

best regards

User avatar
Nikita Kurguzov
Posts: 889
Joined: Mon Jul 03, 2017

07 Nov 2017

Unfortunately, that is the browser restriction – it doesn’t pass values of disabled fields to the server.
Of course, SharePoint default fields can work fine, but they work in a different manner. But in some situations you might encounter this issue even on default SharePoint forms.

Usually, there is no time for these fields to get modified even if the form takes some time to save. If you worry that users might modify the fields, you can hide them right before setting readonly to false. Hidden fields still get saved properly.

Code: Select all

fd.onsubmit(function () {
	fd.field("Title").control()._el().hide();
	fd.field('Title').readonly(false);
	return true;
});
Cheers

urbanal
Posts: 4
Joined: Tue Nov 07, 2017

07 Nov 2017

i have no idea how the spforms designer works, but i guess there is some sort of validation happening somewhere. and this validation has a problem with disabled fields.

i have to correct myself in one case though: already saved values in the edit form don't get deleted.

however your solutions do the thing, but to me they are workarrounds for an actual issue that should be solved instead.

User avatar
Nikita Kurguzov
Posts: 889
Joined: Mon Jul 03, 2017

07 Nov 2017

There is no validation going on. In SharePoint the process of saving items sometimes happens on the client, sometimes on the server. On our forms, the process is always happening on the server. That means that the browser needs to read values from fields and send them to the server. This cannot happen if the fields are disabled, so they need to be enabled manually just prior to saving.

For example, if you try to use your code on a calendar form, it also wouldn't work because calendar always saves items on the server, even on default form.
Cheers

User avatar
Nikita Kurguzov
Posts: 889
Joined: Mon Jul 03, 2017

07 Nov 2017

As an alternative, you can use this code to set fields readonly, but not disable them. This will prevent users from changing the value, but will still save the item correctly. Though the field wouldn't appear disabled, users won't be able to change field value:

Code: Select all

fd.field("Title").control()._el().find('input').attr('readonly', true);
Cheers

urbanal
Posts: 4
Joined: Tue Nov 07, 2017

07 Nov 2017

you are right. strange that this issue has never occurred to me before.

this works - and i can live with the fact that the field appears to be editable.

thx

Locked
  • Information
  • Who is online

    Users browsing this forum: No registered users and 7 guests