ListField

Source
import { ListField } from "@prestojs/viewmodel";

A container field for an array of values

You must pass childField which is the underlying field for each value in the list. ListField will call format, parse, and normalize on this field for each value in the list.

By default defaultValue will be set to an empty array unless blankAsNull is true in which case it will be set to null. normalize and parse also behave in the same way (a falsy value passed to these will either return an empty array when blankAsNull is false or null when it is true).

const listOfInts = new ListField({ childField: new IntegerField() });

API

new ListField(props)

Source
ParameterTypeDescription
*props.blankAsNullundefined|false|true
*props.childFieldField
*props.defaultValuenull|T[]|
Function
*props.restrest

Methods

Calls childField.format on each entry in the passed array

ParameterTypeDescription
*valueT[]
any
ParameterTypeDescription
*value1T[]
*value2T[]
boolean

Calls childField.parse on each entry in the passed array

If value is falsy or an empty array and blankAsNull is true it will return null or if blankAsNull is false then it will return an empty array.

ParameterTypeDescription
*valueany

One of the following:

T[]

OR

null

Calls childField.parse on each entry in the passed array

If value is falsy or an empty array and blankAsNull is true it will return null or if blankAsNull is false then it will return an empty array.

ParameterTypeDescription
*valueParsableType[]|null

One of the following:

T[]

OR

null

Properties

Static Properties

Inherited Methods

Returns a clone of the field that should be functionally equivalent

Field

Called once after fields are attached to a ViewModel. This occurs the first time .fields is accessed on the ViewModel.

By default this does nothing but can be used by fields to attach extra properties or validate against the final view model (for example checking that another field does / does not exist).

NOTE: This is called for every distinct ViewModel class; so if class A is extended by class B then it will be called on both A and B.

ParameterTypeDescription
*viewModelViewModel Class
void

Convert value to plain JS representation useful for things like passing to a form or posting to a backend API

ParameterTypeDescription
*valueT[]

One of the following:

string

OR

number

OR

null

OR

__type
string

Inherited Properties

Async choices for this field.

Is this field required when saving a record?

If true an empty string value should be converted to a null value

When accessed on a bound field will return the current instance of the ViewModel the field is bound to.

If called on an unbound field then this will always be undefined and a warning will be raised.

Get the default value for this field.

Help text that can be displayed with the form widget

Returns true if field is bound to a ViewModel instance. When a field is bound to a instance the value for that field is accessible on the 'value' property.

Label that can be displayed as the form label for a widget

If not specified will be generated from name.

The ViewModel class this field is attached to.

This will throw an error if the field is not attached to a model.

The name of this field.

This will throw an error if the field is not attached to a model.

Indicates this field should only be read, not written. Not enforced but can be used by components to adjust their output accordingly (eg. exclude it from a form or show it on a form with a read only input)

When isBound is true this will return the current value of this field on the bound ViewModel. Otherwise will always be undefined.

Indicates this field should only be written only and is not intended to be read directly. This is not enforced but can be used by components to adjust their output accordingly (eg. exclude it from a detail view on a record)