primeng icon indicating copy to clipboard operation
primeng copied to clipboard

minDate maxDate validation for manually inserted date via input

Open britvik opened this issue 9 years ago • 11 comments

I'm submitting a ...

[X] feature request

Current behavior MinDate and MaxDate only works inside Calendar (dates are disabled) but date can be changed manually to invalid value by editing input directly.

Expected behavior After editing input value, component should be marked as invalid.

britvik avatar Mar 31 '17 13:03 britvik

Since it's not included in the current releases, is there any update when this is going to be fixed?

ghost avatar Sep 26 '17 08:09 ghost

Any updates on that?

ghost avatar Feb 01 '18 20:02 ghost

@cagataycivici any update on the issue?

CarlosPinedaT avatar Apr 06 '18 04:04 CarlosPinedaT

👍

leonardolessa avatar Apr 10 '18 17:04 leonardolessa

So was the fix for this rolled back?

mauromol avatar Apr 13 '18 10:04 mauromol

any updates on this one?

sky007 avatar Oct 28 '19 16:10 sky007

Any updates on this one? This is really frustrating to try and work around!

dylanesque avatar Mar 27 '20 20:03 dylanesque

I think this issue is caused by onUserInput() in calendar.ts ( release 11.0.0):

    onUserInput(event) {
        // IE 11 Workaround for input placeholder : https://github.com/primefaces/primeng/issues/2026
        if (!this.isKeydown) {
            return;
        }
        this.isKeydown = false;

        let val = event.target.value;
        try {
            let value = this.parseValueFromString(val);
            if (this.isValidSelection(value)) {
                this.updateModel(value);
                this.updateUI();
            }
        }
        catch(err) {
            //invalid date
            this.updateModel(null);
        }

        this.filled = val != null && val.length;
        this.onInput.emit(event);
    }

It happens for a p-calendar control configured with a date range, but without the [readonlyInput]="true" flag:

<p-calendar [(ngModel)]="dateValue" [minDate]="minDateValue" [maxDate]="maxDateValue"></p-calendar>

When the user enters a parsable date outside of the minDate-maxDate range, isValidSelection(value) returns false and the control does not update its model.

It would be more consistent if a manually entered invalid date (outside min-max range) called this.updateModel(null) i.e. treated an out-of-range date the same as an unparsable date.

As it stands now, if you have a valid date (within min-max date) and you manually paste in an invalid date then the model does not update. So the control will display the new value, but keeps its original value and validation state.

However, if you manually type in the same invalid date then the internal value will update to null as you type - because the intermediate contents of the input will be unparsable as a date.

This is not a great user experience, and can lead to difficult to diagnose problems.

For template driven forms (using ngModel) the current work-around in the documentation is to set [readonlyInput]="true":

https://www.primefaces.org/primeng/showcase/#/calendar

If you are using Reactive Forms then another approach is to set validators on your FormControl instance and ignore the minDate and maxDate properties on p-calendar.

But it would be nice if the Calendar control updated its model for manually entered out-of-range dates.

tmc101 avatar Dec 14 '20 17:12 tmc101

Any updates on this one? This issue is a big trouble for Reactive Forms, not just an enhancement.

Marhc avatar Aug 15 '22 13:08 Marhc

Is there any chance for fix? I'm also relying on calendar min/max date ranges but manual input doesn't take it into account.

jgrabowski avatar Aug 16 '22 13:08 jgrabowski

Hi,

So sorry for the delayed response! Improvements have been made to many components recently, both in terms of performance and enhancement. Therefore, this improvement may have been developed in another issue ticket without realizing it. You can check this in the documentation. If there is no improvement on this, can you reopen the issue so we can include it in our roadmap? Please don't forget to add your feedback as a comment after reopening the issue. These will be taken into account by us and will contribute to the development of this feature. Thanks a lot for your understanding!

Best Regards,

mertsincan avatar Nov 09 '22 14:11 mertsincan