-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsignal-form.ts
More file actions
61 lines (50 loc) · 1.61 KB
/
Copy pathsignal-form.ts
File metadata and controls
61 lines (50 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { JsonPipe } from '@angular/common';
import { Component, signal } from '@angular/core';
import { disabled, email, Field, form, minLength, required, submit } from '@angular/forms/signals';
import { InputField } from './input-field/input-field';
@Component({
selector: 'app-signal-form',
imports: [JsonPipe, Field, InputField],
templateUrl: './signal-form.html',
})
export class SignalForm {
protected formModel = signal({
email: '',
password: '',
location: {
name: '',
address: '',
},
});
protected form = form(this.formModel, (path) => {
required(path.email, { message: 'Feld required' });
email(path.email, { message: 'Invalid email' });
required(path.password, { message: 'Feld required' });
minLength(path.password, 5, { message: 'Must contain at least 5 characters' });
required(path.location.name, { message: 'Feld required' });
disabled(path, () => this.form().submitting());
});
protected hasServerError = signal(false);
protected toggleServerError() {
this.hasServerError.update((value) => !value);
}
protected register(event: SubmitEvent) {
event.preventDefault();
submit(this.form, async () => {
return new Promise((resolve) => {
setTimeout(() => {
console.log('Form submitted', this.form().value());
resolve(
this.hasServerError()
? {
kind: 'server',
message: 'This email address already exists',
fieldTree: this.form.email,
}
: null,
);
}, 2000);
});
});
}
}