We have done the code for <desired_module> module. Change it as per your requirement. We will mark “Description” field required if status is Publish
Steps are as below,
1. Add a reference to the JavaScript file which will be needed for event binding.
Path: custom/modules/<desired_module>/metadata/editviewdefs.php
<?php
$viewdefs['<desired_module>']['EditView']['templateMeta']['includes'] =
array (
array (
'file' => 'custom/modules/<desired_module>/js/editview.js',
),
);
?>
2. Add the JavaScript file you want to include into the location you referenced above(custom/modules/<desired_module>/js/editview.js).
3. Write following code in editview.js file.
// Change status to the field of your module
$('#status').change(function() {
makerequired(); // onchange call function to mark the field required
});
function makerequired()
{
var status = $('#status').val(); // get current value of the field
if(status == 'Publish'){ // check if it matches the condition: if true,
addToValidate('EditView','description','varchar',true,'Description'); // mark Description field required
$('#description_label').html('Description: <font color="red">*</font>'); // with red * sign next to label
}
else{
removeFromValidate('EditView','description'); // else remove the validtion applied
$('#description_label').html('Description: '); // and give the normal label back
}
}
makerequired(); //Call at onload while editing a Published blog record
4. Quick Repair, then hard refresh your browser. All Done !
5. Done! Refresh the page and start testing.
Note:
1. Read the comments in code carefully and replace the variables as asked.
2. Here, <desired_module> means the module name you see in the URL, for example, Contacts, Leads, Accounts, etc.
3. You may find the field names and its labels in Admin > Studio > <Your_module> > Fields > <Choose correct field> > Take Field Name and System Label.
Hope you find this blog post helpful.
Feel free to add comments and queries, that helps us to improve the quality of posts.
You can contact us at [email protected]
Thank you.