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)
<?php
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(
array(
'name' => 'SetRequired',
'params' => array(
'target' => 'lost_reson_c',
'label' => 'lost_reson_c',
'value' => 'equal($sales_stage, "Closed Lost")',
),
),
),
);
pgr
10 September 2023 10:22
2
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.
opened 11:17AM - 15 Nov 22 UTC
Type:Bug
Priority:Important
#### Issue
Any validation defined in vardefs does not work.
- Add an integer… field via Studio
- Parameter the field with min value 0 and max value 10
- Deploy the field in the views
- Edit the object in edit mode: Validation works, you can not enter values greater than 10
- Edit the object via inline edit: Validation does not work. You can enter a value like 99
Or if you define a custom validation in vardefs, like:
```
'size' => '33',
'validation' =>
array (
'type' => 'callback',
'callback' => 'function(formname,nameIndex){
var fieldvalue=$("#" + formname + " input[name=" + nameIndex + "]").val();
var returncode=true;
if(fieldvalue.trim()!=fieldvalue){
add_error_style(formname,nameIndex,"Space at the start or end of value not allowed " + nameIndex);
returncode= false;
}
return returncode;
}',
),
```
It won't run on inline edt. It will though work on normal edit.
#### Expected Behavior
Validations should run also on inline edit.
#### Actual Behavior
Only "system" validations like validation if it is a number seems to run, but custom ones do not.
#### Possible Fix
x
#### Steps to Reproduce
1. Add custom validation code to vardefs.php of a field
2. Try to save in inline edit with invalid value
3. Value save although it should be invalid
4. Save the same value in normal edit view, validation works, won't save
#### Context
#### Your Environment
* SuiteCRM Version used: 7.12.7, 7.12.8
* Browser name and version (e.g. Chrome Version 51.0.2704.63 (64-bit)): Chrome 107
* Environment name and version (e.g. MySQL, PHP 7): MariaDB 10.2.x, PHP 7.4
* Operating System and version (e.g Ubuntu 16.04): Debian 11
I’m trying to use this feature but I’m not getting any appreciable results.