formsField()
This function is used to create an array of forms. This function returns FormsField object.
Parameters
The formsField() takes 4 parameters:
typescript
declare function formsField<T extends (arg: any) => Form>(
generateForm: T,
initialValueListOrLength: InitialValueListOrLength<T> = [],
schema?: FormsFieldSchema<ReturnType<T>>,
validateOptions?: ValidateOptions
): FormsField<T>;1. generateForm Required
- Pass a function that returns a form with 0 or 1 parameter.
- A form should be defined using
defineForm().
- A form should be defined using
2. initialValueListOrLength Optional / Default: []
- Pass an array to initialize forms using
generateForm.- Each element of the passed array is used as the parameter of
generateForm.
- Each element of the passed array is used as the parameter of
- If the parameter of
generateFormis optional, you can also pass an integer greater than or equal to 0 instead of an array. []and0have equivalent meaning.
3. schema Optional
- Pass a yup array schema or a function that returns a yup array schema.
- If you pass a function, the first argument is a type bound
yup.ArraySchemaobject.- The same type as
$formsis available in thetest()method ofyup.ArraySchema.
- The same type as
- If you pass a function, you can refer to
$valueof other fields.
- If you pass a function, the first argument is a type bound
- For example, use to check that the length of
$formsis greater than 0.
4. validateOptions Optional
- Pass
{ abortEarly: false }if all validation errors by the yup array schema are required.
Details of FormsField object
$forms property Readonly
- Returns the array of forms generated by
generateForm.- For use with
v-for, each generated form is given a unique key named$key.
- For use with
$error property Readonly
- Returns a result of validating
$formsreactively with the yup schema.- If
$formsis invalid,yup.ValidationErroris returned. - If
$formsis valid,undefinedis returned.
- If
$errorMessages property Readonly
- Returns an error messages array (
string[]) reactively as well as$errorproperty.- If
abortEarlyis false, it can return more than one element.
- If
$label property Readonly
- Returns the label of the yup schema.
- For example, if you pass
yup.array().label("Questions"), you will obtain"Questions". - If the
schemayou passed is a function, the function is evaluated once when the object is created.
- For example, if you pass
$initialize() method
- Pass an array to initialize forms using
generateForm.- The parameter of this method is the same as the second parameter of
formsField().
- The parameter of this method is the same as the second parameter of
- If the argument is omitted, all array elements will be removed.
$append() method
- Pass a parameter to initialize a form using
generateForm, and the result is appended to$forms. - If the parameter of
generateFormis optional, you can omit the argument.
$prepend() method
- Pass a parameter to initialize a form using
generateForm, and the result is prepended to$forms. - If the parameter of
generateFormis optional, you can omit the argument.
$remove() method
- Pass the index of the array element to be removed.
- Negative integers count back from the last item in the array, like
Array.prototype.at.
- Negative integers count back from the last item in the array, like
$writableForms property
- ⚠️ Usually should not be used.
- Even if you use the
Array.prototype.pushmethod etc,$errorproperty cannot detect the change by reactively.
- Even if you use the
- Returns the same reference as
$formsproperty, and settable. - For example, using with Vue.Draggable's draggable component.
<draggable v-model="formsFieldObject.$writableForm">