create-huenei-template 0.2.3 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-component/SKILL.md +288 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-component/references/component-patterns.md +358 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-di/SKILL.md +387 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-di/references/di-patterns.md +519 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-directives/SKILL.md +437 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-directives/references/directive-patterns.md +570 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-forms/SKILL.md +434 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-forms/references/form-patterns.md +398 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-http/SKILL.md +366 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-http/references/http-patterns.md +448 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-routing/SKILL.md +395 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-routing/references/routing-patterns.md +472 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-signals/SKILL.md +302 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-signals/references/signal-patterns.md +404 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-ssr/SKILL.md +435 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-ssr/references/ssr-patterns.md +497 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-testing/SKILL.md +660 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-testing/references/testing-patterns.md +707 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-tooling/SKILL.md +352 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-tooling/references/tooling-patterns.md +448 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-accessibility/SKILL.md +780 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-components/SKILL.md +718 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-design-systems/SKILL.md +612 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-layouts/SKILL.md +594 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-animations/SKILL.md +432 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-animations/references/animation-patterns.md +427 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-debugging/SKILL.md +354 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-debugging/references/troubleshooting-guide.md +306 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-framework-integration/SKILL.md +578 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-framework-integration/references/react-patterns.md +353 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/SKILL.md +608 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/references/advanced-theme-patterns.md +215 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/references/custom-utilities-guide.md +281 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-mobile-first/SKILL.md +768 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-performance/SKILL.md +374 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-performance/references/bundle-optimization.md +244 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-plugins/SKILL.md +505 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-responsive-darkmode/SKILL.md +601 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-responsive-darkmode/references/advanced-responsive-patterns.md +485 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/web-design-guidelines/SKILL.md +39 -0
- package/dist/templates/frontend/angular-base/package.json +0 -1
- package/dist/templates/frontend/angular-base/pnpm-lock.yaml +8183 -0
- package/package.json +1 -1
- package/templates/frontend/angular-base/.cursor/skills/angular-component/SKILL.md +288 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-component/references/component-patterns.md +358 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-di/SKILL.md +387 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-di/references/di-patterns.md +519 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-directives/SKILL.md +437 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-directives/references/directive-patterns.md +570 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-forms/SKILL.md +434 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-forms/references/form-patterns.md +398 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-http/SKILL.md +366 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-http/references/http-patterns.md +448 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-routing/SKILL.md +395 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-routing/references/routing-patterns.md +472 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-signals/SKILL.md +302 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-signals/references/signal-patterns.md +404 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-ssr/SKILL.md +435 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-ssr/references/ssr-patterns.md +497 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-testing/SKILL.md +660 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-testing/references/testing-patterns.md +707 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-tooling/SKILL.md +352 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-tooling/references/tooling-patterns.md +448 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-accessibility/SKILL.md +780 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-components/SKILL.md +718 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-design-systems/SKILL.md +612 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-layouts/SKILL.md +594 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-animations/SKILL.md +432 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-animations/references/animation-patterns.md +427 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-debugging/SKILL.md +354 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-debugging/references/troubleshooting-guide.md +306 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-framework-integration/SKILL.md +578 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-framework-integration/references/react-patterns.md +353 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/SKILL.md +608 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/references/advanced-theme-patterns.md +215 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/references/custom-utilities-guide.md +281 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-mobile-first/SKILL.md +768 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-performance/SKILL.md +374 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-performance/references/bundle-optimization.md +244 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-plugins/SKILL.md +505 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-responsive-darkmode/SKILL.md +601 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-responsive-darkmode/references/advanced-responsive-patterns.md +485 -0
- package/templates/frontend/angular-base/.cursor/skills/web-design-guidelines/SKILL.md +39 -0
- package/templates/frontend/angular-base/package.json +0 -1
- package/templates/frontend/angular-base/pnpm-lock.yaml +8183 -0
- package/templates/frontend/angular-base/package-lock.json +0 -13321
|
@@ -0,0 +1,434 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: angular-forms
|
|
3
|
+
description: Build signal-based forms in Angular v21+ using the new Signal Forms API. Use for form creation with automatic two-way binding, schema-based validation, field state management, and dynamic forms. Triggers on form implementation, adding validation, creating multi-step forms, or building forms with conditional fields. Signal Forms are experimental but recommended for new Angular projects.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Angular Signal Forms
|
|
7
|
+
|
|
8
|
+
Build type-safe, reactive forms using Angular's Signal Forms API. Signal Forms provide automatic two-way binding, schema-based validation, and reactive field state.
|
|
9
|
+
|
|
10
|
+
**Note:** Signal Forms are experimental in Angular v21. For production apps requiring stability, see [references/form-patterns.md](references/form-patterns.md) for Reactive Forms patterns.
|
|
11
|
+
|
|
12
|
+
## Basic Setup
|
|
13
|
+
|
|
14
|
+
```typescript
|
|
15
|
+
import { Component, signal } from '@angular/core';
|
|
16
|
+
import { form, FormField, required, email } from '@angular/forms/signals';
|
|
17
|
+
|
|
18
|
+
interface LoginData {
|
|
19
|
+
email: string;
|
|
20
|
+
password: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
@Component({
|
|
24
|
+
selector: 'app-login',
|
|
25
|
+
imports: [FormField],
|
|
26
|
+
template: `
|
|
27
|
+
<form (submit)="onSubmit($event)">
|
|
28
|
+
<label>
|
|
29
|
+
Email
|
|
30
|
+
<input type="email" [formField]="loginForm.email" />
|
|
31
|
+
</label>
|
|
32
|
+
@if (loginForm.email().touched() && loginForm.email().invalid()) {
|
|
33
|
+
<p class="error">{{ loginForm.email().errors()[0].message }}</p>
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
<label>
|
|
37
|
+
Password
|
|
38
|
+
<input type="password" [formField]="loginForm.password" />
|
|
39
|
+
</label>
|
|
40
|
+
@if (loginForm.password().touched() && loginForm.password().invalid()) {
|
|
41
|
+
<p class="error">{{ loginForm.password().errors()[0].message }}</p>
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
<button type="submit" [disabled]="loginForm().invalid()">Login</button>
|
|
45
|
+
</form>
|
|
46
|
+
`,
|
|
47
|
+
})
|
|
48
|
+
export class Login {
|
|
49
|
+
// Form model - a writable signal
|
|
50
|
+
loginModel = signal<LoginData>({
|
|
51
|
+
email: '',
|
|
52
|
+
password: '',
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// Create form with validation schema
|
|
56
|
+
loginForm = form(this.loginModel, (schemaPath) => {
|
|
57
|
+
required(schemaPath.email, { message: 'Email is required' });
|
|
58
|
+
email(schemaPath.email, { message: 'Enter a valid email address' });
|
|
59
|
+
required(schemaPath.password, { message: 'Password is required' });
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
onSubmit(event: Event) {
|
|
63
|
+
event.preventDefault();
|
|
64
|
+
if (this.loginForm().valid()) {
|
|
65
|
+
const credentials = this.loginModel();
|
|
66
|
+
console.log('Submitting:', credentials);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Form Models
|
|
73
|
+
|
|
74
|
+
Form models are writable signals that serve as the single source of truth:
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
// Define interface for type safety
|
|
78
|
+
interface UserProfile {
|
|
79
|
+
name: string;
|
|
80
|
+
email: string;
|
|
81
|
+
age: number | null;
|
|
82
|
+
preferences: {
|
|
83
|
+
newsletter: boolean;
|
|
84
|
+
theme: 'light' | 'dark';
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Create model signal with initial values
|
|
89
|
+
const userModel = signal<UserProfile>({
|
|
90
|
+
name: '',
|
|
91
|
+
email: '',
|
|
92
|
+
age: null,
|
|
93
|
+
preferences: {
|
|
94
|
+
newsletter: false,
|
|
95
|
+
theme: 'light',
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
// Create form from model
|
|
100
|
+
const userForm = form(userModel);
|
|
101
|
+
|
|
102
|
+
// Access nested fields via dot notation
|
|
103
|
+
userForm.name // FieldTree<string>
|
|
104
|
+
userForm.preferences.theme // FieldTree<'light' | 'dark'>
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Reading Values
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
// Read entire model
|
|
111
|
+
const data = this.userModel();
|
|
112
|
+
|
|
113
|
+
// Read field value via field state
|
|
114
|
+
const name = this.userForm.name().value();
|
|
115
|
+
const theme = this.userForm.preferences.theme().value();
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Updating Values
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
// Replace entire model
|
|
122
|
+
this.userModel.set({
|
|
123
|
+
name: 'Alice',
|
|
124
|
+
email: 'alice@example.com',
|
|
125
|
+
age: 30,
|
|
126
|
+
preferences: { newsletter: true, theme: 'dark' },
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
// Update single field
|
|
130
|
+
this.userForm.name().value.set('Bob');
|
|
131
|
+
this.userForm.age().value.update(age => (age ?? 0) + 1);
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## Field State
|
|
135
|
+
|
|
136
|
+
Each field provides reactive signals for validation, interaction, and availability:
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
const emailField = this.form.email();
|
|
140
|
+
|
|
141
|
+
// Validation state
|
|
142
|
+
emailField.valid() // true if passes all validation
|
|
143
|
+
emailField.invalid() // true if has validation errors
|
|
144
|
+
emailField.errors() // array of error objects
|
|
145
|
+
emailField.pending() // true if async validation in progress
|
|
146
|
+
|
|
147
|
+
// Interaction state
|
|
148
|
+
emailField.touched() // true after focus + blur
|
|
149
|
+
emailField.dirty() // true after user modification
|
|
150
|
+
|
|
151
|
+
// Availability state
|
|
152
|
+
emailField.disabled() // true if field is disabled
|
|
153
|
+
emailField.hidden() // true if field should be hidden
|
|
154
|
+
emailField.readonly() // true if field is readonly
|
|
155
|
+
|
|
156
|
+
// Value
|
|
157
|
+
emailField.value() // current field value (signal)
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### Form-Level State
|
|
161
|
+
|
|
162
|
+
The form itself is also a field with aggregated state:
|
|
163
|
+
|
|
164
|
+
```typescript
|
|
165
|
+
// Form is valid when all interactive fields are valid
|
|
166
|
+
this.form().valid()
|
|
167
|
+
|
|
168
|
+
// Form is touched when any field is touched
|
|
169
|
+
this.form().touched()
|
|
170
|
+
|
|
171
|
+
// Form is dirty when any field is modified
|
|
172
|
+
this.form().dirty()
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
## Validation
|
|
176
|
+
|
|
177
|
+
### Built-in Validators
|
|
178
|
+
|
|
179
|
+
```typescript
|
|
180
|
+
import {
|
|
181
|
+
form, required, email, min, max,
|
|
182
|
+
minLength, maxLength, pattern
|
|
183
|
+
} from '@angular/forms/signals';
|
|
184
|
+
|
|
185
|
+
const userForm = form(this.userModel, (schemaPath) => {
|
|
186
|
+
// Required field
|
|
187
|
+
required(schemaPath.name, { message: 'Name is required' });
|
|
188
|
+
|
|
189
|
+
// Email format
|
|
190
|
+
email(schemaPath.email, { message: 'Invalid email' });
|
|
191
|
+
|
|
192
|
+
// Numeric range
|
|
193
|
+
min(schemaPath.age, 18, { message: 'Must be 18+' });
|
|
194
|
+
max(schemaPath.age, 120, { message: 'Invalid age' });
|
|
195
|
+
|
|
196
|
+
// String/array length
|
|
197
|
+
minLength(schemaPath.password, 8, { message: 'Min 8 characters' });
|
|
198
|
+
maxLength(schemaPath.bio, 500, { message: 'Max 500 characters' });
|
|
199
|
+
|
|
200
|
+
// Regex pattern
|
|
201
|
+
pattern(schemaPath.phone, /^\d{3}-\d{3}-\d{4}$/, {
|
|
202
|
+
message: 'Format: 555-123-4567',
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
### Conditional Validation
|
|
208
|
+
|
|
209
|
+
```typescript
|
|
210
|
+
const orderForm = form(this.orderModel, (schemaPath) => {
|
|
211
|
+
required(schemaPath.promoCode, {
|
|
212
|
+
message: 'Promo code required for discounts',
|
|
213
|
+
when: ({ valueOf }) => valueOf(schemaPath.applyDiscount),
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
### Custom Validators
|
|
219
|
+
|
|
220
|
+
```typescript
|
|
221
|
+
import { validate } from '@angular/forms/signals';
|
|
222
|
+
|
|
223
|
+
const signupForm = form(this.signupModel, (schemaPath) => {
|
|
224
|
+
// Custom validation logic
|
|
225
|
+
validate(schemaPath.username, ({ value }) => {
|
|
226
|
+
if (value().includes(' ')) {
|
|
227
|
+
return { kind: 'noSpaces', message: 'Username cannot contain spaces' };
|
|
228
|
+
}
|
|
229
|
+
return null;
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
### Cross-Field Validation
|
|
235
|
+
|
|
236
|
+
```typescript
|
|
237
|
+
const passwordForm = form(this.passwordModel, (schemaPath) => {
|
|
238
|
+
required(schemaPath.password);
|
|
239
|
+
required(schemaPath.confirmPassword);
|
|
240
|
+
|
|
241
|
+
// Compare fields
|
|
242
|
+
validate(schemaPath.confirmPassword, ({ value, valueOf }) => {
|
|
243
|
+
if (value() !== valueOf(schemaPath.password)) {
|
|
244
|
+
return { kind: 'mismatch', message: 'Passwords do not match' };
|
|
245
|
+
}
|
|
246
|
+
return null;
|
|
247
|
+
});
|
|
248
|
+
});
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
### Async Validation
|
|
252
|
+
|
|
253
|
+
```typescript
|
|
254
|
+
import { validateHttp } from '@angular/forms/signals';
|
|
255
|
+
|
|
256
|
+
const signupForm = form(this.signupModel, (schemaPath) => {
|
|
257
|
+
validateHttp(schemaPath.username, {
|
|
258
|
+
request: ({ value }) => `/api/check-username?u=${value()}`,
|
|
259
|
+
onSuccess: (response: { taken: boolean }) => {
|
|
260
|
+
if (response.taken) {
|
|
261
|
+
return { kind: 'taken', message: 'Username already taken' };
|
|
262
|
+
}
|
|
263
|
+
return null;
|
|
264
|
+
},
|
|
265
|
+
onError: () => ({
|
|
266
|
+
kind: 'networkError',
|
|
267
|
+
message: 'Could not verify username',
|
|
268
|
+
}),
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
## Conditional Fields
|
|
274
|
+
|
|
275
|
+
### Hidden Fields
|
|
276
|
+
|
|
277
|
+
```typescript
|
|
278
|
+
import { hidden } from '@angular/forms/signals';
|
|
279
|
+
|
|
280
|
+
const profileForm = form(this.profileModel, (schemaPath) => {
|
|
281
|
+
hidden(schemaPath.publicUrl, ({ valueOf }) => !valueOf(schemaPath.isPublic));
|
|
282
|
+
});
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
```html
|
|
286
|
+
@if (!profileForm.publicUrl().hidden()) {
|
|
287
|
+
<input [formField]="profileForm.publicUrl" />
|
|
288
|
+
}
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
### Disabled Fields
|
|
292
|
+
|
|
293
|
+
```typescript
|
|
294
|
+
import { disabled } from '@angular/forms/signals';
|
|
295
|
+
|
|
296
|
+
const orderForm = form(this.orderModel, (schemaPath) => {
|
|
297
|
+
disabled(schemaPath.couponCode, ({ valueOf }) => valueOf(schemaPath.total) < 50);
|
|
298
|
+
});
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
### Readonly Fields
|
|
302
|
+
|
|
303
|
+
```typescript
|
|
304
|
+
import { readonly } from '@angular/forms/signals';
|
|
305
|
+
|
|
306
|
+
const accountForm = form(this.accountModel, (schemaPath) => {
|
|
307
|
+
readonly(schemaPath.username); // Always readonly
|
|
308
|
+
});
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
## Form Submission
|
|
312
|
+
|
|
313
|
+
```typescript
|
|
314
|
+
import { submit } from '@angular/forms/signals';
|
|
315
|
+
|
|
316
|
+
@Component({
|
|
317
|
+
template: `
|
|
318
|
+
<form (submit)="onSubmit($event)">
|
|
319
|
+
<input [formField]="form.email" />
|
|
320
|
+
<input [formField]="form.password" />
|
|
321
|
+
<button type="submit" [disabled]="form().invalid()">Submit</button>
|
|
322
|
+
</form>
|
|
323
|
+
`,
|
|
324
|
+
})
|
|
325
|
+
export class Login {
|
|
326
|
+
model = signal({ email: '', password: '' });
|
|
327
|
+
form = form(this.model, (schemaPath) => {
|
|
328
|
+
required(schemaPath.email);
|
|
329
|
+
required(schemaPath.password);
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
onSubmit(event: Event) {
|
|
333
|
+
event.preventDefault();
|
|
334
|
+
|
|
335
|
+
// submit() marks all fields touched and runs callback if valid
|
|
336
|
+
submit(this.form, async () => {
|
|
337
|
+
await this.authService.login(this.model());
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
## Arrays and Dynamic Fields
|
|
344
|
+
|
|
345
|
+
```typescript
|
|
346
|
+
interface Order {
|
|
347
|
+
items: Array<{ product: string; quantity: number }>;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
@Component({
|
|
351
|
+
template: `
|
|
352
|
+
@for (item of orderForm.items; track $index; let i = $index) {
|
|
353
|
+
<div>
|
|
354
|
+
<input [formField]="item.product" placeholder="Product" />
|
|
355
|
+
<input [formField]="item.quantity" type="number" />
|
|
356
|
+
<button type="button" (click)="removeItem(i)">Remove</button>
|
|
357
|
+
</div>
|
|
358
|
+
}
|
|
359
|
+
<button type="button" (click)="addItem()">Add Item</button>
|
|
360
|
+
`,
|
|
361
|
+
})
|
|
362
|
+
export class Order {
|
|
363
|
+
orderModel = signal<Order>({
|
|
364
|
+
items: [{ product: '', quantity: 1 }],
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
orderForm = form(this.orderModel, (schemaPath) => {
|
|
368
|
+
applyEach(schemaPath.items, (item) => {
|
|
369
|
+
required(item.product, { message: 'Product required' });
|
|
370
|
+
min(item.quantity, 1, { message: 'Min quantity is 1' });
|
|
371
|
+
});
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
addItem() {
|
|
375
|
+
this.orderModel.update(m => ({
|
|
376
|
+
...m,
|
|
377
|
+
items: [...m.items, { product: '', quantity: 1 }],
|
|
378
|
+
}));
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
removeItem(index: number) {
|
|
382
|
+
this.orderModel.update(m => ({
|
|
383
|
+
...m,
|
|
384
|
+
items: m.items.filter((_, i) => i !== index),
|
|
385
|
+
}));
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
## Displaying Errors
|
|
391
|
+
|
|
392
|
+
```html
|
|
393
|
+
<input [formField]="form.email" />
|
|
394
|
+
|
|
395
|
+
@if (form.email().touched() && form.email().invalid()) {
|
|
396
|
+
<ul class="errors">
|
|
397
|
+
@for (error of form.email().errors(); track error) {
|
|
398
|
+
<li>{{ error.message }}</li>
|
|
399
|
+
}
|
|
400
|
+
</ul>
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
@if (form.email().pending()) {
|
|
404
|
+
<span>Validating...</span>
|
|
405
|
+
}
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
## Styling Based on State
|
|
409
|
+
|
|
410
|
+
```html
|
|
411
|
+
<input
|
|
412
|
+
[formField]="form.email"
|
|
413
|
+
[class.is-invalid]="form.email().touched() && form.email().invalid()"
|
|
414
|
+
[class.is-valid]="form.email().touched() && form.email().valid()"
|
|
415
|
+
/>
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
## Reset Form
|
|
419
|
+
|
|
420
|
+
```typescript
|
|
421
|
+
async onSubmit() {
|
|
422
|
+
if (!this.form().valid()) return;
|
|
423
|
+
|
|
424
|
+
await this.api.submit(this.model());
|
|
425
|
+
|
|
426
|
+
// Clear interaction state
|
|
427
|
+
this.form().reset();
|
|
428
|
+
|
|
429
|
+
// Clear values
|
|
430
|
+
this.model.set({ email: '', password: '' });
|
|
431
|
+
}
|
|
432
|
+
```
|
|
433
|
+
|
|
434
|
+
For Reactive Forms patterns (production-stable), see [references/form-patterns.md](references/form-patterns.md).
|