Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(datepicker): input validator is not called until a date is first chosen #2727

Closed
FrancescoBorzi opened this issue Sep 26, 2017 · 5 comments · Fixed by #3499
Closed

fix(datepicker): input validator is not called until a date is first chosen #2727

FrancescoBorzi opened this issue Sep 26, 2017 · 5 comments · Fixed by #3499

Comments

@FrancescoBorzi
Copy link
Member

FrancescoBorzi commented Sep 26, 2017

You have a model-driven form with a field which is a Datepicker element.

If the value of this field is null by default, then the validator of the field will not be called until a date is chosen using the datepicker.

This means that if for example you just select the field, put some random characters (without selecting any date) and then walk away from then field, the validator will still not be called and then the form will be valid in any case (unless you are making that field as required).

Just to stress what I mean: the input field which uses the Datapicker should be marked as NON pristine as soon as the user enters ANY value. Currently it preserves its pristine status as the user types any character, it looses it only when a date is selected.

@FrancescoBorzi
Copy link
Member Author

As a workaround, I added a hackfix forceValidation on keyup to my input field the following:

<input
  formControlName="myDate"
  bsDatepicker
  bsValue="form.get('myDate')"
  value="{{ form.get('myDate').value | date:'yyyy-MM-dd' }}"
  (keyup)="forceValidation('myDate')">
...
...
  forceValidation(controlName: string) {
    const control = this.form.get(controlName);
    control.markAsDirty();
    control.updateValueAndValidity();
  }

@valorkin
Copy link
Member

valorkin commented Oct 2, 2017

@ShinDarth on the roadmap

@valorkin valorkin added this to the v2.0.0-rc.2 milestone Jan 11, 2018
@YevheniiaMazur YevheniiaMazur changed the title Datepicker input validator is not called until a date is first chosen fix(datepicker): input validator is not called until a date is first chosen Jan 16, 2018
@FrancescoBorzi
Copy link
Member Author

the fix worked, thanks a lot!

@skptricks
Copy link

nice example… Also you can refer to this new type of data picker, support both mobile and web : http://www.skptricks.com/2018/01/jquery-date-picker-with-example.html

@pradeepmay13
Copy link

Hi,
I am unable to validate using custom validator on datepicker while entering junk data it returns Invalid Date.
For Blank input field validation is working fine as shown in figure
datepicker

But when i am entering character or junk data it returns Invalid date but i am unable to apply angular validation. Please help
datepicker2

HTML Code for This

<input type="text" class="form-control" name="rrf_temp_tenure_from_date" formControlName="rrf_temp_tenure_from_date" placeholder="From Date" bsDatepicker [bsConfig]="{ dateInputFormat: 'DD-MM-YYYY', containerClass:'theme-dark-blue' }">

<div *ngIf="rrfForm.controls['rrf_temp_tenure_from_date'].touched && !rrfForm.controls['rrf_temp_tenure_from_date'].valid " class="text-danger">
<span *ngIf="rrfForm.controls['rrf_temp_tenure_from_date'].hasError('required') ">
From Date is required

<span *ngIf="!rrfForm.controls.rrf_temp_tenure_from_date.hasError('required') && rrfForm.controls.rrf_temp_tenure_from_date.hasError('invalidDate')">Please Enter Valid Date

TS file code
this.rrfForm.get('rrf_temp_tenure_from_date').setValidators([Validators.required, ValidationService.dateValidator]);

Service function

static dateValidator(control) {
if (control.value.match(/^(0[1-9]|1\d|2\d|3[01]).(0[1-9]|1[0-2]).(19|20)\d{2}$/)) {
return null;
} else {
return { 'invalidPassword': true };
}
}

Getting Error on Console :
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants