FEAT_DYNAMIC_REQUIRED_AND_NOT_REQUIRED_CLAIM_GMC_SRI

This commit is contained in:
Srinivas-Saravanan 2025-02-27 18:00:22 +05:30
parent 5e609027ca
commit 2dc0eefe4c

View File

@ -563,7 +563,46 @@
} }
}); });
$('.' + thirdClass).find('input, textarea').attr('required', true); // $('.' + thirdClass).find('input, textarea').attr('required', true);
let extrafieldsArray = $('#extra_fields_array_for_validate').val();
try {
extrafieldsArray = JSON.parse(extrafieldsArray); // Convert string to object
} catch (error) {
console.error("Invalid JSON format in extra_fields_array_for_validate:", error);
return;
}
var selectedClaimStatus = $('#claim_status_id').val();
var $thirdClass = $(`.${thirdClass}`);
var hasValue = $thirdClass.find('input, textarea, select').filter(function () {
return $(this).val().trim() !== ''; // Check if at least one field is not empty
}).length > 0;
$thirdClass.each(function () {
var id = $(this).find('[id]').first().attr('id');
var $label = $(this).find('label');
var $thisDiv = $(this);
var $inputs = $thisDiv.find('input, textarea, select');
// ✅ Condition 1: Check if this ID is in extraFieldsArray (required by status)
var shouldAddRequired = extrafieldsArray[selectedClaimStatus] && extrafieldsArray[selectedClaimStatus].includes(id);
// ✅ Condition 2: If any field in the class has a value, make all required
if (shouldAddRequired || hasValue) {
if ($label.length && !$label.find('.text-danger').length) {
$label.append(' <span class="text-danger">*</span>');
}
console.log("input from if ",$inputs);
// $inputs.attr('required', true);
$('.' + thirdClass).find('input, textarea,select').attr('required', true);
} else {
$label.find('.text-danger').remove();
console.log("input ",$inputs);
// $inputs.prop('required', false);
$('.' + thirdClass).find('input, textarea,select').attr('required', false);
}
});
} }
@ -585,7 +624,7 @@
if ($parentDiv.length) { if ($parentDiv.length) {
let thirdClass = $parentDiv.attr("class").split(" ")[2] || ""; let thirdClass = $parentDiv.attr("class").split(" ")[2] || "";
console.log('thirdClass', thirdClass); console.log('thirdClass', thirdClass);
addRequiredFieldSymbolByClass(thirdClass); // addRequiredFieldSymbolByClass(thirdClass);
// addRequiredFieldSymbol(targetId); // addRequiredFieldSymbol(targetId);
if (thirdClass) { if (thirdClass) {
// console.log("fields",fields); // console.log("fields",fields);
@ -606,64 +645,97 @@
} }
}); });
function addRequiredFieldSymbol(field_name) { // function addRequiredFieldSymbol(field_name) {
// Find the field with the given name // // Find the field with the given name
var $field = $(`[name="${field_name}"]`); // var $field = $(`[name="${field_name}"]`);
if ($field.length) { // if ($field.length) {
// Find the parent div of this field // // Find the parent div of this field
var $parentDiv = $field.closest('div'); // var $parentDiv = $field.closest('div');
if ($parentDiv.length) { // if ($parentDiv.length) {
// Get the classes as an array // // Get the classes as an array
var classes = $parentDiv.attr('class') ? $parentDiv.attr('class').split(/\s+/) : []; // var classes = $parentDiv.attr('class') ? $parentDiv.attr('class').split(/\s+/) : [];
if (classes.length > 0) { // if (classes.length > 0) {
// Get the last class in the array // // Get the last class in the array
var lastClass = classes[classes.length - 1]; // var lastClass = classes[classes.length - 1];
// Find all divs with this last class // // Find all divs with this last class
var $allDivsWithClass = $(`.${lastClass}`); // var $allDivsWithClass = $(`.${lastClass}`);
// Add asterisk to all labels within these divs // // Add asterisk to all labels within these divs
$allDivsWithClass.each(function() { // $allDivsWithClass.each(function() {
var $label = $(this).find('label'); // var $label = $(this).find('label');
if ($label.length && !$label.find('.text-danger').length) { // if ($label.length && !$label.find('.text-danger').length) {
$label.append(' <span class="text-danger">*</span>'); // $label.append(' <span class="text-danger">*</span>');
} // }
}); // });
console.log(`Added required symbol to ${$allDivsWithClass.length} labels with class "${lastClass}"`); // console.log(`Added required symbol to ${$allDivsWithClass.length} labels with class "${lastClass}"`);
} else { // } else {
console.warn(`Parent div for field "${field_name}" has no classes`); // console.warn(`Parent div for field "${field_name}" has no classes`);
} // }
} else { // } else {
console.warn(`No parent div found for field name="${field_name}"`); // console.warn(`No parent div found for field name="${field_name}"`);
} // }
} else { // } else {
console.warn(`No field found with name="${field_name}"`); // console.warn(`No field found with name="${field_name}"`);
} // }
} // }
function addRequiredFieldSymbolByClass(className) { function addRequiredFieldSymbolByClass(className) {
// Find all divs with the specified class
var $divsWithClass = $(`.${className}`); var $divsWithClass = $(`.${className}`);
if ($divsWithClass.length) { if ($divsWithClass.length) {
// Add asterisk to all labels within these divs let extrafieldsArray = $('#extra_fields_array_for_validate').val();
$divsWithClass.each(function() { try {
var $label = $(this).find('label'); extrafieldsArray = JSON.parse(extrafieldsArray); // Convert string to object
if ($label.length && !$label.find('.text-danger').length) { } catch (error) {
$label.append(' <span class="text-danger">*</span>'); console.error("Invalid JSON format in extra_fields_array_for_validate:", error);
return;
} }
});
console.log(`Added required symbol to ${$divsWithClass.length} labels with class "${className}"`); var selectedClaimStatus = $('#claim_status_id').val();
console.log("Selected claim status ID:", selectedClaimStatus);
console.log("Extra Field Array:", extrafieldsArray[selectedClaimStatus]);
// ✅ Check if at least one field inside the class has a value
// var hasValue = $divsWithClass.find('input, textarea, select').filter(function () {
// return $(this).val().trim() !== ''; // Check if at least one field is not empty
// }).length > 0;
// $divsWithClass.each(function () {
// var id = $(this).find('[id]').first().attr('id');
// var $label = $(this).find('label');
// var $thisDiv = $(this);
// var $inputs = $thisDiv.find('input, textarea, select');
// // ✅ Condition 1: Check if this ID is in extraFieldsArray (required by status)
// var shouldAddRequired = extrafieldsArray[selectedClaimStatus] && extrafieldsArray[selectedClaimStatus].includes(id);
// // ✅ Condition 2: If any field in the class has a value, make all required
// if (shouldAddRequired || hasValue) {
// if ($label.length && !$label.find('.text-danger').length) {
// $label.append(' <span class="text-danger">*</span>');
// }
// console.log("input from if ",$inputs);
// $inputs.attr('required', true);
// } else {
// $label.find('.text-danger').remove();
// console.log("input ",$inputs);
// $inputs.prop('required', false);
// }
// });
console.log(`Processed ${$divsWithClass.length} labels with class "${className}"`);
} else { } else {
console.warn(`No divs found with class="${className}"`); console.warn(`No divs found with class="${className}"`);
} }
} }
$('#mode_of_intimation').change(function(){ $('#mode_of_intimation').change(function(){
console.log("function called"); console.log("function called");
var mode_of_intimation = $('#mode_of_intimation').val(); var mode_of_intimation = $('#mode_of_intimation').val();