-1

I have a field-A (drop-down) and a field-B (textfield), depending on the value of the field-A the field-B becomes optional or not, for example:

If the value of the field-A is 1; then the field-B becomes required, for this I use the JS function that SuiteCRM brings: addToValidate

But if the value of the field-A is 0 I should convert the field-B to optional

What JS function of SuiteCRM can I use for this last case?

Thank you

Berroterán
  • 131
  • 2
  • 12
  • Would you be interested in a straight Javascript answer or do you need to call the "addToValidate" method for other reasons? It seems you should be able to do this just by adding/removing/toggling classes using plain old Javascript. – TBowman Mar 01 '18 at 20:53
  • First try to do it by adding or removing via Jquery the attribute "required", but at the time of saving the form, it does not recognize that attribute and let the empty field pass (when it should be required), in what other way do you suggest do it using JS ?? – Berroterán Mar 01 '18 at 20:59
  • Do you absolutely need to use addToValidate? There are other ways to accomplish conditional required fields, but they aren't via JS. – Reisclef Mar 02 '18 at 19:40
  • Possible duplicate of [Remove Required Field from QuickCreate in Sugarcrm](https://stackoverflow.com/questions/15332765/remove-required-field-from-quickcreate-in-sugarcrm) – Liam Aug 08 '18 at 15:30

1 Answers1

1

If you want to change whether a field is required dynamically, look into Dependency Actions.

They allow you to automatically switch the required-state (true/false) of a given target field based on the value of a custom Sugar Logic formula.

Based on the linked example and your example it would look something like this:

./custom/Extension/modules/<module>/Ext/Dependencies/fieldB_required.php

<?php
    $dependencies['<module>']['fieldB_required'] = array(
        'hooks' => array("edit"),
        //Trigger formula for the dependency. Defaults to 'true'.
        'trigger' => 'true',
        'triggerFields' => array('fieldA'),
        'onload' => true,
        //Actions is a list of actions to fire when the trigger is true
        'actions' => array(
            array(
                'name' => 'SetRequired', //Action type
                //The parameters passed in depend on the action type
                'params' => array(
                    'target' => 'fieldB',
                    'label'  => '<field label>', //normally <field>_label
                    'value' => 'equal($fieldA, 1)', //Formula
                ),
            ),
        ),
    );

fieldA is the field that, when changed, triggers a re-evaluation of the formula and will set the required state of target field fieldB to the value of equal($fieldA, 1).

Notes:

  • While the Dependency Action is defined in a PHP file, the formula you provide is usually executed on the client/javascript-side of things, therefore saving you the need to write custom javascript code.
  • This is a SugarCRM feature but it's quite old, so SuiteCRM hopefully still supports it.
Jay
  • 3,640
  • 12
  • 17