25
February

3 Comments | 0 Shares | Urdhva Tech

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.

  • 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.

<?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
    }

}

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.
Download attachments:
Comments
  • Posted by Anum| Tuesday, 29 August 2017

    This is so helpful. (Y)

  • Posted by Mustafa Taheri| Friday, 25 August 2017

    Thanx for an awesm post this helped me a lot. But just facing an small issue its not working in subpanels nor in Quick create. Will you please help me..?? The code is exactly as above only module name and fields are changed.

  • Posted by Gaurav Aggarwal| Friday, 16 June 2017

    Thankyou so much for this lovely post. However i used it differently i used addToValidate() function in my js file and just because of your post i was able to do it just with 2 lines. Thanks again.
    Also, I have to ask one thing by using this function i am able to set required to particular field on run time but when i save the form and re-edit it the field is filled but not required as page is refreshed. So, is there any workaround for it so the field should remain required after i save the form.

Post your comment