On save do validate the checkbox is checked/unchecked

Hi,

On save I need to validate the checkbox is checked/unchecked

Which function I should use for on save validation

addToValidate('EditView','woking_in','bool',true, <?php echo "'".$mod_strings['LBL_BUILDING_AED']."'"; ?>);

Any help will be highly appreciable.

Regards

If you want make sure it is checked before saving, you should make the field mandatory from Studio. That doesn’t require code.

@AlxGr, I need conditional required

Check this sample then: https://rumitmittal7.medium.com/hiding-field-in-sugarcrm-conditionally-2376f2ea7717

Hi
We have considered the Account Module to validate the checkbox is checked/unchecked while saving the record in the Module.

Scenario: If Account Module having the Type = “Analyst” then Checkbox Field must be checked. If Checkbox Field is unchecked then show the Validation Message in the alert box and disallow to save the record.

Below are the screenshots. In which User Select the Type = “Analyst” and Checkbox Field is unchecked. So it shows the alert message.

To achieve this needs to do the following customization in your CRM.

  1. Register Logichook by creating a Logichook Registration file on the below path and add code as shown in the below screenshot.

File Path : custom/Extension/application/Ext/LogicHooks/ValidateCheckBoxValueLogicHook.php

  1. Create a Logichook file on the path below and add code as shown in the below screenshot.

File Path :

custom/include/ValidateCheckBoxValue/ValidateCheckBoxValueLogicHook.php

Note : You can replace the Module Name in the Condition. Currently we have considered Account Module that’s why added Account Module in the Condition.

  1. Create a js file on the below path & add code as shown in the below screenshot.

File Path : custom/include/ValidateCheckBoxValue/js/ValidateCheckBoxValueEditView.js

Note : In this file you just need to replace your dropdown field id with “#account_type”, Account Type Condition like Currently we have accountType = “Analyst” and checkbox field id with “#checboxfield_c”.

I hope it will help you
Thanks
Urvi

@Urvi, thank you so much for help much impressive.
if you can send code as text or upload some where it will be more help full

1 Like

It would be easier with a condition like:

	addToValidateCallback(
        'EditView', // Form Name
        'YOUR_FIELD_NAME', // field name
        'bool', // Field type
        false, // Is required
        "ERROR MESSAGE", // Message to be displayed if not OK
        function() { // Callback
        	// return true if the condition is OK else return false
        });

@blqt. already tried and its was not worked

Did you? Your post shows addToValidate and not addToValidateCallback.

@blqt, yes I tried with addToValidate but not worked I applied another logic and now issue resolved

Good thing you were able to solve the issue. It will be good to share the solutions so the community can benefit from it too.

Hi, I followed the post and used custom js code that’s it