Search code examples
javascriptajaxsuitecrm

SuiteCRM Custom Duplicate Validation won`t save or update


Good Day!

I created a custom validation on my SuiteCRM were it will validate the First Name, Last Name and Mobile number if it was already existing. But my problem is after the validation works the data won`t save or update.

Check my view.edit.php codes below:

$javascript = <<<'EOT'
    <script type="text/javascript" language="JavaScript">
    function customJavascriptDuplicateValidation(thisview)
    {
        var firstname = document.getElementById('first_name').value;
        var lastname = document.getElementById('last_name').value;
        var mobile = document.getElementById('mobile_number').value;
        var birthday = document.getElementById('birthday').value;
        var email = document.getElementById('email_c').value;
        var location = document.getElementById('location_code').value;
        var consent = document.getElementById('consent_timestamp_date').value;
        var salutation = document.getElementById('salutation').value;
        
        $.post('index.php?entryPoint=checkDuplicateCustomers', { first_name: firstname, last_name: lastname, mobile_number: mobile }, function(data)
        {
            if (data == 'exists') {
                $('#error_firstname_msg').html('<strong style="color:red;"> &#x2717; Already Used</strong>');
                $('#error_lastname_msg').html('<strong style="color:red;"> &#x2717; Already Used</strong>');
                $('#error_mobile_msg').html('<strong style="color:red;"> &#x2717; Already Used</strong>');
                return check_form('EditView');
            }else if (data == 'unique'){
                $('#error_firstname_msg').hidden;
                $('#error_lastname_msg').hidden;
                $('#error_mobile_msg').hidden;
                if(firstname != '' && lastname != '' && mobile != '' && birthday != '' && email != '' && location != '' && consent != '' && salutation != ''){
                    return check_form('EditView');
                }else{
                    return check_form('EditView');
                }
            }else{
                $('#error_firstname_msg').hidden;
                $('#error_lastname_msg').hidden;
                $('#error_mobile_msg').hidden;
                return check_form('EditView');
            }
        });
        
    }
    </script>
    EOT;

And I modify the save button at editviewdefs.php

$viewdefs['Accounts']['EditView']['templateMeta']['form']['buttons'][0] = array(
'customCode' => '<input title="Save" accesskey="a" class="button primary" onclick="var _form = document.getElementById(\'EditView\'); _form.action.value=\'Save\'; if(customJavascriptDuplicateValidation(\'EditView\'))SUGAR.ajaxUI.submitForm(_form);return false;" type="submit" name="button" value="Save" id="SAVE">',);

The Native Valition works when you click SAVE. The Native Valition Works when you click save

The custom validation works also, if you click SAVE: enter image description here

enter image description here

After filling up the required field and inputting unique customers information. The data will not save.

My conclusion on this was on my custom javascript codes. Because if I put the codes below outside of the $.POST function the data will save, but the problem the custom duplicate validation will not work if all field in the condition is not null/empty.

if(firstname != '' && lastname != '' && mobile != '' && birthday != '' && email != '' && location != '' && consent != '' && salutation != ''){
                     SUGAR.ajaxUI.showLoadingPanel();
                    return check_form('EditView');
                }

Solution

  • Please disregard this one, I was able to fixed this by just adding this code below:

                    SUGAR.ajaxUI.showLoadingPanel();
                    var _form = document.getElementById('EditView');
                        _form.action.value='Save';
                    SUGAR.ajaxUI.submitForm(_form);
                    return check_form('EditView');