Options
All
  • Public
  • Public/Protected
  • All
Menu

Class FormModel<MODEL_T>

Wraps and holds the data and errors for it. Each instance has own validator.

Whenever you use makeFormStateWithModel they will initialize instance of this class and it will be passed to formState.

You can create form separately via makeFormModel and pass it e.g. in your own formState, or use it as nested model.

Generally FormData (holds data) - ModelValidator (validates it) - FormState (Controls logic)

example

form model via makeFormStateWithModel

const formState = makeFormStateWithModel({
initialData: {firstName: 'foo'},
validations: {
foo: (value) => isRequired(value)
}
})

const formModel = makeFormModel({
initialData: {firstName: 'foo'},
validations: {
foo: (value) => isRequired(value)
}
})

// formModel and formState.model will be functionally same (not referentially)
// makeFormStateWithModel intantiates model and passes it as model to formState.
example

usage makeFormModel

const formModel = makeFormModel({
initialData: {firstName: 'foo'},
validations: {
foo: (value) => isRequired(value)
}
})

formModel.validator.foo()
formModel.errors // {foo: ['required']}
formModel.foo = 'bar'
formModel.errors // {foo: ['required']} // it wa not yet revalidated with new value
formModel.validate()
formModel.errors // undefined
example

custom form model

//nothing stops you from creating custom form models, and it's even recommended. Its more type safe, and you can add
//methods:
class UserFormModel extends FormModel {
validator = new UserValidator(this) // you can create custom validators as well

name = 'foo'
email = 'baz'
// you can also modify data in constructor

get name() {
// write some behaviour funcs, etc.
}

}

Type parameters

  • MODEL_T = any

Hierarchy

  • FormModel

Index

Constructors

  • new FormModel<MODEL_T>(data: MODEL_T): FormModel<MODEL_T>

Properties

_general: any

property for holding general information / flag if needed, and for adding _general errors. important: this will be excluded during serialization

_uniqueReferenceKey?: number
errors?: Record<keyof MODEL_T, undefined | string[]>

holds an errors for properties, this value will be managed by validator. errors have following signature:

{
[propertyName]?: string[]
}

each property may have multiple errors, that's why errors are array for each property.

validator: ModelValidator<FormModel<MODEL_T> & MODEL_T> = ...

validator always accompanies model. Each model has own validator instance so referential integrity can be ensured.

uniqueKey: number = 0

{@see FormModel.getUniqueReferenceKey}

Methods

  • getUniqueReferenceKey(): number
  • for usage in components, when e.g. you have empty instance without any identifier

    example
    <Foo model={model} key={model.getUniqueKey()}>
    

    Returns number

  • toObject(options?: ModelToObjectOptions<this>): MODEL_T

Generated using TypeDoc