我的模块中有两个字段: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
}
}