Angular 2 form validations start date <= end date

前端 未结 6 2015
孤街浪徒
孤街浪徒 2020-12-09 05:01

I\'m trying to add validations such that the end date can\'t be before the start date. Unfortunately I have no idea how to do that, and I didn\'t find any helpful advice in

6条回答
  •  北荒
    北荒 (楼主)
    2020-12-09 05:16

    create a form group . Let the controls be a part of form group .

           new FormGroup({   
            startDate: this.fb.group({
                dateInput: [{value: ""}, startDateValidator()]
            }),   
            endDate: this.fb.group({
                dateInput: ["", endDateValidator()]
            })  
        }, startDateCannotBeLessThanEndDateValidator());   
    
            startDateCannotBeLessThanEndDateValidator(formGroup: FormGroup) {
    
                 let startDate = formGroup.get("startDate");   
                let endDate = formGroup.get("endDate");
                // compare 2 dates 
            }
    

提交回复
热议问题