Read-only fields based on Roles

Hi

I am trying to make some “Date & Time” and “Dropdown” fields read-only (on edit page) according to the Role of the user.

I am able to do it with “type=text” fields using the following code from this post. See the code bellow.

But I am unable to do it for “Date & Time” and “Dropdown” field types.

On custom/modules/Contacts/metadata/editviewdefs.php


'customCode' => '<input type="text" class="phone" tabindex="0" title="" value="{$fields.phone_work.value}" maxlength="100" size="30" id="phone_work" name="phone_work" {$readOnly}>',

On custom/modules/Contacts/views/view.edit.php


function display(){ // add this if function display doesn't exist  
                global $current_user;
                // check if current user is in specific role
                // code taken from thread
                $IS_AM = in_array("<ROLE NAME>", ACLRole::getUserRoleNames($current_user->id));
                if($IS_AM)
                    $this->ev->ss->assign('readOnly', 'readonly = "readonly"');
                else
                    $this->ev->ss->assign('readOnly', '');
                parent::display(); // add this if function display doesn't exist 
} // add this if function display doesn't exist

Thanks

Hi berriop,

This is more of a development query than support. I’m sure there are developers out there who can assist you with this.

Thanks,

Will.

Hello Frnd,

I am looking for read only drop down field according to roles in sugarcrm. I found your topic as you were looking for the same thing.

Could you please help me out for making the dropdown field read only?

What to add in place of … for dropdown field?

Thanks in advance and waiting for your response.

Hello everyone,

Dropdown fields not accept readonly atribute in tag. And date & time field have a javascript onclick atribute in the calendar image, so the best way is hide the field. To make dropdown field “readonly” and hide the date field, include in the display() function:

function display(){ // add this if function display doesn't exist  
                global $current_user;
                // check if current user is in specific role
                // code taken from thread
                $IS_AM = in_array("<ROLE NAME>", ACLRole::getUserRoleNames($current_user->id));
                if($IS_AM){
                    $this->ev->ss->assign('readOnly', 'readonly = "readonly"');
                    $this->ev->ss->assign('readOnlyDropDown', 'disabled= "true"');
		    $this->ev->ss->assign('HideDateTime', 'style="display:none;"');
                }else{
                    $this->ev->ss->assign('readOnly', '');
                }
                parent::display(); // add this if function display doesn't exist 
} // add this if function display doesn't exist

In the editviewdefs:

Dropdown:

'customCode' => '<select {$readOnlyDropDown} name="account_type" id="account_type" title="">
<option label="" value=""></option>
<option label="Analista" value="Analyst">Analista</option>
<option label="Cliente" value="Customer">Cliente</option>
<option label="Concorrente" value="Competitor">Concorrente</option>
</select><script>$("#account_type").val("{$fields.account_type.value}");</script>',

Date&Time:

'customCode' => '<input {$HideDateTime} class="date_input" autocomplete="off" type="text" name="date_closed" id="date_closed" value="{$fields.date_closed.value}" title="" tabindex="0" size="11" maxlength="10">',

Not forget to do the Repair and Rebuild.

I hope this help! :slight_smile:

1 Like

Not working with Assigned to: .
Drop-down menu not visible.
Could you help me?Thanks