2015-10-21 3 views
2

У меня есть два поля в моем модуле: rules_name_c, который является текстовым полем и rules_author_c, который является полем связи.Условное поле с использованием javascript - SuiteCRM 7.3.1

Эти поля не являются обязательными для заполнения, однако, когда я ввожу данные в поле rules_name_c, я хотел бы сделать так, чтобы rules_author_c был заполнен для завершения записи.

Я попытался следующим:

<?php 

require_once('include/MVC/View/views/view.edit.php'); 
/* 
* replace UT_Blogs with the module your are dealing with 
*/ 

class conn_connectionViewEdit extends ViewEdit { 

    public function __construct() { 
     parent::ViewEdit(); 
     $this->useForSubpanel = true; // this variable specifies that these changes should work for subpanel 
     $this->useModuleQuickCreateTemplate = true; // quick create template too 
    } 

    function display() { 

     global $mod_strings; 
     //JS to make field mendatory 
     $jsscript = <<<EOQ 
        <script> 
         // Change rules_author_c to the field of your module 
         $('#rules_name_c').change(function() { 
          makerequired(); // onchange call function to mark the field required 
         }); 
        function makerequired() 
        { 
         var status = $('#rules_name_c').val(); // get current value of the field 
         if(status != ''){ // check if it matches the condition: if true, 
           addToValidate('EditView','rules_author_c','varchar',true,'{$mod_strings['LBL_RULES_AUTHOR']}'); // mark rules_author_c field required 
           $('#description_label').html('{$mod_strings['LBL_RULES_AUTHOR']}: <font color="red">*</font>'); // with red * sign next to label 
          } 
          else{ 
           removeFromValidate('EditView','rules_author_c');      // else remove the validtion applied 
           $('#rules_author_c_label').html('{$mod_strings['LBL_RULES_AUTHOR']}: '); // and give the normal label back 
          } 
        } 
        makerequired(); //Call at onload while editing a Published blog record 
       </script> 
EOQ; 
     parent::display(); 
     echo $jsscript;  //echo the script 
    } 

} 

ответ

2

Вы бы лучше создать вызов функцию вместо этого, а затем добавить свои собственные проверки. Измените свой JavaScript на что-то вроде этого.

addToValidateCallback(
    'EditView', // Form Name 
    'rules_author_c', // Field Name 
    'relate', // Field type 
    true, // Is required 
    "Please do what I need.", // Message 
    function() { // Callback 
      var is_valid = true; 
      // Do custom validation 
      if($('#rules_name_c').val() != '' && $('#rules_author_c').val() == '') { 
      is_valid = false; 
      } 
     return is_valid; 
    }); 
    }); 
Смежные вопросы