Good day!
I have a complex validation issue which I can't fix.
Here is my scenario:
I have a drop-down field called 'Staff group'
IF user select Medical & dental from the 'Staff Group' it shows the two single choice hours selectors (see below):
If user selects 'Hours' it will show two hours fields, I want it to validate fields under that choice.
If users select 'PAs', it will show two PAs fields, I want it to validate the fields under that choice.
I have typed up this code to handle both the Hours and PA's
fd.field('NewPAs').validators.push({
name:"",
error:"",
validate: function(value){
if (fd.field('Main_Staff_Group').value == "Medical & Denatal"){
if (fd.field("Choice").value == "PAs"){
if (fd.field('CurrentPAs').value == fd.field('NewPAs').value){
return false;
this.error= "Please note Current PAs and New PAs cannot be the same"
}
return true;
}
if (fd.field("Choice").value == "Hours"){
if (fd.field('Current_Hours').value == fd.field('PSNewHours').value){
return false;
this.error = "Please note Current Hours and New Hours cannot be the same"
}
}
}
}
});
Here is a screenshot of the form:
UPDATE:
I have also tried this code, but no luck.
function validateHours(){
if (fd.field("Main_Staff_Group").value == "Medical & Dental"){
if (fd.field("Choice").value == "Hours"){
fd.field('Current_Hours').required = true;
fd.field('PSNewHours').required = true;
}
if (fd.field("Choice").value == "PAs"){
fd.field('CurrentPAs').required = true;
fd.field('NewPAs').required = true;
}
}
}
fd.field('Choice').$on('change',validateHours);
I have also tried this:
//validte Hours
fd.field('PSNewHours').validators.push({
name: '',
error: 'Please note Current Hours and New Hours cannot be the same.',
validate: function(value) {
if (fd.field('Choice').value == "Hours"){
fd.field('CurrentPAs').clear();
fd.field('NewPAs').clear();
console.log(value)
if(value < 0 || value > 40 ){
return false;
}
return true;
}
}
});
//validte Hours
fd.field('NewPAs').validators.push({
name: '',
error: 'Please note Current PAs and New PAs cannot be the same.',
validate: function(value) {
if (fd.field('Choice').value == "PAs"){
fd.field('Current_Hours').clear();
fd.field('PSNewHours').clear();
console.log(value)
if(value < 0 || value > 10 ){
return false;
}
return true;
}
}
});
//This will validate the Hours field
fd.field('PSNewHours').addValidator({
name: '',
error: '',
validate:function(value){
if(fd.field("Main_Staff_Group").value == "Medical & Dental"){
if (fd.field("Choice").value == "Hours"){
if (fd.field("Current_Hours") < 0 && fd.field("Current_Hours").value > 40
|| fd.field("PSNewHours") < 0 && fd.field("PSNewHours").value > 40){
return false
this.error = "Please enter a value between 0 and 40"
}
}
}
}
});
Ideally, I want to set-up a threshold for the fields. That would be amazing!