I have a requirement where I need to validate two date fields but they're both need to be related to each other.
The only problem I'm having is, it won't throw an error if the last working day exceeds the termination date that is selected
//This code will hide future dates in the Last Working Day field
fd.spRendered(function(){
fd.field('Employee_Termination_Date').validators.push({
name: '',
error: "Employee last Working Day must be before the Termination Date",
validate: function(value) {
if(fd.field('Employee_Termination_Date').value){
fd.field('Employee_Last_Working_Day').widgetOptions = {max: new Date()}
return true;
if (fd.field('Employee_Termination_Date').value >= fd.field('Employee_Last_Working_Day').value){
return true;
}
if (fd.field('Employee_Last_Working_Day').value > fd.field('Employee_Termination_Date').value){
return false;
}
}
}
});
});
Dear @DryChips,
You're returning true, before the condition can be checked, use it like this, and move setting of max date outside:
function setMaxDate(){
if(fd.field('Employee_Termination_Date').value){
fd.field('Employee_Last_Working_Day').widgetOptions = {max: new Date() };
}
else{
fd.field('Employee_Last_Working_Day').widgetOptions = {};
}
}:
//This code will hide future dates in the Last Working Day field
fd.spRendered(function(){
setMaxDate();
fd.field('Employee_Termination_Date').$on('change',function(){
setMaxDate();
});
fd.field('Employee_Termination_Date').validators.push({
name: '',
error: "Employee last Working Day must be before the Termination Date",
validate: function(value) {
if (fd.field('Employee_Last_Working_Day').value > fd.field('Employee_Termination_Date').value){
return false;
}
}
});
});
Just a quick question, is it possible to hide future dates based on whatever date is selected.
To elaborate, IF a user selects, 10/02/2022, I want it to hide all dates after this....
Also, is it possible to validate the setMaxDate function? I can't seem to think of a way to validate it.
fd.spRendered(functon(){
function setMaxDate(){
if (fd.field('Employee_Termination_Date').value){
fd.field('Employee_Last_Working_Day').widgetOptions = {max: new Date()};
}
else{
fd.field('Employee_Last_Working_Day').widgetOptions = {};
}
}
//calling function on change
fd.field('Employee_Termination_Date').$on('change',setMaxDate)
});
Whilst testing, I've noticed that users can enter a date manually which overrides the validation that I've set via this code:
//checks if last working day > termination date
fd.spRendered(function(){
fd.field('Employee_Last_Working_Day').validators.push({
name: '',
error: "Employee last Working Day must be before the Termination Date",
validate: function(value) {
if (fd.field('Employee_Last_Working_Day').value > fd.field('Employee_Termination_Date').value){
return false;
}
return true;
}
});
});
I have noticed that the date field lets users type in string values and completely voids the validation that I have set-up. How can I handle this exception?
//checks if last working day > termination date [DO NOT DELETE]
fd.field('Employee_Last_Working_Day').validators.push({
name: '',
error: "Employee last Working Day must be on or before the Termination Date",
validate: function(value) {
if (fd.field('Employee_Last_Working_Day').value > fd.field('Employee_Termination_Date').value){
$(fd.field('Reason_For_Difference').$parent.$el).hide();
return false;
}
return true;
}
});
//This code will show/hide the reason for difference field based on user selection.
function showhideReasonForDifference(){
if (fd.field('Employee_Termination_Date').value == '' && fd.field('Employee_Last_Working_Day').value == ''){
//hide reasonForDifference field when no difference between termination & last working day
$(fd.field('Reason_For_Difference').$parent.$el).hide();
//Hides the field in the review wizard
$(fd.field('RReasonForDifference').$parent.$el).hide();
}
//show reasonForDifference when there is a difference between termination & last working day
if (fd.field('Employee_Termination_Date').value > fd.field('Employee_Last_Working_Day').value){
$(fd.field('Reason_For_Difference').$parent.$el).show();
$(fd.field('RReasonForDifference').$parent.$el).show();
}
else {
$(fd.field('Reason_For_Difference').$parent.$el).hide();
$(fd.field('RReasonForDifference').$parent.$el).hide();
fd.field('Reason_For_Difference').clear();
}
}
// Calling function when Destination on Leave value changes
fd.field('Employee_Last_Working_Day').$on('change',showhideReasonForDifference);
// Calling function on form loading
showhideReasonForDifference();
Dear @DryChips,
Without going into too much detail, how does it void your validation? What's the value that gives you trouble? You can also validate for data type as well, or convert the input string into date if necessary.
So when I type a random string value of say "gnurdngnfgwr" or "22nd April 2022" in the date field, it lets me proceed to the next wizard. In my Organisation, there will be cases where people will put silly values in the field, even though they should specifically put a numerical date in the field.
I have a problem with my validation, nothing to complex I hope.
Here is the logic:
IF Termination Date field and Last Working Date field are Greater than or Equal to each other return True.
IF Last Working Date field is Greater Than the Termination Date field then return False.
My problem:
IF both fields have a value and user decides that the Termination Date field is wrong and goes and interacts with the Termination Date field, the error message under the Last Working Date field doesn't update/disappear as the condition is now "True".
//checks if last working day is empty - [Wizard 3]
fd.field('Employee_Last_Working_Day').validators.push({
name: '',
error: "Please enter the Employee's last working day",
validate: function(value) {
if (fd.field('Question_2').value == 'Yes' && fd.field('Question_3').value == 'No'
&& fd.field('Employee_Last_Working_Day').value == null){
return false;
}
return true;
}
});
//checks if last working day > Termination date - [Wizard 3]
fd.field('Employee_Last_Working_Day').validators.push({
name: "",
error: "",
validate: function(value) {
if (fd.field('Employee_Last_Working_Day').value > fd.field('Termination_Date').value){
this.error = "Employee last working day must be on or before the termination date"
return false;
}
return true;
}
});
//checks if last working day > Termination date - [Wizard 3]
fd.field('Termination_Date').validators.push({
name: "",
error: "",
validate: function(value) {
if (fd.field('Termination_Date').value >= fd.field('Employee_Last_Working_Day').value){
fd.validators.length = 0;
return true;
}
}
});
Update:
I figured it out.
I used an on-Change function to resolve this.
Here is my code:
//this code will validate the last working day
function ValidateLastWorkingDay(){
if (fd.field('Termination_Date').value >= fd.field('Employee_Last_Working_Day').value){
//validate last workiing day
fd.field('Employee_Last_Working_Day').validate();
}
}
// Calling validateFields when the Status value changes
fd.field('Termination_Date').$on('change', ValidateLastWorkingDay);
I've added some validation to the ProjectStartDate field when users enter a date greater than the ProjectEndDate.
This is the code I'm using:
//Adds validation to start date field
fd.field("ProjectStartDate").validators.push({
name:"",
error:"Start Date must be less than the End Date",
validate: function(value){
if (fd.field("ProjectStartDate").value > fd.field("ProjectEndDate").value){
return false;
}
return true;
}
})
In the video, I typed in a value (using keyboard) greater than the ProjectEndDate and the form allowed me to pass through to the next wizard. How do you handle this exception?
I just noticed that in the video you entered a correct date range. The error message didn't disappear when it should have. Could you text again with an incorrect date range?