简体   繁体   English

如何对缺货日实施自定义日期验证

[英]How to implement a custom date validation for Out of Stock days

I am trying to implement a custom date validator where I can check if the date entered is not in a list of invalid dates (array of custom dates, ex: ['08/20', '08/24', '08/31'] ) (Out of Stock) using Angular reactive forms. 我正在尝试实现一个自定义日期验证器,我可以在其中检查输入的日期是否不在无效日期列表中(自定义日期的数组,例如: ['08/20', '08/24', '08/31'] )(缺货),使用Angular反应形式。

My validator looks like this: 我的验证器如下所示:

export function OutOfStockValidator(invalidDates: any): ValidatorFn {
    return (control: AbstractControl): { [key: string]: boolean } | null => {
        if (!invalidDates) { return null; }
        let isInvalid = false;
        for (const i in invalidDates) {
            const d = moment(control.value).format('MM' ) + '/' + moment(control.value).format('DD');
            if (d == invalidDates[i]) { isInvalid = true; }
        }

        return isInvalid ? { outOfStock: true } : null;
    };
}

This is how the definition of the control looks like: 控件的定义如下所示:

this.form = this._formBuilder.group({
    installDate: new FormControl(null, [Validators.required, OutOfStockValidator(this._order.InvalidDates['CARPET_AM'])]),
    ...
});

Here is my template: 这是我的模板:

<div class="form-group">
    <div class="row">
        <label [for]="installDate" class="col-md-12 col-form-label font-weight-bold">Install Date</label>
        <div class="col-md-12">
            <kendo-datepicker #installDate formControlName="installDate" placeholder="" class="form-control col-md-6" [min]="today">
                <ng-template kendoCalendarMonthCellTemplate let-date>
                    <span [ngClass]="isInvalidDate(date)">{{date.getDate()}}</span>
                </ng-template>
            </kendo-datepicker>
        </div>
    </div>
    <div *ngIf="submitted && f.installDate.outOfStock" class="invalid-feedback d-block">
        <p>Selected date is not available for selection</p>
    </div>
 </div>

The widget works good, but I can not display my custom message: Selected date is not available for selection. 该小部件工作正常,但我无法显示自定义消息:所选日期不可选择。

Any idea? 任何想法? Thanks! 谢谢!

The problem is that you are not checking the error properly in the *ngIf . 问题是您没有在*ngIf正确检查错误。

This *ngIf="submitted && f.get('installDate').errors?.outOfStock" should have the desired effect. *ngIf="submitted && f.get('installDate').errors?.outOfStock"应该具有预期的效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM