This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component({ | |
selector: 'app-modal', | |
template: ` | |
<button | |
[routerLink]="[]" | |
[queryParams]="{isOpen: true}"> | |
Abrir Modal | |
</button> | |
<nz-modal |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form [formGroup]="form"> | |
<input formControlName="username" /> | |
<span *ngIf="form.controls.username.pending">Estamos consultando este nome...</span> | |
<span *ngIf="form.controls.username.hasError('uniqueName')">Este nome já está sendo utilizado por outro usuário!</span> | |
</form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class FormComponent { | |
form: FormGroup; | |
constructor(private formBuilder: FormBuilder) { | |
this.form = this.formBuilder.group({ | |
username: ['', forbiddenNameValidator(/bob/i)], | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { AsyncValidatorFn, FormControl } from '@angular/forms'; | |
import { map } from 'rxjs/operators'; | |
export function uniqueNameValidator(userService: UserService): AsyncValidatorFn { | |
return (control: FormControl<string>) => { | |
return userService.getByName(control.value).pipe( | |
map((isTaken) => (isTaken ? { uniqueName: true } : null)), | |
); | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class FormComponent { | |
form: FormGroup; | |
constructor(private formBuilder: FormBuilder, userService: UserService) { | |
this.form = this.formBuilder.group({ | |
username: ['', forbiddenNameValidator(/bob/i), [uniqueUserValidator(userService)]], | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { AbstractControl, ValidationErrors, ValidatorFn } from '@angular/forms'; | |
export function forbiddenNameValidator(nameRe: RegExp): ValidatorFn { | |
return (control: AbstractControl): ValidationErrors | null => { | |
const forbidden = nameRe.test(control.value); | |
return forbidden ? { forbiddenName: { value: control.value } } : null; | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form [formGroup]="form"> | |
<input formControlName="username" /> | |
<span *ngIf="form.controls.username.hasError('required')">Este campo é obrigatório!</span> | |
</form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class FormComponent { | |
form: FormGroup; | |
constructor(private formBuilder: FormBuilder) { | |
this.form = this.formBuilder.group({ | |
username: ['', trimmedRequiredValidator], | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { FormControl, ValidationErrors } from '@angular/forms'; | |
export function trimmedRequiredValidator(control: FormControl<string>): ValidationErrors | null { | |
return !control.value || (control.value && control.value.trim() == '') ? { required: true } : null; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component({ | |
selector: 'app-form', | |
templateUrl: './form.component.html', | |
}) | |
export class FormComponent { | |
form: FormGroup; | |
constructor(private formBuilder: FormBuilder, userService: UserService) { | |
this.form = this.formBuilder.group({ | |
username: ['', Validators.required, [uniqueUserValidator(userService)]], | |
email: ['', [Validators.required, Validators.email]], |
NewerOlder