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.