Make a quick vote 🗳 ️How do you refer to SuiteCRM extensions: add-ons or plug-ins? Click here to have your say 👈

Validation AJAX-jQuery

Im trying to validate a input with JQUERY and AJAX… The input I am validating “disappears”…
In some modules it works, in others the field disappears.

1- create an entry point that points to a PHP file that performs the validation in the database and returns one value or another if the field exists or not.

The module doesn’t have an edit view created, so in my CUSTOM folder create the path:
/custom/MODULO/views/view.edit.php :

<?php

if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

require_once('include/MVC/View/views/view.edit.php');

class CustomEmpresasViewEdit extends ViewEdit

{

    public function __construct()

    {

        parent::__construct();        

    }

    /**

     * @see SugarView::display()

     *

     * We are overridding the display method to manipulate the sectionPanels.

     * If portal is not enabled then don't show the Portal Information panel.

     */

    public function display()

    {    

        $this->useForSubpanel = true;

       

        // habilitamos el valor del nombre en el archivo tpl (enviar y recibir archivos por medio de HTTP)

        $this->ss->assign('cuit_cuil_c', $this->bean->cuit_cuil_c);

        // cargamos el contenido del tpl en la variable        

        $name_input_code = $this->ss->fetch('custom/modules/empre_Empresas_vp/tpls/cuitUnico.tpl.js');

        // enviamos el contenido a "editviewdefs"        

        $this->ss->assign('validacionCuit', $name_input_code);

            parent::display();

                    

        }

       

    }

?>

.tpl.js file : /custom/MODULO/tpls/…tpl.js

<input type="text" name="cuit_cuil_c" id="cuit_cuil_c" maxlength="11" value="{$cuit_cuil_c}"/>

   <span id="cuit_unico_resultado"></span>

{literal}

<script src='/include' type="text/javascript">

    $(document).ready(function() {$('#cuit_cuil_c').blur(function () {

        $(".module-title-text").prop("id", "tipoVista");

        let vista = document.getElementById('tipoVista').innerHTML.trim();

        $('#cuit_unico_resultado').html('<strong> Verificando cuit...</strong>');

        $.post('RutaDelEntryPoint', { cuit_cuil_c: $('#cuit_cuil_c').val() }, function (data) {

            var cuitString = document.getElementById('cuit_cuil_c').value;

            var digitos = cuitString.length;

            const regex = /^[0-9]*$/;

            const soloNumeros = regex.test(cuitString);

            //Si existe la información o tiene menos de 11 dígitos, agregamos la propiedad Disabled al botón de guardar y le damos un valor true.

            //Agregamos comentarios para que el usuario sepa el motivo del error

            if (!soloNumeros) {

                $(".button.primary").prop("disabled", "true");

                $('#cuit_unico_resultado').html('<strong style="color:red;"> &#x2717 <br>Ingrese únicamente dígitos numéricos</strong>');

            }

            else if (digitos < 11) {

                $(".button.primary").prop("disabled", "true");

                $('#cuit_unico_resultado').html('<strong style="color:red;"> &#x2717 <br>Debe tener al menos 11 dígitos</strong>');

            }

            else if (data == 'existe' && vista == 'Crear') {

                $(".button.primary").prop("disabled", "true");

                $('#cuit_unico_resultado').html('<strong style="color:red;"> &#x2717 <br>CUIT Duplicado o inválido</strong>');

            }

            else if (data == 'existe' && vista != 'Crear' || data == 'unico') {

                $(".button.primary").prop("disabled", "");

                $('#cuit_unico_resultado').html('<strong style="color:green;"> &#x2713 </strong>');

            }

            else { }

        });

    })});

</script>

{/literal}

and finally, in custom/metadata/editviewdefs.php :
'customCode' => '{$validacionCuit}',