Remove Validation from Required Field Conditionally

SuiteCRM Version 7.10.11

Via Custom JS in Opportunities Module, when sales stage is Prospecting (default) i have added following code in order to make Expected Close Date as NOT Mandatory.



$(document).ready(function(){
$('#sales_stage').change(function(){
  if($(this).val() == "Prospecting") {
     removeFromValidate('EditView','date_closed');
  }
  else {
     addToValidate('EditView', 'date_closed', 'date', true,'Expected Close Date' );
  }
}
});
// call on load 
$('#sales_stage').trigger("change");

});

Apparently it should work as not requiring the Expected Close Date on Create Opportunity (as Prospect is default stage) yet it does not work and still the date is Mandatory.

It only works when i Change the Dropdown from Prospect to Another Status and then back to Prospect. NOT on the Load or First Go. Is it related to the validate[“EditView”] object?

This PR adds a validation, and it seems to do 2 things: add a CSS class “required”, and call the Javascript addToValidate function.

https://github.com/salesagility/SuiteCRM/pull/474/files

Maybe you can check that CSS class in your case?

REMOVING the class=“required” from the LABEL span element worked for me. Following is the LABEL html and note the span class which needs to be removed to make it NOT Required.

<div class="col-xs-12 col-sm-4 label" data-label="LBL_DATE_CLOSED">
Expected Close Date:<span class="required">*</span></div>

If you check the browser’s console, you’ll find the theme creates an different object as the label. That object is the one that receives the validation star. The default label of the field is never displayed. Now, regarding the created label object, is hard to find the name. To modify it I use my field name to get the parent. This is what I use to add the star. It might give you an idea on how to remove it.


// adding star to label
$('#myfield').parent().parent().children().first().html('{$mod_strings['LBL_MYFIELD']}: <span class=\"required\">*</span>');	

Thanks,

AlxGr

I can get that Element via following Code.

$('#date_closed').closest('div').prev().find('span');

One thing that is apparently causing issue is that the removeFromValidate function is not available when the document ready is called. The reason being that validation code is appended in the document in the last, as checked via inspecting dom elements.