Required-Validation for Flex-Related not Working

Hallo Community,

we added Flex-Related Field on Opportunity-Modul.
We want to set this Field as, Required.
But when we do so, Opportunity can not be Saved, even if Relation ist set.

It looks like the value in the parent_name field is not recognized.
As a workaround, we tried to store the validation in the vardef file and set the field as not required.
Unfortunately the same effect.

<?php
// created: 2024-12-04 14:50:4
// 4
//

$dictionary['Opportunity']['fields']['parent_name']['logic']['required']['key'] = 'required';
$dictionary['Opportunity']['fields']['parent_name']['logic']['required']['modes'] = array('edit', 'create');
$dictionary['Opportunity']['fields']['parent_name']['logic']['required']['params'] = array (
                'fieldDependencies' => [
                    'parent_name'
                ],
                'activeOnFields' => [
                    'parent_name' => [['operator' => 'is-empty' ]],
                ],
);

 ?>

After saving with empty parent_name field and edit again, the validation works.

Ideas about that?

Can you please check the required logic where parent_type and parent_id fields are checked for empty.
The following code works fine in my suite application.

file:
public\legacy\custom\modules\Opportunities\metadata\detailviewdefs.php

          1 => 
          array (
            'name' => 'parent_name',
            'studio' => 'visible',
            'label' => 'LBL_FLEX_RELATE',
			'logic' => [
				'required-logic' => [
					'key' => 'required',
					'modes' => ['edit','create'],
					'params' => [
						'fieldDependencies' => ['parent_type','parent_id'],
						'activeOnFields' => [
							'parent_id' => [['operator'=>'is-empty']],
							'parent_type' => [['operator'=>'is-empty']],
						],
					],
				],
			],
          ),

File: public\legacy\custom\Extension\modules\Opportunities\Ext\Vardefs_override_sugarfield_parent.php (created)

<?php
 $dictionary['Opportunity']['fields']['parent_type'] =
            array(
                'name' => 'parent_type',
                'vname' => 'LBL_PARENT_TYPE',
                'type' => 'parent_type',
                'dbType' => 'varchar',
                'group' => 'parent_name',
                'options' => 'parent_type_display',

                'len' => '255',
                'comment' => 'The Sugar object to which the opportunity is related',
                'options' => 'parent_type_display',
            );

         $dictionary['Opportunity']['fields']['parent_name'] =
            array(
                'name' => 'parent_name',
                'parent_type' => 'record_type_display',
                'type_name' => 'parent_type',
                'id_name' => 'parent_id',
                'vname' => 'LBL_LIST_RELATED_TO',
                'type' => 'parent',
                'group' => 'parent_name',
                'source' => 'non-db',
                'options' => 'parent_type_display',
            );

         $dictionary['Opportunity']['fields']['parent_id'] =
            array(
                'name' => 'parent_id',
                'type' => 'id',
                'group' => 'parent_name',
                'reportable' => false,
                'vname' => 'LBL_PARENT_ID',
            );
 ?>

php bin/console cache:clear

1 Like

Hey Harshad,

thanks for Reply.

I had used Studio fĂĽr adding the Field.
If i do so and add your definitions, same behavior.

If I remove field from Studio an add your definitions, same behavior.

In Detail:
By creating an new Opportunity an don’t touch die parent_type field, i can save without adding any relation.
By editing this Opportunity after saving the parenty_type field is required but after adding a relation i can not save anyway.
By creating a Opportunity and touching the parent_type field, same behavior like on edit.

Which Version you are using?
I’am on 8.6.2.

Thanks!

Version 8.7.1

The same issue I faced when I had the changes only in vardefs file. Later I added the required validation logic in detailviewdefs.php, it worked.
Maybe it is the version, it is not working as expected.

Can you please try using OR logical operator which I think will check if at least one is empty and not allow to save when you don’t set a relation entirely. (parent_id & parent_type)

Do you have the logic on both, vardef and viewdef, file?
Or just on viewdef?

Do you mean something like this?

                1 =>
          array (
            'name' => 'parent_name',
            'studio' => 'visible',
            'label' => 'LBL_FLEX_RELATE',
            'logic' => [
                        'required-logic' => [
                                'key' => 'required',
                                'modes' => ['edit','create'],
                                'params' => [
                                        'fieldDependencies' => ['parent_id'],
                                        'activeOnFields' => [
                                                'parent_id' => [['operator'=>'is-empty']],
                                        ],
                                ],
                        ],
                        'required-logic2' => [
                                'key' => 'required',
                                'modes' => ['edit','create'],
                                'params' => [
                                    'fieldDependencies' => ['parent_type'],
                                    'activeOnFields' => [
                                           'parent_type' => [['operator'=>'is-empty']],
                                            ],
                                    ],
                        ],
                ],
          ),

Does not work, can save without entering any parent_name.

Sorry about the confusion. I have required logic only in detailviewdefs.php file. For the above issue, can you please update the required logic in detailviewdefs.php as follows.

          1 => 
          array (
            'name' => 'parent_name',
            'studio' => 'visible',
            'label' => 'LBL_FLEX_RELATE',
			'logic' => [
				'required-logic' => [
					'key' => 'required',
					'modes' => ['create','edit'],
					'params' => [
						'fieldDependencies' => ['parent_type','parent_id'],
						'activeOnFields' => [
							'parent_id' => [['operator'=>'is-empty']],
							'parent_type' => [['operator'=>'is-empty']],
						],
					],
				],
				'required-parent-name' => [
					'key' => 'required',
					'modes' => ['edit','create'],
					'params' => [
						'fieldDependencies' => ['parent_name'],
						'activeOnFields' => [
							'parent_name' => [['operator'=>'is-empty']],
						],
					],
				],
			],
          ),

Hey thanks for your Reply again,

it seems that i finaly found a working setup:


                1 =>
          array (
            'name' => 'parent_name',
            'studio' => 'visible',
            'label' => 'LBL_FLEX_RELATE',
            'logic' => [
                        'required-logic' => [
                                'key' => 'required',
                                'modes' => ['edit','create'],
                                'params' => [
                                        'fieldDependencies' => ['parent_name','parent_type'],
                                        'activeOnFields' => [
                                                'parent_name' => [['operator'=>'is-empty']],
//                                              'parent_type' => [['operator'=>'not-empty']],  <-This is optional
                                        ],
                                ],
                        ],
                ],
          ),
        ),

the “parent_type” at fieldDependencies seems to be the important part. Without that you can save empty “parent_name” on first save.

1 Like