Date Validation

Hi,

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;
            }
            }
        }
    });    
});

Please can you advise/help where possible?

Many thanks!

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;
              }
        }
    });    
});
1 Like

Awesome! That's perfect.

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; 
       }
    });

});

Dear @DryChips,
Try it like this:

fd.spRendered(functon(){
    function setMaxDate(){
      if (fd.field('Employee_Termination_Date').value){
        fd.field('Employee_Last_Working_Day').widgetOptions = 
          { max: fd.field('Employee_Termination_Date').value };
       }
      else{
        fd.field('Employee_Last_Working_Day').widgetOptions = {};
      }
    }
    //calling function on change
    fd.field('Employee_Termination_Date').$on('change',setMaxDate)
});
1 Like

Perfect! Thank you Nikita.

Hi Nikita,

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.

Hi Nikita,

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.

Dear @DryChips,
Non-date values shouldn't be accepted. Can you test it from console? Add this line to the JS on the form:

window.fd = fd;

Then try to input some silly value and try fd.field('Date').value in console.

Hi Nikita,

Here is the Outcome:

image

This lets me proceed to the next wizard. I haven't tested this with server side validation at the moment....

UPDATE

I have added server side validation and this has resolved the issue. Aka, 'enforce unique values' on SharePoint side.

Hiya Nikita,

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".

What am I doing wrong here?

My code:

//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);
1 Like