Greetings!
To Do: On my custom module "UT_Blogs" I will mark "Description" field required if status of My Blog Post is Publish.
Step 1: Check if you have a file named view.edit.php under custom/modules/<desired_module>/views folder.
Check if you have a file named view.edit.php under modules/<desired_module>/views folder.
Read the comments in code carefully and replace the variables as asked.
P.S. Here, <desired_module> means the module name you see in the url, for example, Contacts, Leads, UT_Blogs, etc.
You may find the field names and its labels in Admin > Studio > <Your_module> > Fields > <Choose correct field> > Take Field Name and System Label.
Step 3: Done! Refresh the page and start testing.
To Do: On my custom module "UT_Blogs" I will mark "Description" field required if status of My Blog Post is Publish.
Step 1: Check if you have a file named view.edit.php under custom/modules/<desired_module>/views folder.
- If you have, skip to Step 2.
- If you do not have, like me, follow..
Check if you have a file named view.edit.php under modules/<desired_module>/views folder.
- If you have, copy that file and paste under custom/modules/<desired_module>/views folder and skip to Step 2.
- If you do not have it, create a file named view.edit.php under custom/modules/<desired_module>/views folder.
<?php
require_once('include/MVC/View/views/view.edit.php');
/*
* replace UT_Blogs with the module your are dealing with
*/
class UT_BlogsViewEdit 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 blog_status_c to the field of your module
$('#blog_status_c').change(function() {
makerequired(); // onchange call function to mark the field required
});
function makerequired()
{
var status = $('#blog_status_c').val(); // get current value of the field
if(status == 'Publish'){ // check if it matches the condition: if true,
addToValidate('EditView','description','varchar',true,'{$mod_strings['LBL_DESCRIPTION']}'); // mark Description field required
$('#description_label').html('{$mod_strings['LBL_DESCRIPTION']}: <font color="red">*</font>'); // with red * sign next to label
}
else{
removeFromValidate('EditView','description'); // else remove the validtion applied
$('#description_label').html('{$mod_strings['LBL_DESCRIPTION']}: '); // 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
}
}
require_once('include/MVC/View/views/view.edit.php');
/*
* replace UT_Blogs with the module your are dealing with
*/
class UT_BlogsViewEdit 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 blog_status_c to the field of your module
$('#blog_status_c').change(function() {
makerequired(); // onchange call function to mark the field required
});
function makerequired()
{
var status = $('#blog_status_c').val(); // get current value of the field
if(status == 'Publish'){ // check if it matches the condition: if true,
addToValidate('EditView','description','varchar',true,'{$mod_strings['LBL_DESCRIPTION']}'); // mark Description field required
$('#description_label').html('{$mod_strings['LBL_DESCRIPTION']}: <font color="red">*</font>'); // with red * sign next to label
}
else{
removeFromValidate('EditView','description'); // else remove the validtion applied
$('#description_label').html('{$mod_strings['LBL_DESCRIPTION']}: '); // 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
}
}
Read the comments in code carefully and replace the variables as asked.
P.S. Here, <desired_module> means the module name you see in the url, for example, Contacts, Leads, UT_Blogs, etc.
You may find the field names and its labels in Admin > Studio > <Your_module> > Fields > <Choose correct field> > Take Field Name and System Label.
Here is a special mention about custom module: if you have a custom module, we prefer to create/change the file under modules/<my_custom_module>/views
Step 3: Done! Refresh the page and start testing.
Before | After |
Works with 6.5+ Versions as here we have used jQuery, but can easily changed to work with just Javascript for < 6.5 versions. Compatible with all SugarCRM flavors.