Skip to content

Commit 69eef51

Browse files
committed
rework validation again
1 parent da045a6 commit 69eef51

File tree

4 files changed

+16
-17
lines changed

4 files changed

+16
-17
lines changed

src/demo-app/datepicker/datepicker-demo.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ <h2>Result</h2>
4040
placeholder="Pick a date"
4141
(dateInput)="onDateInput($event)"
4242
(dateChange)="onDateChange($event)">
43-
<md-error *ngIf="resultPickerModel.hasError('mdDatepickerParse')">Not a valid date!</md-error>
43+
<md-error *ngIf="resultPickerModel.hasError('mdDatepickerParse')">
44+
"{{resultPickerModel.getError('mdDatepickerParse').text}}" is not a valid date!
45+
</md-error>
4446
<md-error *ngIf="resultPickerModel.hasError('mdDatepickerMin')">Too early!</md-error>
4547
<md-error *ngIf="resultPickerModel.hasError('mdDatepickerMax')">Too late!</md-error>
4648
<md-error *ngIf="resultPickerModel.hasError('mdDatepickerFilter')">Date unavailable!</md-error>

src/lib/datepicker/calendar.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ export class MdCalendar<D> implements AfterContentInit, OnDestroy {
154154

155155
/** Handles date selection in the month view. */
156156
_dateSelected(date: D): void {
157-
console.log('date selected', date, this.selected);
158157
if (!this._dateAdapter.sameDate(date, this.selected)) {
159158
this.selectedChange.emit(date);
160159
}

src/lib/datepicker/datepicker-input.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces
119119
if (value != null && !this._dateAdapter.isDateInstance(value)) {
120120
throw Error('Datepicker: value not recognized as a date object by DateAdapter.');
121121
}
122+
this._lastValueValid = !value || this._dateAdapter.isValid(value);
122123
value = this._getValidDateOrNull(value);
123124

124125
let oldDate = this.value;
@@ -133,7 +134,7 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces
133134
@Input()
134135
get min(): D | null { return this._min; }
135136
set min(value: D | null) {
136-
this._min = this._getValidDateOrNull(value);
137+
this._min = value;
137138
this._validatorOnChange();
138139
}
139140
private _min: D | null;
@@ -142,7 +143,7 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces
142143
@Input()
143144
get max(): D | null { return this._max; }
144145
set max(value: D | null) {
145-
this._max = this._getValidDateOrNull(value);
146+
this._max = value;
146147
this._validatorOnChange();
147148
}
148149
private _max: D | null;
@@ -173,9 +174,9 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces
173174
private _datepickerSubscription: Subscription;
174175

175176
/** The form control validator for whether the input parses. */
176-
private _parseValidator: ValidatorFn = (control: AbstractControl): ValidationErrors | null => {
177-
return (!control.value || this._dateAdapter.isValid(control.value)) ?
178-
null : {'mdDatepickerParse': true};
177+
private _parseValidator: ValidatorFn = (): ValidationErrors | null => {
178+
return this._lastValueValid ?
179+
null : {'mdDatepickerParse': {'text': this._elementRef.nativeElement.value}};
179180
}
180181

181182
/** The form control validator for the min date. */
@@ -203,6 +204,9 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces
203204
Validators.compose(
204205
[this._parseValidator, this._minValidator, this._maxValidator, this._filterValidator]);
205206

207+
/** Whether the last value set on the input was valid. */
208+
private _lastValueValid = false;
209+
206210
constructor(
207211
private _elementRef: ElementRef,
208212
private _renderer: Renderer2,
@@ -280,6 +284,8 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces
280284

281285
_onInput(value: string) {
282286
let date = this._dateAdapter.parse(value, this._dateFormats.parse.dateInput);
287+
this._lastValueValid = !date || this._dateAdapter.isValid(date);
288+
date = this._getValidDateOrNull(date);
283289
this._cvaOnChange(date);
284290
this._valueChange.emit(date);
285291
this.dateInput.emit(new MdDatepickerInputEvent(this, this._elementRef.nativeElement));

src/lib/datepicker/datepicker.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export class MdDatepicker<D> implements OnDestroy {
129129
// selected value is.
130130
return this._startAt || (this._datepickerInput ? this._datepickerInput.value : null);
131131
}
132-
set startAt(date: D | null) { this._startAt = this._getValidDateOrNull(date); }
132+
set startAt(date: D | null) { this._startAt = date; }
133133
private _startAt: D | null;
134134

135135
/** The view that the calendar should start in. */
@@ -165,7 +165,7 @@ export class MdDatepicker<D> implements OnDestroy {
165165

166166
/** The currently selected date. */
167167
get _selected(): D | null { return this._validSelected; }
168-
set _selected(value: D | null) { this._validSelected = this._getValidDateOrNull(value); }
168+
set _selected(value: D | null) { this._validSelected = value; }
169169
private _validSelected: D | null = null;
170170

171171
/** The minimum selectable date. */
@@ -347,12 +347,4 @@ export class MdDatepicker<D> implements OnDestroy {
347347
{ overlayX: 'end', overlayY: 'bottom' }
348348
);
349349
}
350-
351-
/**
352-
* @param obj The object to check.
353-
* @returns The given object if it is both a date instance and valid, otherwise null.
354-
*/
355-
private _getValidDateOrNull(obj: any): D | null {
356-
return (this._dateAdapter.isDateInstance(obj) && this._dateAdapter.isValid(obj)) ? obj : null;
357-
}
358350
}

0 commit comments

Comments
 (0)