-->

Adding custom jQuery validation in SugarCRM editvi

2019-06-13 20:04发布

问题:

I am trying to add custom jQuery script in either header or footer of Accounts edit view, searched everywhere in Google but can't find an hook that prints scripts in header or footer.

My goal is to add custom validation for my custom fields, but as soon as I add jQuery code the page gets halted.

I am inserting following code in custom/modules/Accounts/metadata/editviewdefs.php

$viewdefs ['Accounts'] = array(
    'EditView' => array(
        'templateMeta' => array(
            'javascript' => '<script type="text/javascript">
            $(document).ready(function(){
                alert("This is my custom javascript code");
            });
            </script>',

Above code produces below screen.

I remove the jQuery part like this

$viewdefs ['Accounts'] = array(
    'EditView' => array(
        'templateMeta' => array(
            'javascript' => '<script type="text/javascript">

                alert("This is my custom javascript code");

            </script>',

I see following alert

I see that the simple JavaScript code is working fine but problem occurs only when I add jQuery code, I tried adding jQuery library before above code but that did not solve the problem.

Can someone tell what I am missing here?

回答1:

You also need to wrap the js with {literal} tags:

$viewdefs ['Accounts'] = array(
'EditView' => array(
    'templateMeta' => array(
        'javascript' => '<script type="text/javascript">
        {literal}
        $(document).ready(function(){
            alert("This is my custom javascript code");
        });
        {/literal}
        </script>',