Created
November 28, 2025 12:35
-
-
Save Armenvardanyan95/fabb2f8d63e1ecad5b5571bfd8d76949 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Component({ | |
| template: | |
| <button (click)="addItem()">Add Item</button> | |
| @for (item of form.items().value(); track $index) { | |
| <input [field]="form.items[$index]" /> | |
| <button (click)="removeItem($index)">Remove</button> | |
| } | |
| @for (error of form.items().errors(); track error) { | |
| @if (error.kind === 'noItems') { | |
| <div class="error-message">At least one item is required.</div> | |
| } | |
| } | |
| <button [disabled]="form().invalid()">Submit</button>, | |
| }) | |
| export class ItemsComponent { | |
| items = signal<{items: string[]}>({items: []}) | |
| form = form(this.items, path => { | |
| validate(path.items, items => { | |
| if ( | |
| items.value().some(item => item.trim() === '') || | |
| items.value().length === 0 | |
| ) { | |
| return {kind: 'noItems'}; | |
| } | |
| return null; | |
| }); | |
| }); | |
| addItem() { | |
| this.items.update(prev => ({items: [...prev.items, '']})); | |
| } | |
| removeItem(index: number) { | |
| this.items.update(prev => ({ | |
| items: prev.items.filter((_, i) => i !== index) | |
| })); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment