SugarCRM: Making Fields Required But Not Hidden

sugarcrmdevelopers —  January 27, 2012

Welcome to the first Jon’s Tips and Tricks for SugarCRM.  In this post I’m going to cover how make fields required when other fields equal something specific.  Now I know what you are going to say, “This is available in studio” and while it is, it will hide the field on the edit view when you don’t have the values selected/set in the specific control fields.

This is done though a vardef (variable definition) extension in Sugar and it’s an upgrade safe customization.

Here we go:

1. Start by making sure the file path exists: custom/Extension/modules/<modulename>/Ext/Dependencies/

2. Create a new file (descriptive file name is best so you know what it does just by looking at the file name).  Mine is called primary_address_postalcode_required.php

3. Here is my code:

SugarLogic Method Information

Let me break apart the value param and explain it what each one does:

  • and() – a simple way to tie multiple command together, this can have 2 or more sub commands in it.
  • equals($field, “value”) – a quick and dirty way to say that $field is equal to “value”
  • isInList($field, $list) – a way to say that $field’s value is in a list.
    • for the list you can do two things
      1. createList($value, $value1, …) which will create a list on the fly but to update the values if need you need to do a code change
      2. getDropdownKeySet($list_name) will return the keys from a drop down list.  What is allows you to do is create a new list in the drop down editor and it allows anyone who has access, to add or remove values without having to make code changes.
For more information check out the developer docs

4. Once that is done you put it on your server and run a repair and rebuild.

5. Go about your day have better data quality due to making fields require when other specific values are set.

Cheers!