Force a field to be read only
Prerequisite: You must be familiar with the Syntax used in Tutorials
- learning:
- Force a computed field to be read only
- level:
- Intermediate
- domains:
- PHP, Constrain
- methods:
- GetAttributeFlags, GetInitialStateAttributeFlags
- min version:
- 2.1.0
Goal
In this example, we will ensure that the user cannot enter an End Date on a WorkOrder object, as we have computed its value with this Tutorial: Compute WorkOrder End Date
This method can force a field
to be read-only, on the Console, CSV import and in the Portal.
But it does not work for DataSynchro and REST/JSON API.
But it does not work for DataSynchro and REST/JSON API.
Implementation
-
To do it with the Designer you must have
Designer PHP developer
profile. -
To do it with an extension, you must have already created an extension.
In both case, the main job is to write PHP code as described below. Including that piece of code in your iTop is much quicker with the Designer, than writing an extension, but that's the only difference.
Make a field read only
In this use case, we want the end_date
field
declared on WorkOrder class to be read-only.
To address this use case, we must overload 2
methods, one for the “Creation Form” and one for the
“Modify Form”. We force end_date
to be read-only at
creation and on modification.
- class:WorkOrder
-
public function GetAttributeFlags($sAttCode, &$aReasons = array(), $sTargetState = '') { // This function is invoked when the object is EDITED on the Console // It is called for each and every field of the object, // but we just want to change the behavior for a single field if ($sAttCode == 'end_date') { // Combine the new Flag with those impose by a parent class return(OPT_ATT_READONLY | parent::GetAttributeFlags($sAttCode, $aReasons, $sTargetState)); } return parent::GetAttributeFlags($sAttCode, $aReasons, $sTargetState); } public function GetInitialStateAttributeFlags($sAttCode, &$aReasons = array()) { // This function is invoked when the object is CREATED on the Console // It is called for each and every field of the object, // but we just want to change the behavior for a single field if (($sAttCode == 'end_date')) { // Combine the new Flag with those imposed by a parent class return(OPT_ATT_READONLY | parent::GetInitialStateAttributeFlags($sAttCode, $aReasons)); } // For other fields ask the parent class to do the job return parent::GetInitialStateAttributeFlags($sAttCode, $aReasons); }
Using
Using
OPT_ATT_HIDDEN
you can hide a fieldUsing
OPT_ATT_MANDATORY
you can make a
required field
3_1_0/customization/read-only-field.txt · Last modified:
2023/07/21 10:19 by 127.0.0.1