We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Datepicker with masking and ranges enabled does not fire validation when invalid

Hey,

The issue seems to be with the following code:

 <ejs-datepicker [openOnFocus]='false' [showTodayButton]='false' [min]="dateMin"
                                [max]="dateMax" [enableMask]="true"
                                [formControl]="newProviderForm.get('DateOfBirth')" 
                                format="MM/dd/yyyy" [maskPlaceholder]="maskPlaceholderValue">
 </ejs-datepicker>


Steps to replicate issue:

  1. Create control as above.
  2. Attempt to enter values that are out of the range set in dateMin or dateMax
  3. Notice that validation on the control shows a red line, indicating an invalid value. 
  4. Check the form validity and it shows that it is "VALID".
  5. Check the control validity and it also shows "VALID"

This behaviour is unexpected as the field has a value that is considered invalid but does not indicate the form field validation as such.

4 Replies

BC Berly Christopher Syncfusion Team August 25, 2021 12:46 PM UTC

Hi Brandon M, 
  
Greetings from Syncfusion support. 
  
We would like to inform you that, the reported issue is not related to form validation and this is component’s built-in range validation. Since, when the entered value (“1/7/202 - 2/25/202”) passed to the new Date() constructor and returns the valid date object, then the component state is set as valid and component’s model value has the valid date value.  
  
 
 
 
  
If the new Date() constructor returns the value as invalid date(numeric value, alphabetic value,.. etc) then the component state set to invalid. 
  
 
For brief explanation, if the min is “new Date("1/1/2021")” and max is “new Date("1/31/2021")” and the value is “new Date()” then component displays the value with e-error border since that is a valid value which is entered beyond with min and max range.  
 
 
 
DatePicker Valid? True 
 
 
Similar that, if you are entering any valid date value which is beyond to the default min and max value means, the error class has been added and component state is maintained as valid. 
 
 
 
If you are clearing the value, then model value null and component state goes to invalid. 
 
Regards, 
Berly B.C 



BM Brandon M August 25, 2021 05:12 PM UTC

Berly,

"We would like to inform you that, the reported issue is not related to form validation and this is component’s built-in range validation."

That is... the issue.

Why does the control carry a validation system of its own? What is the point of having a red outline when it doesn't actually validate the control in any way? What is the use case of a validation system that doesn't validate the control? This is most definitely not the expected behaviour, as it is inconsistent with literally any other prebuilt component found anywhere.

If the min-max field values apparently don't actually extend to the form validation, how are we supposed to validate the control?

"Similar that, if you are entering any valid date value which is beyond to the default min and max value means, the error class has been added and component state is maintained as valid."

I am aware of that- and that behaviour is even more problematic.

This means that getting value always returns null when it's not a valid date, making it impossible to apply any sort of manual validation.


Your attempt to justify the improper behaviour of the control leads me to believe that you are not acknowledging this as an issue.

It seems like the solution here is to move away from using Syncfusion controls in it's entirety as they are fundamentally nowhere near feature complete.

Please escalate this issue as it makes the control completely useless.



BC Berly Christopher Syncfusion Team August 26, 2021 04:05 PM UTC

Hi Brandon, 
  
Sorry for the inconvenience caused. 
  
We will validate the requested requirement and update the further details in two business days (30th August 2021).  
  
Regards, 
Berly B.C 



SN Sevvandhi Nagulan Syncfusion Team August 31, 2021 11:04 AM UTC

Hi Brandon, 

Sorry for the inconvenience caused. 

As we mentioned earlier, we have showcased the error class in the component for the user identification when the entered value beyond to the min and max value. This behavior not related to the built-in form validation. Since the form validation will be done based on the date value in the form instance.  

So, we have prepared the sample for showcase the error message when you provide the value which is beyond to the min and max value with help of custom validator in reactive form. Kindly refer the below code example. 

function minDateValidator(minDatevalueDate) { 
  return (control): { [keystring]: boolean } | null => { 
    if (+min < +control.value) { 
      return { minDateValidator: false }; 
    } else { 
      return { minDateValidator: true }; 
    } 
  }; 
} 
function maxDateValidator(maxDatevalueDate) { 
  return (control): { [keystring]: boolean } | null => { 
    if (+max > +control.value) { 
      return { maxDateValidator: false }; 
    } else { 
      return { maxDateValidator: true }; 
    } 
  }; 
} 
@Component({ 
  selector: 'app-root', 
  templateUrl: 'app.component.html', 
  styleUrls: ['app.component.css'], 
  encapsulation: ViewEncapsulation.None, 
  providers: [MaskedDateTimeService] 
}) 
export class AppComponent { 
  @ViewChild('dateObj') 
  public datepickerObjectDatePickerComponent; 
  min = new Date('8/1/2021'); 
  max = new Date('8/31/2021'); 
  value = new Date('08/19/2022'); 
  dateMask = 'MM/dd/yyyy'; 
  maskPlaceholderValue = 'MM/dd/yyyy'; 
  skillFormFormGroup; 
 
 createForm(): void { 
    this.skillForm = this.fb.group({ 
      date: [ 
        this.value, 
        [ 
          minDateValidator(this.minthis.value), 
          maxDateValidator(this.maxthis.value) 
        ] 
      ] 
    }); 
  } 
  ngAfterViewInit() { 
    this.skillForm.controls['date'].updateValueAndValidity(); 
  } 
  onClick() { 
    if (this.skillForm.valid) { 
      console.log('form submitted'); 
    } else { 
      this.skillForm.controls['date'].updateValueAndValidity(); 
    } 
  } 
} 
<form [formGroup]="skillForm"> 
    <ejs-datepicker [openOnFocus]='false' #dateObj [showTodayButton]='false' [enableMask]="true" format="MM/dd/yyyy" 
        [maskPlaceholder]="maskPlaceholderValue" formControlName="date" [(value)]="value" placeholder="Date" 
        floatLabelType="Auto" [min]="min" [max]="max"> 
    </ejs-datepicker> 
    <span class='e-error' *ngIf= "skillForm.get('date').errors?.minDateValidator">Please select a date after min value</span> 
    <span class='e-error' *ngIf= "skillForm.get('date').errors?.maxDateValidator">Please select a date after max value</span> 
    <br/> 
    <button (click)="onClick($event)">Submit</button> 
</form> 
 

Please find the sample from the below link. 
 

Regards, 
Sevvandhi N 


Loader.
Up arrow icon