Possible to make custom Form Component un-editable by admins?

Kentico Dev asked on November 28, 2019 18:28

I made a simple custom form component that is only meant to be a hidden field. It can be included on any form and always be the value of the logged-in users UserName (I use HttpContext.Current.User.Identity.Name) in the GetValue function of my custom form component.

The problem is, when an admin goes into the Recorded Data tab, they can edit form data. If they do, my custom form field will be set to the administrator's username.

Is there a way around this? Can I determine in the custom component whether I am on the Recorded Data tab or something like that? Is there a way to make a field un-editable after first entry? Or is a custom form component not suitable for what I'm doing?

Correct Answer

Peter Mogilnitski answered on November 28, 2019 19:15

I guess you it should be something related to ViewMode property. Here an example how to get it the old way.

1 votesVote for this answer Unmark Correct answer

Recent Answers

Roman Hutnyk answered on November 29, 2019 17:12

Why don't you use regular text box that is hidden (with CSS) that has default value set to current user.

On admin form it should be visible and it won't be rewritten as default value populates only to blank fields. An admin will see it as well as will be able to modify it, but only intentionally.

1 votesVote for this answer Mark as a Correct answer

Kentico Dev answered on November 29, 2019 21:38

Thank you both. This code did what I needed: PortalContext.ViewMode == ViewModeEnum.LiveSite

But I also did some checking upon insert and update by hooking into the form events.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.