11import { Component , inject , resource , signal } from '@angular/core' ;
2- import { apply , applyEach , applyWhen , Field , customError , CustomValidationError , disabled , email , FieldTree , form , maxLength , min , minLength , pattern , required , schema , submit , validate , validateAsync , validateTree , ValidationError , WithField } from '@angular/forms/signals' ;
2+ import { apply , applyEach , applyWhen , Field , disabled , email , FieldTree , form , maxLength , min , minLength , pattern , required , schema , submit , validate , validateAsync , validateTree , ValidationErrorWithField } from '@angular/forms/signals' ;
33
44import { BackButton } from '../back-button/back-button' ;
55import { DebugOutput } from '../debug-output/debug-output' ;
@@ -51,10 +51,10 @@ export const formSchema = schema<RegisterFormData>((fieldPath) => {
5151 // Maps resource to error
5252 onSuccess : ( result ) => {
5353 return result
54- ? customError ( {
54+ ? {
5555 kind : 'userExists' ,
5656 message : 'The username you entered was already taken' ,
57- } )
57+ }
5858 : undefined ;
5959 } ,
6060 onError : ( ) => undefined
@@ -71,10 +71,10 @@ export const formSchema = schema<RegisterFormData>((fieldPath) => {
7171 // E-Mail validation
7272 validate ( fieldPath . email , ( ctx ) =>
7373 ! ctx . value ( ) . some ( ( e ) => e )
74- ? customError ( {
74+ ? {
7575 kind : 'atLeastOneEmail' ,
7676 message : 'At least one E-Mail address must be added' ,
77- } )
77+ }
7878 : undefined
7979 ) ;
8080 applyEach ( fieldPath . email , ( emailPath ) => {
@@ -97,11 +97,11 @@ export const formSchema = schema<RegisterFormData>((fieldPath) => {
9797 validateTree ( fieldPath . password , ( ctx ) => {
9898 return ctx . value ( ) . pw2 === ctx . value ( ) . pw1
9999 ? undefined
100- : customError ( {
100+ : {
101101 field : ctx . fieldOf ( fieldPath . password . pw2 ) , // assign the error to the second password field
102102 kind : 'confirmationPassword' ,
103103 message : 'The entered password must match with the one specified in "Password" field' ,
104- } ) ;
104+ } ;
105105 } ) ;
106106
107107 // Newsletter validation
@@ -111,10 +111,10 @@ export const formSchema = schema<RegisterFormData>((fieldPath) => {
111111 ( fieldPathWhenTrue ) => {
112112 validate ( fieldPathWhenTrue . newsletterTopics , ( ctx ) =>
113113 ! ctx . value ( ) . length
114- ? customError ( {
114+ ? {
115115 kind : 'noTopicSelected' ,
116116 message : 'Select at least one newsletter topic' ,
117- } )
117+ }
118118 : undefined
119119 ) ;
120120 }
@@ -158,23 +158,21 @@ export class RegistrationForm3 {
158158
159159 // validate when submitting and assign possible errors for matching field for showing in the UI
160160 await submit ( this . registrationForm , async ( form ) => {
161- const errors : WithField < CustomValidationError | ValidationError > [ ] = [ ] ;
161+ const errors : ValidationErrorWithField [ ] = [ ] ;
162162
163163 try {
164164 await this . #registrationService. registerUser ( form ( ) . value ) ;
165+ setTimeout ( ( ) => this . resetForm ( ) , 3000 ) ;
165166 } catch ( e ) {
166167 errors . push (
167- customError ( {
168+ {
168169 field : form ,
169- error : {
170- kind : 'serverError' ,
171- message : 'There was an server error, please try again (should work after 3rd try)' ,
172- } ,
173- } )
170+ kind : 'serverError' ,
171+ message : 'There was an server error, please try again (should work after 3rd try)' ,
172+ }
174173 ) ;
175174 }
176175
177- setTimeout ( ( ) => this . resetForm ( ) , 3000 ) ;
178176 return errors ;
179177 } ) ;
180178 }
0 commit comments