Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- this.registerForm.controls['email'].setErrors({ invalid: true, valid: false })
- export class RegistrationComponent implements OnInit {
- ...
- constructor(...) {}
- ngOnInit() {
- this.registerForm = this.fb.group({
- email: ['', [FormValidationService.emailValidator]],
- password: this.fb.group({
- pwd: ['', [FormValidationService.passwordValidator]],
- confirm: ['', [FormValidationService.passwordValidator]],
- }, {validator: FormValidationService.confirmValidator('pwd', 'confirm')}),
- username: ['', [FormValidationService.shortStringValidator]],
- })
- }
- register() {
- this.spinnerShow = !this.spinnerShow
- const regData = this.registerForm.value
- regData.confirm = this.registerForm.value.password.confirm
- regData.password = this.registerForm.value.password.pwd
- this.auth.registration(regData).subscribe(
- (res: any) => {
- if (res.error) {
- this.spinnerShow = !this.spinnerShow
- const messages = res.messages
- for (const key in messages) {
- if (messages.hasOwnProperty(key)) {
- if (key === 'both') {
- this.bothErrMsg = messages[key]
- this.registerForm.controls['email'].setErrors({ notUnique: true })
- } else {
- this.registerForm.controls[key].setErrors({ errMsg: messages[key] })
- }
- }
- }
- } else {
- this.lc.setItem('user', res.user)
- this.matDialogRef.close()
- }
- },
- error => {
- console.log(error) // TODO
- this.spinnerShow = !this.spinnerShow
- }
- )
- }
- }
- <div class="container">
- <h3>Register new user.</h3>
- <form [formGroup]="registerForm" novalidate>
- <mat-form-field>
- <input matInput formControlName="email" placeholder="Email" type="email" required>
- <mat-error *ngIf="registerForm.controls.email.hasError('errMsg') && registerForm.controls.email.touched">
- {{ registerForm.get('email').getError('errMsg') }}
- </mat-error>
- </mat-form-field>
- <div class="error" *ngIf="bothErrMsg !== undefined">{{ bothErrMsg }}</div>
- <button mat-raised-button color="primary" [disabled]="registerForm.invalid == true" (click)="register()">Sign Up</button>
- </form>
- </div>
- <form [formGroup]="registerForm">
- <mat-form-field>
- <input matInput placeholder="Email" formControlName="email">
- <mat-error *ngIf="email.dirty && email.hasError('notUnique')">This email is already taken!</mat-error>
- </mat-form-field>
- </form>
- public registerForm = new FormGroup({
- email: new FormControl(null, {
- validators: [Validators.required, Validators.email]
- })
- });
- public get email(): AbstractControl {
- return this.registerForm.get('email')!;
- }
- public register(): void {
- this.auth.registration(this.registerForm.value).subscribe((res) => {
- this.email.setErrors({ notUnique: true });
- });
- }
Add Comment
Please, Sign In to add comment