In Edit View, addToValidate function is loaded in the footer, if we include a custom JS file (via editviewdefs include parameter) for custom validations and dynamic required fields, it will get executed before (even with doc ready or on load ) while the footer functions run after it. I have tried following options in the Custom JS script to make the custom validations override the default but it does not take effect.
@cherub-chum
I made it without problem. I used 2 functions “removeFromValidate”(remove current) and “addToValidate”(add new) . I switched control for field assigned_user_id . For example look at the codes. Attention the fourth parameter of “addToValidate” function.
switched off:
let msg = document.getElementById('assigned_user_name').parentElement.previousElementSibling.innerHTML.split(':')[0];
document.getElementById('assigned_user_name').parentElement.previousElementSibling.innerHTML = msg+':';
removeFromValidate(name_form,'assigned_user_id');
addToValidate(name_form,'assigned_user_id','varchar',false, msg);
switched on:
var required_switch = document.createElement('span');
required_switch.className = 'required';
required_switch.innerHTML = '*';
document.getElementById('assigned_user_name').parentElement.previousElementSibling.appendChild(required_switch);
let msg = document.getElementById('assigned_user_name').parentElement.previousElementSibling.innerHTML.split(':')[0];
removeFromValidate(name_form,'assigned_user_id');
addToValidate(name_form,'assigned_user_id','varchar',true,msg);
If you inspect Edit View there would be a block of addToValidate Statements. Those are core validations setup at the page load.
Adding the above code and then changing Task Type to Transfer does not adds the Custom field to Required as pressing SAVE button saves the record without asking for the custom required field.
You correctly write about “block of addToValidate Statements”. Before add new record you should remove old record. The function “addToValidate” can not change parameters it is only add but the function “removeFromValidate” remove old record. The validation for field need every time to call 2 functions the first “removeFromValidate” and the second “addToValidate”.
I have added both conditions in the code block as you have mentioned.
By Default the fields are not required.
Looking at the addToValidate code block, it has a statement in the start
addForm(‘EditView’);
the function is defined as
function addForm(formname) {
validate[formname] = new Array();
}
So since the custom JS is at the top (if you include js file via include method in editviewdefs.php) and this block is resetting the validate array, could this be nullifying custom JS validation ?
$(document).ready(function() {
// control change filed: "description"
$("#description").on('change', function(){
// check data in "description" for valid in filed "parent_id"
if($(this).val() !== ''){
// make sumbol "*" for field "Related to"
var required_switch = document.createElement('span');
required_switch.className = 'required';
required_switch.innerHTML = '*';
document.getElementById('parent_name').parentElement.previousElementSibling.appendChild(required_switch);
let msg = document.getElementById('parent_name').parentElement.previousElementSibling.innerHTML.split(':')[0];
// remove old record about field "parent_id"
removeFromValidate('EditView', 'parent_id');
// switch on valid and new message for field "parent_id"
addToValidate('EditView', 'parent_id', 'varchar', true, msg);
}else{
// remove sumbol "*" for field "Related to"
let msg = document.getElementById('parent_name').parentElement.previousElementSibling.innerHTML.split(':')[0];
document.getElementById('parent_name').parentElement.previousElementSibling.innerHTML = msg+':';
// remove old record about field "parent_id"
removeFromValidate('EditView', 'parent_id');
// switch off valid and new message for field "parent_id"
addToValidate('EditView', 'parent_id', 'varchar', false, msg);
}
});
});
The issue was i was looping on the field required and passing extra ‘’ in the addToValidate and removeFromValidate params . @p.konetskiy thank you for your time and effort.