:: Version 2.7.0 ::

Force a field to be read only

Prerequisite: You must be familiar with the Syntax used in Tutorials and have already created an extension.

learning:
Force a computed field to be read only
level:
Intermediate
domains:
PHP, Constrain
min version:
2.1.0

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.

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);
}
2_7_0/customization/read-only-field.txt · Last modified: 2020/04/15 15:23 (external edit)
Back to top
Contact us