I had a requirement to add custom form validation to a couple of custom fields in the meetings module. The requirement was as follows:

When a Meeting is marked as ‘Held’ the Objective Result and Follow Up fields will be compulsory.

A quick google search revealed that as usual there are many ways to this in sugar so I opted for the one which seemed to be keeping within the sugar architecture the most. This was a post in the sugarcrm forums by eggsurplus which could be quickly adapted to suit my requirement.

I Added the following to the JavaScript section in custom/modules/Meetings/metadata/editviewdefs.php:

Now when editing a meeting in sugar and the ‘Status’ drop down is set to ‘held’ the custom fields I added ‘Objective Result’ and ‘Follow Up’ are automatically added to the required fields.

This code can be easily adapted for use with any standard module and field in sugar.

Thanks eggsurplus!!
Edit:
One cavieat with this is that the validation wont work until the held status is changed therefore we need to add another snippet of JavaScript to view. This will ensure that when editing records that already have their status set to held the validation will still work. This snippet also has to be included after the rest of the page has been loaded.

First we add a new view:

custom/modules/Calls/views/view.edit.php

And then add the snippet after the rest of the page has loaded by calling the parent display function