Be able to validate the datepicker format
Bug, feature request, or proposal:
Bug&Proposal
What is the expected behavior?
Have a way to validate the datepicker format.
What is the current behavior?
I cannot use a custom validation for the datepicker since the value is formatted to a date.
What are the steps to reproduce?
In a datepicker introduce for instance '33'. This value is valid because the datepicker is parsing it to a valid date. So the result is that we are not showing an error.
I have tried to use a directive in order to make this validation, but the problem is that the value received in the 'ValidatorFn' is the formatted date, not the original value.
Then I have tried to create a custom date adapter, but this option has the same problem: the 'format' method receives the formatted date, not the original value. There is only one place where we can see the original value, the 'parse' method. But I don't want to extend this method because I want to be synchronized with the latest changes of material.
So it would be great if we could see which is the original value. I think that the 'format' method could receive also that value, so at least we could use it to validate the date.
On the other hand I think that maybe providing a way to disable the automatic format we could have, as a developers, a better control of the date pickers.
I think the second solution is better because it gives us a way to customize the format in the 'format' method and also a way to validate the original value in a directive.
What is the use-case or motivation for changing an existing behavior?
Now when a user sets a date as, for instance, '33', no errors are shown because this value is parsed to a date. We should be able to handle this value and show an error like 'This value is not valid'.
Which versions of Angular, Material, OS, TypeScript, browsers are affected?
Angular: 4.4.3 Material: 2.0.0-beta.11 OS: Windows 10 TypeScript: 2.4.0 Browsers: All
With the 'mdDatepickerFilter' validation we have the same problem: we receive a date, not the original value. This could be also a great place where we could receive the original value to validate it.
In these cases, you can overwrite the DataAdapter parse function. You can decide what the parsing logic is. As it stands, it is using the default parsing logic for browser Date. They have also added a MomentAdapter so you can have a look at that as well.
Anyway, the method 'getValidDateOrNull' contained in the DatePicker is going to return a date or null, so I would lose the original value before validate it. To be clearer:
- the method 'onInput' is launched:
MdDatepickerInput.prototype._onInput = function (value) {
var /** @type {?} */ date = this._dateAdapter.parse(value, this._dateFormats.parse.dateInput);
this._lastValueValid = !date || this._dateAdapter.isValid(date);
date = this._getValidDateOrNull(date);
this._value = date;
this._cvaOnChange(date);
this._valueChange.emit(date);
this.dateInput.emit(new MdDatepickerInputEvent(this, this._elementRef.nativeElement));
};
- I return in the parse method the original value as string (because it is not a valid date).
- Then, the value is passed to the '_getValidDateOrNull' method, where it is parsed to null because it's not a date.
- So now, I have lost the value and when the '_cvaOnChange' method is executed and fires my validation contained in a custom directive, the value that I receive is null, so I cannot show a custom error because null is also returned when the value is empty.
I don't know if I am missing something, so if you think that I am wrong please, tell me how to reach my objective.
@franpsif Have a look at DateAdapter and specifically NativeDateAdapter.
This is the parse function I mentioned earlier (I am assuming that you are using it). The parse is responsible for turning the string that a user types into a date so you should be able to filter non-dates such as 33. However, you can do additional validation on the input itself once you've got the date.
Anyway, in order to receive more help, try to ask more concise questions and give a Plunker or some kind of a demo (e.g. a gif). Also, try SO (https://stackoverflow.com/ ) as this seems more like a support question rather than bug/feature.
@alex321 The problem I see, is when you fill the input with something that is invalid, then the resulting value will be null. As a result the date FormControl looks empty to the validators but the input of the user is still visible in the input field. If you have a required validator, then it will show "this field is required" even if it isn't empty. Also any sort of "this is not a valid date"-validator will be impossible, because it can't know if it is null because of a invalid input or no input at all.
@saithis The DateAdapter does distinguish between null and invalid values. If you want to do some additional checking by overriding the parse function you can do so like this:
parse(value: any): Date | null {
if (!this.myValidCheck()) {
return this.invalid();
}
return super.parse(value);
}
Behind the scenes the NativeDateAdapter uses new Date(NaN) to create invalid dates: https://github.com/angular/material2/blob/master/src/lib/core/datetime/native-date-adapter.ts#L260
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.