Make Field required based on another field

I’m trying to make a field in the opportunities required if the sales_stage is equal to “Closed Lost”.

There are surprisingly few posts on this topic. There are a couple that insert JS to change the class of the field, which seems kind of bulky if you have more than a few fields.

I see there is a Sugar feature which seems to go way back for $dependencies.

Does anyone know if this is still part of SuiteCRM? This seems like a really elegant way to handle this kind of issue and many others.

I’m trying this but it doesn’t seem to be working and I don’t know if it’s because I made a mistake or if this feature is no longer in SuiteCRM.

(I know there’s a spelling mistake in lost_reson_c)

if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
// custom/Extension/modules/Opportunities/Ext/Dependencies/custom_dependency.php

$dependencies['Opportunities']['CustomDependencyName'] = array(
    'hooks' => array("edit"),
    'trigger' => 'true',
    'triggerFields' => array('sales_stage'),
    'onload' => true,
    'actions' => array(
            'name' => 'SetRequired',
            'params' => array(
                'target' => 'lost_reson_c',
                'label' => 'lost_reson_c',
                'value' => 'equal($sales_stage, "Closed Lost")',

I didn’t know about this. Interesting.

In v8 there’s the new “field logic”.

1 Like

Alas, I’m thinking this was a Sugar Pro feature and not available in CE, thus not available in 7.

You can use a a custom javascript function for validation. In that you can read the value of the other field.

Example for vardefs:

      'validation' => 
      array (
        'type' => 'callback',
        'callback' => 'function(formname,nameIndex){
                            var str=$("#" + formname + " #" + nameIndex).val();  // this is this field
                            if(str.trim()==="" && ($("#" + formname + " #theotherfield").val()).trim()=="closed_lost" ){
                                add_error_style(formname,nameIndex,"Field must be filled when theotherfield is closed lost!"); 
                                return false;
                            return true;
1 Like

Nice that looks clean and easy. Going to try.

Oh, sorry I forgot to mention the big drawback on the callback method.
It will not work on Inline edit as there is a bug that no validations work in Inline edit, that probably never gets fixed.