|
| 1 | +import { Component, Directive, Input, ViewChild } from '@angular/core'; |
| 2 | +import { AbstractControl, FormGroup, NG_VALIDATORS, ValidationErrors, ValidatorFn, Validators } from '@angular/forms'; |
| 3 | +import { IgxGridComponent } from 'igniteui-angular'; |
| 4 | +import { IGridFormGroupCreatedEventArgs } from 'igniteui-angular/lib/grids/common/grid.interface'; |
| 5 | +import { employeesData } from '../../data/employeesData'; |
| 6 | + |
| 7 | +export function phoneFormatValidator(phoneReg: RegExp): ValidatorFn { |
| 8 | + return (control: AbstractControl): ValidationErrors | null => { |
| 9 | + const match = phoneReg.test(control.value); |
| 10 | + return match ? null : { phoneFormat: { value: control.value } } ; |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +@Directive({ |
| 15 | + selector: '[phoneFormat]', |
| 16 | + providers: [{ provide: NG_VALIDATORS, useExisting: PhoneFormatDirective, multi: true }] |
| 17 | +}) |
| 18 | +export class PhoneFormatDirective extends Validators { |
| 19 | + @Input('phoneFormat') |
| 20 | + public phoneFormatString = ''; |
| 21 | + |
| 22 | + public validate(control: AbstractControl): ValidationErrors | null { |
| 23 | + return this.phoneFormatString ? phoneFormatValidator(new RegExp(this.phoneFormatString, 'i'))(control) |
| 24 | + : null; |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +@Component({ |
| 29 | + selector: 'app-grid-validator-service-extended', |
| 30 | + styleUrls: ['./grid-validator-service-extended.component.scss'], |
| 31 | + templateUrl: './grid-validator-service-extended.component.html' |
| 32 | +}) |
| 33 | +export class GridValidatorServiceExtendedComponent { |
| 34 | + |
| 35 | + @ViewChild('grid1', { read: IgxGridComponent }) |
| 36 | + public grid: IgxGridComponent; |
| 37 | + |
| 38 | + public data = employeesData; |
| 39 | + |
| 40 | + public formCreateHandler(formGroupArgs: IGridFormGroupCreatedEventArgs) { |
| 41 | + const createdOnRecord = formGroupArgs.formGroup.get('created_on'); |
| 42 | + const lastActiveRecord = formGroupArgs.formGroup.get('last_activity'); |
| 43 | + createdOnRecord.addValidators(this.futureDateValidator()); |
| 44 | + lastActiveRecord.addValidators([this.pastDateValidator(), this.futureDateValidator()]); |
| 45 | + } |
| 46 | + |
| 47 | + public commit() { |
| 48 | + const invalidTransactions = this.grid.validation.getInvalid(); |
| 49 | + if (invalidTransactions.length > 0 && !confirm('You\'re committing invalid transactions. Are you sure?')) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + this.grid.transactions.commit(this.data); |
| 54 | + this.grid.validation.clear(); |
| 55 | + } |
| 56 | + |
| 57 | + public undo() { |
| 58 | + /* exit edit mode and commit changes */ |
| 59 | + this.grid.endEdit(true); |
| 60 | + this.grid.transactions.undo(); |
| 61 | + } |
| 62 | + |
| 63 | + public redo() { |
| 64 | + /* exit edit mode and commit changes */ |
| 65 | + this.grid.endEdit(true); |
| 66 | + this.grid.transactions.redo(); |
| 67 | + } |
| 68 | + |
| 69 | + public futureDateValidator(): ValidatorFn { |
| 70 | + return (control: AbstractControl): ValidationErrors | null => { |
| 71 | + const date = control.value; |
| 72 | + if(date > new Date()){ |
| 73 | + return { futureDate: { value: control.value } }; |
| 74 | + } |
| 75 | + return null; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + public pastDateValidator(): ValidatorFn { |
| 80 | + return (control: AbstractControl): ValidationErrors | null => { |
| 81 | + const date = control.value; |
| 82 | + let pastDate = new Date('Nov 5 2010'); |
| 83 | + if(pastDate){ |
| 84 | + return pastDate < date ? null : { pastDate: { value: control.value } } |
| 85 | + } else return null; |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments