Adding Required Field Logic

When making these changes be sure to make them in the custom directory, ie: 'public/legacy/custom/modules/<module>/…​'

1. Introduction

Required logic allows you to define rules to mark fields as required depending on other fields values.

1.1 Example scenario

In this example, using the Accounts module, we are going to require the field Annual Revenue if the Employees field is between 10-50.

Non Required Annual Leave

Required Annual Leave

2. Logic Metadata definition

The first thing to define is the logic entry in the metadata. This is where we are going to define the triggers to mark our field as required.

The configuration for the logic can be added to the vardefs.php or the detailviewdefs.php.

In the following example we are going to add them to the Accounts module field vardefs.

2.1 Steps to add the logic on the custom vardefs

  1. Create a new field in custom extensions for Accounts module:

    1. Example: public/legacy/custom/Extension/modules/Accounts/Ext/Vardefs/annual_revenue.php

  2. Copy the 'annual_revenue' definition from public/legacy/include/SugarObjects/templates/company/vardefs.php

  3. Replace the logic entry to the annual_revenue definition with the code on the snippet below.

    • Re-set permissions if needed (will depend on your setup)

  4. Run Repair and Rebuild on Admin menu

<?php

$dictionary['Account']['fields']['annual_revenue'] = array(
    'name' => 'annual_revenue',
    'vname' => 'LBL_ANNUAL_REVENUE',
    'type' => 'varchar',
    'len' => 100,
    'comment' => 'Annual revenue for this company',
    'merge_filter' => 'enabled',
    'logic' => [
        'required' => [
            'key' => 'required',
            'modes' => ['edit', 'create'],
            'params' => [
                'fieldDependencies' => [
                    'employees'
                ],
                'activeOnFields' => [
                    'employees' => [
                        '10-50'
                    ],
                ],
            ],
        ]
    ],
);

Required Logic

Within logic for specific fields you can set multiple definitions for required. Each definition will work like an OR. For example:

    'logic' => [
        'required' => [
            'key' => 'required',
            'modes' => ['edit', 'create'],
            'params' => [
                'fieldDependencies' => [
                    'employees'
                ],
                'activeOnFields' => [
                    'employees' => [
                        '10-50'
                    ],
                ],
            ],
        ],
        // OR
        'required1' => [
            'key' => 'required',
            'modes' => ['edit', 'create'],
            'params' => [
                'fieldDependencies' => [
                    'name'
                ],
                'activeOnFields' => [
                    'name' => [
                        'Example'
                    ],
                ],
            ],
        ],
    ],

Now the Annual Revenue field will be required if the Name is example OR Employees is 10-50.

Properties description

  • Key

    • The key within the named logic array is stating which logic type will be used for the following. In this case it’s required.

  • Modes

    • Modes are views you would like your required logic to take effect on, as shown above it will be detail, edit and create. Another example of a mode that could be selected could be list for example.

Params

Field Dependencies

fieldDependencies is where you declare the field(s) that you would like your logic to depend on.

'fieldDependencies' => [
    'employees',
]

Active on Fields

activeOnFields is where you declare the field/values that trigger the change of the field to a required field.

In the example above we have the field annual_revenue set to required when Employees is 10-50.

If we wanted it to set it to required if it was either 10-50 or another value such as 50-100 then a new value would be added like so:

'activeOnFields' => [
  'employees' => ['10-50', '50-100'],
],

50-100 Employees

Multiple Fields

Within the activeonFields you can add more than one field such as:

'activeOnFields' => [
    'employees' => ['10-50', '50-100'],
    'name' => ['Example'],
],

This works like an OR. If Name is Example OR Website is either www.google.com or www.yahoo.com. When adding more fields to activeOnFields be sure to also add them to fieldDependencies

For more information on different field logic see here.

Content is available under GNU Free Documentation License 1.3 or later unless otherwise noted.