Make Field Conditionally Read Only Based on Another Field

Just found out how to do something cool with Smarty templates and customcode.

There are a lot of posts about how to make one field conditionally read only based on another. Most of the solutions involve JS. I just found a much simpler way to do it and I don’t think I’ve seen it before, so I thought I’d share.

(SuiteCRM 7)

So let’s say we want to make the Opportunity Amount read only if the sales stage is Closed Won.

All you have to do is add one line to your editiviewdefs like this:

array (
          0 => 
          array (
            'name' => 'amount',
			'type' => 'currency',
			'customCode' => '{if $fields.sales_stage.value eq "Closed Won"}<span>{$fields.amount.value|number_format:2:".":","|regex_replace:"/^/":"$ "}</span>{else}<input type="text" name="amount" value="{$fields.amount.value}" id="amount" name="amount"  />{/if}',

That’s it. So we are checking if the sales stage has the value of closed won, if so we just output the value as a <span> and do some formatting to make it look nice with a currency symbol and commas. If not, then we output the regular <input> field.

You don’t even need to use the “readonly” property!

You do have to disable in-line editing for the field as the user would still be able to double click to edit from the detailview and listview if you don’t.

1 Like

can you please tell me where exactly I can disable inline edit for detailview and listview ,

Would appreciate if any screenshots attached for reference

In studio, in the field definitions, just uncheck “inline edit”