Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Adam Werner 21 posts 202 karma points MVP c-trib
    Feb 15, 2023 @ 20:53
    Adam Werner
    0

    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,

    .AddField(p => p.TeamName).SetDefaultValue(defaultTeamName).MakeReadOnly().SetLabel("Team Name");
    

    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?

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Feb 16, 2023 @ 08:53
    Matt Brailsford
    0

    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.

  • Adam Werner 21 posts 202 karma points MVP c-trib
    Feb 25, 2023 @ 04:28
    Adam Werner
    100

    Matt --

    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,

    .AddField(p => p.TeamName).SetDefaultValue(defaultTeamName).SetDataType("Label (string)").SetLabel("Team Name");
    

    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.

    Thank you for your help!

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Feb 27, 2023 @ 09:01
    Matt Brailsford
    0

    Very interesting!

    Thanks for sharing your finding and really pleased you found a solution.

    Matt

  • David Peck 687 posts 1863 karma points c-trib
    Mar 16, 2023 @ 11:53
    David Peck
    0

    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:

    fieldsetConfig.AddField(a => a.Key).MakeReadOnly().SetDefaultValue("(Created on save)")
    

    And then subsequently setting the value on save using events

    public class ApiKeyModelSavingNotification : INotificationHandler<KonstruktEntitySavingNotification>
    {
        public void Handle (KonstruktEntitySavingNotification notification)
        {
            if (notification.Entity.After is ApiKeyModel entity && entity.Id == default)
            {
                entity.Key = Guid.NewGuid().ToString("N");
            }
        }
    }
    

    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.

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Mar 16, 2023 @ 13:42
    Matt Brailsford
    0

    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

  • David Peck 687 posts 1863 karma points c-trib
    Mar 16, 2023 @ 15:11
    David Peck
    0

    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.

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Mar 16, 2023 @ 15:18
    Matt Brailsford
    0

    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?

    public class MyEntity
    {
        public string Key { get; } = Guid.NewGuid().ToString();
    }
    
  • David Peck 687 posts 1863 karma points c-trib
    Mar 16, 2023 @ 15:32
    David Peck
    0

    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.

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Mar 16, 2023 @ 15:44
    Matt Brailsford
    0

    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 allows SetDefaultValue to be called with it 🤔

Please Sign in or register to post replies

Write your reply to:

Draft