Created
October 9, 2025 14:10
-
-
Save Armenvardanyan95/22af047d5ed87eb48d52500de89bbddc 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
| export interface ProductSchema { | |
| title: string; | |
| description: string; | |
| price: number; | |
| } | |
| // define a validation schema | |
| export const productSchema = schema<ProductSchema>(ctx => { | |
| required(ctx.title); | |
| min(ctx.price, 0); | |
| }); | |
| // use it in a component | |
| @Component({ | |
| template: ` | |
| <div> | |
| <h2>Create Product</h2> | |
| <input [control]="form.title" placeholder="Title" /> | |
| <input [control]="form.description" placeholder="Description" /> | |
| <input type="number" [control]="form.price" placeholder="Price" /> | |
| @if (form.price().errors().at(0)?.kind === 'min') { | |
| <p>Price must be positive.</p> | |
| } | |
| </div> | |
| `, | |
| }) | |
| export class CreateProductComponent { | |
| product = signal<ProductSchema>({ | |
| title: '', | |
| description: '', | |
| price: 0, | |
| }); | |
| form = form(this.product, (ctx) => { | |
| apply(ctx, productSchema); | |
| }); | |
| } | |
| // then another component | |
| export class EditProductComponent { | |
| product = signal<ProductSchema>({ | |
| title: '', | |
| description: '', | |
| price: 0, | |
| }); | |
| form = form(this.product, (ctx) => { | |
| apply(ctx, productSchema); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment