Skip to content

Commit 4e2c589

Browse files
fmalcherd-koppenhagen
authored andcommitted
feat: remove customError() and directly return error object
1 parent e449beb commit 4e2c589

File tree

3 files changed

+30
-34
lines changed

3 files changed

+30
-34
lines changed

src/app/registration-form-1/registration-form-1.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Component, inject, signal } from '@angular/core';
2-
import { Field, FieldState, form, maxLength, min, minLength, pattern, required, schema, submit } from '@angular/forms/signals';
2+
import { Field, FieldState, form, maxLength, min, minLength, required, schema, submit } from '@angular/forms/signals';
33

44
import { FormError } from '../form-error/form-error';
55
import { RegistrationService } from '../registration-service';

src/app/registration-form-2/registration-form-2.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Component, inject, resource, signal } from '@angular/core';
2-
import { 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 { applyEach, applyWhen, Field, disabled, email, FieldTree, form, maxLength, min, minLength, pattern, required, schema, submit, validate, validateAsync, validateTree, ValidationErrorWithField } from '@angular/forms/signals';
33

44
import { BackButton } from '../back-button/back-button';
55
import { DebugOutput } from '../debug-output/debug-output';
@@ -47,10 +47,10 @@ export const formSchema = schema<RegisterFormData>((fieldPath) => {
4747
// Maps resource to error
4848
onSuccess: (result) => {
4949
return result
50-
? customError({
50+
? {
5151
kind: 'userExists',
5252
message: 'The username you entered was already taken',
53-
})
53+
}
5454
: undefined;
5555
},
5656
onError: () => undefined
@@ -67,10 +67,10 @@ export const formSchema = schema<RegisterFormData>((fieldPath) => {
6767
// E-Mail validation
6868
validate(fieldPath.email, (ctx) =>
6969
!ctx.value().some((e) => e)
70-
? customError({
70+
? {
7171
kind: 'atLeastOneEmail',
7272
message: 'At least one E-Mail address must be added',
73-
})
73+
}
7474
: undefined
7575
);
7676
applyEach(fieldPath.email, (emailPath) => {
@@ -93,11 +93,11 @@ export const formSchema = schema<RegisterFormData>((fieldPath) => {
9393
validateTree(fieldPath.password, (ctx) => {
9494
return ctx.value().pw2 === ctx.value().pw1
9595
? undefined
96-
: customError({
96+
: {
9797
field: ctx.fieldOf(fieldPath.password.pw2), // assign the error to the second password field
9898
kind: 'confirmationPassword',
9999
message: 'The entered password must match with the one specified in "Password" field',
100-
});
100+
};
101101
});
102102

103103
// Newsletter validation
@@ -107,10 +107,10 @@ export const formSchema = schema<RegisterFormData>((fieldPath) => {
107107
(fieldPathWhenTrue) => {
108108
validate(fieldPathWhenTrue.newsletterTopics, (ctx) =>
109109
!ctx.value().length
110-
? customError({
110+
? {
111111
kind: 'noTopicSelected',
112112
message: 'Select at least one newsletter topic',
113-
})
113+
}
114114
: undefined
115115
);
116116
}
@@ -151,19 +151,17 @@ export class RegistrationForm2 {
151151

152152
// validate when submitting and assign possible errors for matching field for showing in the UI
153153
await submit(this.registrationForm, async (form) => {
154-
const errors: WithField<CustomValidationError | ValidationError>[] = [];
154+
const errors: ValidationErrorWithField[] = [];
155155

156156
try {
157157
await this.#registrationService.registerUser(form().value);
158158
} catch (e) {
159159
errors.push(
160-
customError({
160+
{
161161
field: form,
162-
error: {
163-
kind: 'serverError',
164-
message: 'There was an server error, please try again (should work after 3rd try)',
165-
},
166-
})
162+
kind: 'serverError',
163+
message: 'There was an server error, please try again (should work after 3rd try)',
164+
}
167165
);
168166
}
169167

src/app/registration-form-3/registration-form-3.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { 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

44
import { BackButton } from '../back-button/back-button';
55
import { 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

Comments
 (0)