Monday, 25 February 2013

Mark a field Required conditionally


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.
Step 2: Write following code in that file.


 * replace UT_Blogs with the module your are dealing with

class UT_BlogsViewEdit extends ViewEdit {

    public function __construct() {
        $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
                       // 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
                                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
        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.


    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.


  1. Thanks for such social platform which give us variety of idea to explore ourself technically .This exposure give benefits to everyone to fit or to survive in global market which is very essential in the global era.
    Time Attendance System

  2. This code is not working for me ....I have replaced fields according to my module field ...what can be the issue ?

    1. Sorry for delay in answering your question. If you are still struggling with the same issue, can you email(blogs(at)urdhva-tech(dot)com ) us the file and let us know the field name?


Urdhva Tech Pvt. Ltd. Powered by Blogger.

Urdhva Tech

My photo
SugarCRM Consultants Contact us at Skype : urdhvatech

Search This Blog

Stay connected Socially


Total Pageviews

Popular Posts