Custom error message on before save validation

Hello,

I am using before_save hook and running some data validation. How do I customize standard message ā€œError occurred while saving recordā€ when I throw an exception due to data being invalid?

Tried SugarApplication::appendErrorMessage(ā€˜ā€¦ā€™) but it didn’t help.

Thanks.

Maybe you need to edit it here:


  1. ā€˜LBL_ERROR_SAVING’ => ā€˜Error occurred while saving record’,

I need to throw custom error messages based up on validation so it will change depending on user input.

In that you need to check in your module code which line calling on save() method and which file is it accessing?

Sorry I don’t fully understand. I am just using

$hook_array['before_save'][] = [
    1,
    'Custom Logic for Field Modification',
    'custom/modules/CustomLogic.php',
    'CustomLogic',
    'validateResult',
];

then inside CustomLogic.php I have code that can conditionally throw error in exception or any other format that can trigger custom message display:

class CustomLogic
{
    public function validateResult($bean, $event, $arguments)
    {
       ....
       throw Exception('CUSTOM MESSAGE TO DISPLAY IN UI');

    }
}

If i am not wrong. You need to register your custom label in the language.

custom/modules/module/Language/en_us.lang.php

LBL_MY_CUSTOM_ERROR = ā€˜CUSTOM MESSAGE TO DISPLAY IN UI’;

Then do quick repair & rebuild from the admin dashboard and use that variable name in your logic hook.

Also, you may need to include that en_us.lang.php file location in your code.

Could you also advise on how to use LBL_MY_CUSTOM_ERROR in the code once it is defined? Is it passed to exception or need to be set in some global variable?

I never did it but may like below:

class CustomLogic
{
    public function validateResult($bean, $event, $arguments)
    {
        $errorMessage = translate('LBL_MY_CUSTOM_ERROR', 'YourModule');
        
        throw new Exception($errorMessage);
    }
}

Doesn’t work unfortunately. This is also no different than just doing:

throw new Exception('Custom Message');

So, is it not displaying the error? Do you want to display custom message on the UI?

It displays the standard error message not the custom one.

In that case, you need to find out code flow. Check how it is doing it.

Then you can add your custom label in same file and change the calling method.

I don’t think the general approach is the best one…

The validations are front-end, they shouldn’t be happening in back-end hooks which run detached from the front-end.

In v7 you would do this specifying a validation in the metadata, see validation example here, in the ā€œHow to Test Thisā€ section.

In v8 I am not so sure but it would surely be a field-logic entry.

I need to be able to restrict Account editing based up on role or security group. Is it possible to do it on the front end? I thought it is not that’s why I was doing backend validation.

I’m not sure if that is currently possible.

@Harshad can you help?

Sure @pgr, I will try to help. We have a role based ACL which can help to restrict editing and saving based on custom roles. We can create a custom role and set restrictions for the required modules from the admin panel. If this is not feasible, then we can achieve the required functionality through defining custom validators and registering in validation manager by writing Angular Typescript code OR implementing a Process Handler in PHP code. But that depends upon the type of validation. If the validation is related to field like integer, phone, required , readonly etc. We can have a common generic validator. Or it is business validation, the process handler would help. @amsl can you please help understand your validation requirement ? is it a field or business rule that checks a set of fields before saving the record? Also please specify which version you are using. Version 8.8 has some way to extend the save action.

Thank you very much for your response. We are using 8.7 so maybe updating to 8.8 is the first step.

As far as business logic it’s pretty simple. We need to prevent some set of users from modifying certain fields for Accounts. For example only users that belong to security group A (lets call them admins) can modify custom drop down and check box fields. All fields should be visible to everyone. Doing it via roles or security group is fine for us but we need to being able to prevent saving or make field read only if user does not have permission do perform edit.

Custom error message on save and aborting the operation is fine as well as a solution for us if there is no better one.

This doesn’t look impossible, We can get there by adding a new logic type for ā€˜readonly’ and use the key in fields definitions logics to restrict. There may be some better solutions. For now I think we can display the fields in detail page and set the display off in edit mode using ā€˜displayTypeBackend’ where the process handler will take care of returning value ā€˜show’ or ā€˜none’ depending upon the current user role or the required business logic. I am working on the ā€˜readonly’ logic type in extensions and will share after completing the code. Thanks

Thank you for you response!

Hey @amsl have u ever made this to work ?

Got the same basic requirement, just match a regex or a custom validation on a specific field, throw a custom error message and roll back to old value if the validation is not meet.

old V7 hook work, only the custom error message cant be display for some reason. also :

$bean->custom_field== $bean->fetched_row[ā€˜custom_field’];

Dont look to work anymore aswell.

This could help but without a proper example its hard to make it to work.
Im trying to find out how the default one for mail & phone number is set up to replicate it