Skip to content

Instantly share code, notes, and snippets.

@Armenvardanyan95
Created November 28, 2025 12:35
Show Gist options
  • Select an option

  • Save Armenvardanyan95/fabb2f8d63e1ecad5b5571bfd8d76949 to your computer and use it in GitHub Desktop.

Select an option

Save Armenvardanyan95/fabb2f8d63e1ecad5b5571bfd8d76949 to your computer and use it in GitHub Desktop.
@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