Make all fields readonly based on condition

I have a scenario where i need to make all edit fields readonly based on a condition, then make only one field editablefor example:

if ($this->fk_Status=='1' && !IsAdmin()){
//make all fields readonly

//then make one field editable
$this->Description->ReadOnly = FALSE;
}

I know I can set them all to readonly like this:

 $this->Issue->ReadOnly = TRUE;
    $this->fk_Priority->ReadOnly = TRUE;
    $this->Files->ReadOnly = TRUE;
    $this->fk_Building->ReadOnly = TRUE;

But for tables with a lot of fields I’m wondering if there’s a way to do it that’s less workv2023

You may use, e.g. $this->setFieldProperties(“ReadOnly”, true).

Nice solution. You can even set all the fields in current form become Disabled, too:

$this->setFieldProperties("Disabled", true);

Thank youI was struggling with getting all my fields to individually set to readonly.
I have about 12 fields and some accepted the readonly command, some didn’t even when i used

$this->setFieldProperties("ReadOnly", true);

some fields refused to play along, and I have no idea whyhowever

$this->setFieldProperties("Disabled", true);

worked perfectlyThe problem now is that using the “disabled” option means that all existing field values are lost on save.Any ideas why the readonly attribute won’t apply to some fields?

Note that the ReadOnly property adds readonly attribute to the input tag only, see HTML attribute: readonly:
Note: Only text controls can be made read-only, since for other controls (such as checkboxes and buttons) there is no useful distinction between being read-only and being disabled, so the readonly attribute does not apply.

The readonly property is failing to apply to about half of each of my input, and select tags.
I can’t see any logical reason why any would be skipped.Can setFieldProperties be used to set visibility?

philmills wrote:

The problem now is that using the “disabled” option means that all existing field values are lost on save.

You may actually assign the new value of the field by the old value in Row_Updating server event, so that it won’t be lost on save.

philmills wrote:

Can setFieldProperties be used to set visibility?

I tried

$this->setFieldProperties("Visible", false);

but it didn’t work, is the syntax wrong or is it just not possible?

$this->setFieldProperties(“Visible”, false); works, depending on where you use it, you may post your complete code (and in which server event) for discussion.

This is great!
I was able to use it successfully in Edit Page > Page_DataRenderingPreviously i tried it in Row_Rendered, but I guess it makes sense why it wouldn’t work there, as the record id must be preserved.

You may also use Page_Load server event that belongs to the Edit page, for example, the following code will hide all the fields first, after that, only show 2 fields (OrderID and ProductID):

$this->setFieldProperties("Visible", false);
$this->OrderID->Visible = true;
$this->ProductID->Visible = true;

I hit a problem using $this->setFieldProperties(“Visible”, false); in Edit Page > Page_DataRenderingAs I understand it:

  • the “Visible” property should preserve the existing field values on submit
  • the “Disabled” property entirely disables the field and existing values are not preserved

However, $this->setFieldProperties(“Visible”, false); causes the fields’ orginal values to be lost on save.Coming back to my original plan to use $this->setFieldProperties(“ReadOnly”, true);
I discovered that if i use that, all the fields are in fact set to ReadOnly as expected, BUT some select and input fields are rendered as greyed out while some are rendered as if they are editable (white box) although they aren’t actually editable.
I’m combing through my code but I can’t find anything which would cause such an anomaly, and in fact my Row_Rendered code is only applied to list and view

philmills wrote:

while some are rendered as if they are editable (white box) although they aren’t actually editable.

You may add your CSS code from HTML → Styles → User in order to change the background color of the controls which has ReadOnly attribute.

I’m trying to get to the bottom of the issue, rather than create elaborate CSS workarounds.I have inspected the rendered code. And for example I have have two adjacent fields fk_Building and fk_Location
on the edit page one has rendered html with aria-readonly=“true” - correct, and field contains the value and is greyed out
the other is rendered with aria-disabled=“false” and the readonly attribute is missing even though I’m using $this->setFieldProperties(“ReadOnly”, true);.