import 'reflect-metadata'; import type { BaseComponent } from './base_component.js'; import { type Decorator } from './features/support_decorators/decorator.js'; import { type InferValidationReturnType } from './features/support_validation/types.js'; import { type Component } from './component.js'; import { type HttpContext } from '@adonisjs/core/http'; import { type ApplicationService } from '@adonisjs/core/types'; /** * Error bag for storing validation errors * Key: field name, Value: array of error messages */ export type ErrorBag = Record; /** * Symbols for Form internal state * Using symbols instead of private fields because Proxy changes `this` context * and private fields only work with the original instance */ declare const FORM_COMPONENT: unique symbol; declare const FORM_PROPERTY_NAME: unique symbol; declare const FORM_INITIAL_VALUES: unique symbol; declare const FORM_INITIAL_VALUES_STORED: unique symbol; /** * Form base class for Form Objects * PHP parity: Livewire\Form * * Form Objects encapsulate form properties and lifecycle hooks. * Uses Proxy to delegate validation to the parent Component. * * Uses the same @validate decorator as Component. * * @example * ```typescript * import { Form, validate } from 'adonisjs-livewire' * import vine from '@vinejs/vine' * * export class PostForm extends Form { * @validate(() => vine.string().minLength(3)) * declare title: HasValidate * * @validate(() => vine.string().minLength(10)) * declare content: HasValidate * } * ``` */ export declare abstract class Form { #private; /** * Reference to the parent component * Set during initialization by SupportFormObjects */ [FORM_COMPONENT]: BaseComponent | undefined; /** * Property name on the component * Set during initialization by SupportFormObjects */ [FORM_PROPERTY_NAME]: string | undefined; /** * Initial property values for reset functionality */ [FORM_INITIAL_VALUES]: Record; /** * Track if initial values have been stored (to avoid re-capturing on hydrate) */ [FORM_INITIAL_VALUES_STORED]: boolean; constructor(); /** * Get the HTTP context from the parent component * Delegates to component.ctx for session, logger, etc. */ get ctx(): HttpContext; /** * Get the application instance from the parent component */ get app(): ApplicationService; /** * Get all decorators from prototype chain * Used by @validate decorator */ getDecorators(): Decorator[]; /** * Add a decorator - called by @validate */ addDecorator(decorator: Decorator): void; /** * Boot the form object (called once per class) */ boot?(): void | Promise; /** * Mount the form object (called when component mounts) */ mount?(): void | Promise; /** * Called before form is dehydrated for client */ dehydrate?(): void | Promise; /** * Called after form is hydrated from client */ hydrate?(): void | Promise; /** * Called before a property is updated */ updating?(property: string, value: any): void | boolean | Promise; /** * Called after a property is updated */ updated?(property: string, value: any): void | Promise; /** * Set the component reference and return a proxied Form */ setComponent(component: BaseComponent, propertyName: string): Form; /** * Get the parent component */ getComponent(): BaseComponent | undefined; /** * Get the property name on the component */ getPropertyName(): string | undefined; /** * Get all form property names * Uses decorators and own properties */ getPropertyNames(): string[]; /** * Check if form has a property */ hasProperty(name: string): boolean; /** * Get a property value */ getPropertyValue(name: string): any; /** * Set a property value */ setPropertyValue(name: string, value: any): void; /** * Validate form - delegates to component's validation * Errors are stored in component's error bag with "formName.field" keys * * Pass `normalize: true` to run the validated result through * {@link normalizeData} before returning it — coercing empty strings to * `null`, which is what a nullable DB column usually wants from an * untouched text input. */ validate(options?: { normalize?: boolean; }): Promise>; /** * Normalize form data by coercing empty strings ("") to null */ normalizeData>(data: T): T; /** * Get errors for form fields from component's error bag */ getErrorBag(): ErrorBag; /** * Reset error bag for form fields */ resetErrorBag(fields?: string | string[]): void; /** * Add error for a form field */ addError(field: string, message: string): void; /** * Check if a field has errors */ hasError(field: string): boolean; /** * Get errors for a specific field */ getError(field: string): string[]; /** * Get all form data */ all(): InferValidationReturnType; /** * Get only specified fields */ only>(keys: K[]): Pick, K>; only(...keys: (string | string[])[]): Record; /** * Get all fields except specified ones */ except(keys: string[]): Record; except(...keys: (string | string[])[]): Record; /** * Fill form with data */ fill(data: Record): this; /** * Reset form to initial values */ reset(...fields: (string | string[])[]): this; /** * Reset all fields except specified ones */ resetExcept(...keys: (string | string[])[]): this; /** * Get value and reset field */ pull(key: string): any; /** * Convert form to array */ toArray(): Record; /** * Register form class for hydration */ static register(constructor: new () => Form): void; } export interface Form extends Component { } export {};