Mudanças entre as edições de "Validating Date from and Date to"
De Basef
(Criou página com 'To create a custom validation with date from and date to: <source lang="javascript"> import { Component, OnInit } from '@angular/core'; import { FormBuilder, FormGroup, Valid...') |
|||
Linha 37: | Linha 37: | ||
</source> | </source> | ||
− | [[Category: | + | [[Category: Angular]] |
Edição atual tal como às 10h39min de 27 de janeiro de 2020
To create a custom validation with date from and date to:
import { Component, OnInit } from '@angular/core'; import { FormBuilder, FormGroup, Validators } from "@angular/forms"; export class ReportsComponent implements OnInit { reportsForm: FormGroup; constructor(private fb: FormBuilder) { } ngOnInit() { this.reportsForm = this.fb.group({ startDate: [''], endDate: [''] }, {validator: this.dateLessThan('startDate', 'endDate')}); } dateLessThan(from: string, to: string) { return (group: FormGroup): { [key: string]: any } => { const inputFrom = group.controls[from]; const inputTo = group.controls[to]; const bothFilled = inputFrom.value && inputTo.value; if (bothFilled && inputFrom.value > inputTo.value) { return { dates: "A data início deve ser menor ou igual à data fim." }; } return {}; } } }