SugarCRM 6.5 CE如何使用ajax正确验证表单数据?

时间:2022-11-24 14:46:03

I need to check field phone_mobile for duplicate into database. If field value is not a duplicate then continue saving. And if such phone already exists in database, then show the alert message and stop process(form submittion).

我需要在数据库中检查字段phone_mobile是否重复。如果字段值不是重复的,那么继续保存。如果这样的电话已经存在于数据库中,那么显示警报消息并停止进程(表单提交)。

My actions:

我的行为:

In the file ./modules/Contacts/metadata/editviewdefs.php connected custom js file:

在文件中。/模块/联系人/ / editviewdefs元数据。php连接自定义js文件:

$viewdefs['Contacts']['EditView'] = array(
'templateMeta' => array(
    'includes' => array (
            array (
                'file' => 'custom/include/javascript/custom_contact.js'
            ),
        ),
    'form'=>array(
    ...

Works great.

伟大的工作。

In custom_contact.js file overload check_form(formname) function:

在custom_contact。js文件重载check_form(formname)函数:

function check_form(formname)
{
    if(formname === 'correct')
    {
       // This part does not work right for me
        var _form = document.getElementById('EditView');
        _form.action.value='Save'; 
        SUGAR.ajaxUI.submitForm(_form);
        return false;
    }
    if(formname === 'EditView')
    {
        // Ajax query works perfectly
        $.ajax({
            url : '/',
            method : 'POST',
            data : {},// some data
            success : function(data) {
                data = JSON.parse(data);
                if(!data.success)
                {
                    var text = 'The phone already exists';
                    return false;
                }
                check_form('correct');
            }
        });
    }
    return false;
}

But the if(formname === 'correct') ... block does not work correctly.

但是if(formname === 'correct')…块不能正确工作。

I need to stop the work of the form_save and include when necessary.

我需要停止form_save的工作,并在必要时包含它。

Please help to solve the problem correctly.I'm new to SugarCRM.

请帮助正确地解决这个问题。我新到SugarCRM。

2 个解决方案

#1


1  

This is something related to javsacrip/jquery error handling and you can find many logics on google as well.

这是与javabp /jquery错误处理相关的,您也可以在谷歌上找到许多逻辑。

Try following code:

尝试以下代码:

// DOM Ready 
$('input#PHONE_FIELD_ID').on('change', function () {      
    handlePhoneValidation();
    return false;
});

 var clickAttr = $("#SAVE_BUTTON_ID").attr("onclick"); 
 $("#SAVE_BUTTON_ID").attr("onclick","return handlePhoneValidation(); "+clickAttr);


function handlePhoneValidation(){

    clear_all_errors();

    var node = $('input#PHONE_FIELD_ID');
        current_val = node.val();

   /*
    * Your validation will go here
    * if condition fail then return false otherwise true 
   */

    return false;
}

#2


1  

I resolved this another way

我用另一种方法解决了这个问题

./custom/modules/Module_name/metadata/editviewdefs.php

$viewdefs ['Accounts'] = [
    'EditView' => [
        'templateMeta' => [
            'form'                => [
                'includes'            => [
                 [
                     // include custom js file
                     'file' => 'modules/Module_name/file_name.js'
                 ],
                'buttons' => [
                    // Override save button and return after click custom function
                    0 => array (
                        'customCode' => '<input type="submit" name="save" id="save" onClick="this.form.return_action.value=\'DetailView\'; this.form.action.value=\'Save\'; return check_custom_data(\'EditView\'); " value="'.$GLOBALS['app_strings']['LBL_SAVE_BUTTON_LABEL'].'">',
                    ),
                    'CANCEL',

After

modules/Module_name/file_name.js:

模块/ Module_name / file_name.js:

// Function check_custom_data() :

function check_custom_data(formname)
{
    if(formname === 'correct')
    {
        var _form = document.getElementById('EditView');
        _form.action.value='Save';
        SUGAR.ajaxUI.submitForm(_form);
        return check_form('EditView');
    }
    if(formname === 'EditView')
    {
        $.ajax({
            url : '/',
            method : 'POST',
            data : { }, // Some data
            success: function(data) {
                data = JSON.parse(data);
                if(!data.success)
                {
                    // Some code
                    return false;
                }
            }
            // If everything is ok
            check_custom_data('correct');
        }
    });
    return false;
}

This working for me.

这为我工作。

#1


1  

This is something related to javsacrip/jquery error handling and you can find many logics on google as well.

这是与javabp /jquery错误处理相关的,您也可以在谷歌上找到许多逻辑。

Try following code:

尝试以下代码:

// DOM Ready 
$('input#PHONE_FIELD_ID').on('change', function () {      
    handlePhoneValidation();
    return false;
});

 var clickAttr = $("#SAVE_BUTTON_ID").attr("onclick"); 
 $("#SAVE_BUTTON_ID").attr("onclick","return handlePhoneValidation(); "+clickAttr);


function handlePhoneValidation(){

    clear_all_errors();

    var node = $('input#PHONE_FIELD_ID');
        current_val = node.val();

   /*
    * Your validation will go here
    * if condition fail then return false otherwise true 
   */

    return false;
}

#2


1  

I resolved this another way

我用另一种方法解决了这个问题

./custom/modules/Module_name/metadata/editviewdefs.php

$viewdefs ['Accounts'] = [
    'EditView' => [
        'templateMeta' => [
            'form'                => [
                'includes'            => [
                 [
                     // include custom js file
                     'file' => 'modules/Module_name/file_name.js'
                 ],
                'buttons' => [
                    // Override save button and return after click custom function
                    0 => array (
                        'customCode' => '<input type="submit" name="save" id="save" onClick="this.form.return_action.value=\'DetailView\'; this.form.action.value=\'Save\'; return check_custom_data(\'EditView\'); " value="'.$GLOBALS['app_strings']['LBL_SAVE_BUTTON_LABEL'].'">',
                    ),
                    'CANCEL',

After

modules/Module_name/file_name.js:

模块/ Module_name / file_name.js:

// Function check_custom_data() :

function check_custom_data(formname)
{
    if(formname === 'correct')
    {
        var _form = document.getElementById('EditView');
        _form.action.value='Save';
        SUGAR.ajaxUI.submitForm(_form);
        return check_form('EditView');
    }
    if(formname === 'EditView')
    {
        $.ajax({
            url : '/',
            method : 'POST',
            data : { }, // Some data
            success: function(data) {
                data = JSON.parse(data);
                if(!data.success)
                {
                    // Some code
                    return false;
                }
            }
            // If everything is ok
            check_custom_data('correct');
        }
    });
    return false;
}

This working for me.

这为我工作。