2017-11-21 3 views
1

Ich muss Feld phone_mobile für Duplikat in Datenbank überprüfen. Wenn der Feldwert kein Duplikat ist, fahren Sie mit dem Speichern fort. Und wenn ein solches Telefon bereits in der Datenbank vorhanden ist, dann zeigen Sie die Warnmeldung an und stoppen Sie den Vorgang (Formularübermittlung).SugarCRM 6.5 CE, wie man Formulardaten mit ajax richtig validiert?

Meine Aktionen:

In der Datei ./modules/Contacts/metadata/editviewdefs.php verbunden benutzerdefinierten js-Datei:

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

Arbeiten groß.

In custom_contact.js Datei Überlastung check_form(formname) Funktion:

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; 
} 

Aber der if(formname === 'correct') ... Block nicht richtig funktionieren.

Ich muss die Arbeit der form_save stoppen und bei Bedarf einschließen.

Bitte helfen Sie, das Problem richtig zu lösen. Ich bin neu in SugarCRM.

+0

@Adriano Ich muss die Telefonnummern in der Datenbank überprüfen. –

+0

warum rufen Sie einige JS-Funktion auf den Fokus aus? oder bei Wertänderung? Wenn die Validierung fehlschlägt, deaktivieren Sie die Schaltfläche Speichern. – Star

+0

@Star Bitte geben Sie ein Beispiel, wie ich das machen kann? –

Antwort

1

Dies ist etwas mit javsacrip/jquery Fehlerbehandlung verbunden und Sie können viele Logiken auf Google finden.

Versuchen folgenden 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; 
} 
+0

Ich habe das anders gelöst. –

+0

können Sie Ihre Lösung teilen. Es wäre hilfreich für andere Entwickler. – Star

+0

https://Stackoverflow.com/a/47470741/6120970 Aber trotzdem danke –

1

ich beschlossen ein anderer auf diese Weise

./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', 

Nach

modules/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; 
} 

Diese Arbeit für mich.

Verwandte Themen