2015-10-14 84 views
1

我在我的模塊中有兩個字段,名爲:rules_name_c這是一個文本字段,rules_author_c是一個相關字段。有條件的必填字段

這些字段不是必填字段,但是當我將數據輸入rules_name_c字段時,我想使其填寫rules_author_c以完成記錄。

我曾嘗試以下:

<?php 
$dependencies['conn_connection']['required_author'] = array(
    'hooks' => array("edit"), 
    'trigger' => 'true', //Optional, the trigger for the dependency. Defaults to 'true'. 
    'triggerFields' => array('rules_name_c'), 
    'onload' => true, 
    //Actions is a list of actions to fire when the trigger is true 
    'actions' => array(
     array(
      'name' => 'SetRequired', 
      //The parameters passed in will depend on the action type set in 'name' 
      'params' => array(
       'target' => 'rules_author_c', 
       'label' => 'rules_author_c_label', 
       'value' => 'not(equal($rules_name_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 
    } 

} 
+0

使用JavaScript將此字段設置爲必填您無法使用php來完成此操作。 – ErasmoOliveira

+0

我嘗試使用我剛添加的代碼,這也不起作用 – BillyMichael

回答

1

我寫了這個javascript函數,後來我用jQuery使用它:

function lxValidateCRMfield(form_name, field_name, label, validate, fnCallerName = "") { 
    fnCallerName = (fnCallerName != "") ? "(Function " + fnCallerName + ")" : ""; 
    if (validate) { 
     console.log("lxValidateCRMfield adding validation on form " + form_name + " to field " + field_name, fnCallerName); 
     //addToValidate is defined somewhere on suitecrm 
     addToValidate(form_name, field_name, 'varchar', true, "Falta campo requerido: " + label); 
     $('#' + field_name + '_label').html(label + ': <font color="red">*</font>'); 
    } else { 
     console.log("lxValidateCRMfield removing validation on form " + form_name + " to field " + field_name, fnCallerName); 
     //removeFromValidate is defined somewhere on suitecrm 
     removeFromValidate(form_name, field_name); 
     $('#' + field_name + '_label').html(label + ': '); 
    } 
} 

然後調用它的形式,你需要(使用你的領域可能看起來是這樣):

// List all forms available 
console.log(document.forms); 
// select one you need 
var form_name = 'EditView'; eg: EditView, DetailView, search_form 
var module = 'YourModule'; // eg: Opportunities 

var crmEditView = document.forms[form_name]; 
if (crmEditView.module.value == module) { 
    if ($('#rules_name_c').val() != '') { 
     lxValidateCRMfield(form_name, 'rules_author_c', 'Author', true, 'optionalNameOfFunctionYourCallingThis'); 
    } else { 
     lxValidateCRMfield(form_name, 'rules_author_c', 'Author', false, 'optionalNameOfFunctionYourCallingThis'); 
    } 
} 

我希望它能幫助
Regards