When we are creating a new form, we've set up our editor form to have one of the fields be assigned a specific default value and we are looking to have that "locked" so that it cannot be changed by the user.
To achieve that "lock" we thought to make use of the MakeReadOnly() on the field. Here's what the code for the field looks like,
When we've had users submitting the form with this setup, we've come to discover that the value does come across and we're seeing 'null' attempting to be saved to the TeamName field.
I've seen the call back to the API and I do see that the TeamName and its default value are being sent back to the endpoint, but that doesn't seem to be coming through in the backend upon the insert.
Are we using MakeReadOnly() properly in this instance for the creation of a new entity? Is there a recommended approach for "locking" a value for an editor when creating a new record?
Yea a read only field wouldn't allow you to set it's value as by it's nature, it is read only. One way around it could be to not bother making it read only but instead set it's data type to label. This way it's not phsically editable in the UI, but it would be writable in code.
Sorry for the delayed response. We followed your suggestion and altered the AddField method to remove the MakeReadOnly() and added SetDataType(). The updated line looked like the following,
After some testing with this, we experienced the same issue of a 'null' value attempting to be saved to the TeamName field.
We continued to run with your thought and decided to create a custom data type property editor. After creating it, we were able to use the SetDataType() to this new data type and we were able to get everything working great from there!
When I finally had a moment to do a little more investigating, I found it interesting that, when using the SetDataType() method to set it as a label data type, the angularjs form output appeared to be identical to the angularjs output when using the MakeReadOnly() method. I'm figuring that's why we experienced the issue again when we went the path of assigning the data type as a label.
Not that anyone asked, but if it's not possible to support Adam's original code, then could Konstrukt provide a property editor that supports default set values? Or maybe loading the initial values of the model when in the editor on Create.
So Konstrukt does support having writable properties on create, and read only properties on edit. The MakeReadOnly method should accepts a delegate that is passed a context and in that context you are told if it's create or edit so you can return whether the field should be readonly based on that.
Useful to know, but I don't ever want the field to be editable. I want to set a value for a property on creation programmatically, and displayed this property as readonly both on create and update.
One could reasonably argue that that isn't a UI consideration, but AFAIK Konstrukt doesn't provides an OOTB solution to setting initial values for readonly properties.
From my experiments it seems that an entity isn't initialised on Create.
My value isn't pulled through but I've also put breakpoints on the ctor of my model and doesn't get hit on initial display of the Create editor.
If that is a unexpected then could it be that my custom repo is setup wrong? That also doesn't appear to be called on initial display of the Create editor.
A field with MakeReadOnly when creating an entity
When we are creating a new form, we've set up our editor form to have one of the fields be assigned a specific default value and we are looking to have that "locked" so that it cannot be changed by the user.
To achieve that "lock" we thought to make use of the MakeReadOnly() on the field. Here's what the code for the field looks like,
When we've had users submitting the form with this setup, we've come to discover that the value does come across and we're seeing 'null' attempting to be saved to the TeamName field.
I've seen the call back to the API and I do see that the TeamName and its default value are being sent back to the endpoint, but that doesn't seem to be coming through in the backend upon the insert.
Are we using MakeReadOnly() properly in this instance for the creation of a new entity? Is there a recommended approach for "locking" a value for an editor when creating a new record?
Hey Adam,
Yea a read only field wouldn't allow you to set it's value as by it's nature, it is read only. One way around it could be to not bother making it read only but instead set it's data type to
label
. This way it's not phsically editable in the UI, but it would be writable in code.Matt --
Sorry for the delayed response. We followed your suggestion and altered the
AddField
method to remove theMakeReadOnly()
and addedSetDataType()
. The updated line looked like the following,After some testing with this, we experienced the same issue of a 'null' value attempting to be saved to the TeamName field.
We continued to run with your thought and decided to create a custom data type property editor. After creating it, we were able to use the
SetDataType()
to this new data type and we were able to get everything working great from there!When I finally had a moment to do a little more investigating, I found it interesting that, when using the
SetDataType()
method to set it as alabel
data type, the angularjs form output appeared to be identical to the angularjs output when using theMakeReadOnly()
method. I'm figuring that's why we experienced the issue again when we went the path of assigning the data type as alabel
.Thank you for your help!
Very interesting!
Thanks for sharing your finding and really pleased you found a solution.
Matt
Thought I would add my solution too, that avoids a custom property editor.
My use case is creating API Keys, and the actual API keys I want to be programmatically created on Create, and be readonly.
I achieved this with two steps. Firstly creating the property and informing the editor to ignore the blank value:
And then subsequently setting the value on save using events
Of course you need to register the event handler too
Not that anyone asked, but if it's not possible to support Adam's original code, then could Konstrukt provide a property editor that supports default set values? Or maybe loading the initial values of the model when in the editor on Create.
Hey,
So Konstrukt does support having writable properties on create, and read only properties on edit. The
MakeReadOnly
method should accepts a delegate that is passed a context and in that context you are told if it's create or edit so you can return whether the field should be readonly based on that.Hope that helps
Useful to know, but I don't ever want the field to be editable. I want to set a value for a property on creation programmatically, and displayed this property as readonly both on create and update.
One could reasonably argue that that isn't a UI consideration, but AFAIK Konstrukt doesn't provides an OOTB solution to setting initial values for readonly properties.
But you are in charge of the model, so can't you make it part of your model for it to have a default value?
From my experiments it seems that an entity isn't initialised on Create. My value isn't pulled through but I've also put breakpoints on the ctor of my model and doesn't get hit on initial display of the Create editor.
If that is a unexpected then could it be that my custom repo is setup wrong? That also doesn't appear to be called on initial display of the Create editor.
Ahh, sorry, no you are probably right. I might be confusing Vendr with Konstrukt 😁
Ok, I can see what you mean. Maybe I need to do something with
MakeReadOnly
that still allowsSetDefaultValue
to be called with it 🤔is working on a reply...