How to make a field read only

Hi - I am new SuiteCRM OnDemand user. I have used aPaas platforms for years and am glad to be exploring SuiteCRM.

In my previous aPaas platform, LongJump (aka AgileApps), it is possible to make a form field read only.

In SuiteCRM, I have a situation where I have a calculated field (Workflow) on the Account EditView (linked to DetailView). I would like the field in EditView to not be editable since a user may be confused that they CAN edit the calculated field text but when they save, they will find that their change was not saved.

Since I have DetailView linked to EditView, I cant simply not list the calculated field on the EditView.

What are my options?

Thanks!

Strangely enough, I have the need to do the exact same thing! I found this article about how to do it, but haven’t tried yet. If you get to it before me, I’d love to hear how you made out!

https://suitecrm.com/suitecrm/blog/entry/make-an-edit-view-field-read-only-based-on-user-role-in-suitecrm-or-sugarcrm-ce

Link not working.

How to make field read-only for the users? :thinking:

@rsp first question, in SuiteCRM 8 or SuiteCRM 7 it’s different?

SuiteCRM 7.x

I have created new custom field under aos_products module, but I don’t know how to set it read-only field. The user should not be allowed to update value.

You can make it so users can’t edit certain fields by going to : /custom/modules//metadata/editviewdefs.php In this file, add ‘type’⇒’readonly’, to the definitions of the file you want to make readonly.

array (
            'name' => 'testdropdown_c',
              'studio' => 'visible',
            'label' => 'LBL_TESTDROPDOWN',
          ),

Would become:

array (
            'name' => 'testdropdown_c',
              'type'=>'readonly',
              'studio' => 'visible',
            'label' => 'LBL_TESTDROPDOWN',
          ),

Afterwards, run a Quick Repair & Rebuild and clear your Browser’s cache to view changes.

you have to turn off inline edit or they’ll be able to edit inline

The above doesn’t format checkbox fields and the like. It only works for text fields. If you have a field like checkboxes or radio that require formatting, you can do this as follows:

0 =>
           array (
             'name' => 'field_name',
             'label' => 'LBL_FIELD_NAME',
            ' displayParams' => array(
                 'readonly' => true,
             )
           ),

This allows the field to be formatted properly. Make sure to turn off inline edit!!!

1 Like

@rsp, sorry just re-read, the code above will make it read only for everyone. For just admin you have to jump through some hoops. It’s different depending on the type of field you have to use customCode to format the output if the user is not admin. It’s slightly differnent for each type of field. What type of field do you want to make read only except admins?

Here is an example of a text field, this will not work for dropdowns or checkboxes.

First:

copy modules/Contacts/views/view.edit.php to custom/modules/Contacts/views/view.edit.php

You’re going to need to pass two variables to the Smart Template: 1) isAdministrator 2) readOnly

class AccountsViewEdit extends ViewEdit
{
public function __construct()
{
parent::__construct();
$this->useForSubpanel = true;
$this->useModuleQuickCreateTemplate = true;
}
public function display()
{
global $current_user;

    // Check if the user is an administrator
    $isAdministrator = $current_user->is_admin;

    // Set the $readOnly variable based on the user's administrator status
    $readOnly = $isAdministrator ? '' : 'readonly = "readonly"';

    // Assign $readOnly to Smarty template
    $this->ev->ss->assign('readOnly', $readOnly);
  
  //Assign $isAdministrator to Smarty template
   $this->ev->ss->assign('isAdministrator', $isAdministrator);

    parent::display(); // Call the parent display function to render the view
}

}

then in the editviewdefs

1 => 
          array (
            'name' => 'price_level_date_c',
	'customCode' => '{if $isAdministrator}@@FIELD@@{else}<input type="text" class="date_input" tabindex="0" title="" value="{$fields.price_level_date_c.value}" maxlength="99" size="30" id="price_level_date_c" name="price_level_date_c" {$readOnly}>
				{/if}',
            'label' => 'LBL_PRICE_LEVEL_DATE',			
          ),
        ),
1 Like

Thank you for providing code.

My fields are integer.

For example, the values will be like -3, -1, 0, 23, 4

No, you’re correct. I don’t want anyone to update those number fields even admins. :face_with_peeking_eye:

Then I think this will probably work for you in my first example.

'type'=>'readonly',

1 Like

Thank you so much :heavy_heart_exclamation:

That’s correct!

Now, those integer fields are read-only. :partying_face: :crossed_fingers:

1 Like