How to use save handlers

Hello, I am trying to validate some fields in Opportunity module, I found save handlers however there are no examples how to use it with the save button.

Save Handlers :: SuiteCRM Documentation

I think this also applies to Save handlers (I am not 100% sure, though)

@scicali might be able to help you :slight_smile:

Thank you pgr for your response, I can’t fully understand what you mean.

I tried hallucinating with chat GPT and I got this response which is not working

<?php

namespace App\Extension\defaultExt\modules\Opportunities\Service\SaveHandlers;

use App\Service\RecordSaveHandlerInterface;

class OpportunityRequiredFields implements RecordSaveHandlerInterface
{
    public static function getModuleName(): string
    {
        return 'Opportunities';
    }

    public function beforeSave(array $bean, array $options = []): array
    {
        return [
            'status' => 'error',
            'data' => ['reload' => true],
            'messages' => [
                'LBL_FIELD_VALIDATION'
            ],
        ];
        
    }

    public function afterSave(array $bean, array $options = []): void
    {
        // no action after save
    }
}

?>

“run” method in RecordSaveHandlerInterface return “void”, so your object cannot return information to gui (like validation error information)

namespace App\Data\Service\Record\RecordSaveHandlers;

use App\Data\Entity\Record;
use App\FieldDefinitions\Entity\FieldDefinition;

interface RecordSaveHandlerInterface extends BaseModuleSaveHandlerInterface
{
    /**
     * Run save handler
     * @param Record|null $previousVersion
     * @param Record $inputRecord
     * @param Record|null $savedRecord
     * @param FieldDefinition $fieldDefinition
     * @return void
     */
    public function run(?Record $previousVersion, Record $inputRecord, ?Record $savedRecord,  FieldDefinition $fieldDefinition): void;

}

If you need frontend validation use Front-end Logic Examples :: SuiteCRM Documentation

If you need backend validation please upvote my suggestion Backend field validation

Time ago I made a sort of backend validation with 7.x LogicHooks Logic Hooks :: SuiteCRM Documentation using

SugarApplication::appendErrorMessage('Enter your message string');
1 Like

Do I have to use the Angular framework for the front-end validation?

No, if you use simple validation like this:

<?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'
                    ],
                ],
            ],
        ]
    ],
);

https://docs.suitecrm.com/8.x/developer/extensions/frontend/logic/field-logic/fe-extensions-required-logic/#_required_logic

Yes if you need more complex validation:

Thank you scicali for your help. I have another question, mind me please I am still beginner at using SuiteCRM! Can I embed Javascript code in the PHP code that will be called before_save in logic_hooks.php? I saw a code snippet on the internet that was using Javascript inside the PHP code. I want to confirm that it’s possible.

Short answer no

Long answer you can enable legacy view (7.x) for the module: SuiteCRM Developer Insights - Overriding Config - SuiteCRM

Even in v7 (or v8 using legacy mode), logic hooks are back-end, not front-end, so it makes no sense to use Javascript in them.

The only exception are hooks that are somehow producing HTML to inject in the front-end (like process_record hook) so that the JS somehow ends up in the front-end to be executed.

I will use Angular framework because the validation is not really that simple, I want to validate that three checkboxes are not all false (at least one is true), I have reviewed the Thread you post about the complex validation, he was dealing with a single field, how I am going to deal with three checkboxes all together at the same time.

It would be be really helpful to help if you could explain the requirement about what exactly the validation is. I think you have three checkbox fields to be used to validate a particular field input data before saving, here the logic operators would help.