File tree Expand file tree Collapse file tree 3 files changed +176
-0
lines changed
components/examples/fields Expand file tree Collapse file tree 3 files changed +176
-0
lines changed Original file line number Diff line number Diff line change @@ -58,6 +58,7 @@ export default defineConfig({
5858 { text : 'FieldColor' , link : '/guide/fields/FieldColor' } ,
5959 { text : 'FieldMask' , link : '/guide/fields/FieldMask' } ,
6060 { text : 'FieldNumber' , link : '/guide/fields/FieldNumber' } ,
61+ { text : 'FieldObject' , link : '/guide/fields/FieldObject' } ,
6162 { text : 'FieldPassword' , link : '/guide/fields/FieldPassword' } ,
6263 { text : 'FieldRadio' , link : '/guide/fields/FieldRadio' } ,
6364 { text : 'FieldReset' , link : '/guide/fields/FieldReset' } ,
Original file line number Diff line number Diff line change 1+ <template >
2+ Person: <code >{{ form.model.person }}</code >
3+ <vue-form-generator :schema =" form.schema" :model =" form.model" />
4+ </template >
5+
6+ <script setup>
7+ import { onBeforeMount , ref } from ' vue'
8+
9+ const props = defineProps ({
10+ addValidators: Boolean
11+ })
12+
13+ const form = ref ({
14+ model: {
15+ person: {
16+ name: ' ' ,
17+ surname: ' ' ,
18+ age: null
19+ }
20+ },
21+ schema: {
22+ fields: [
23+ {
24+ type: ' object' ,
25+ model: ' person' ,
26+ schema: {
27+ fields: [
28+ {
29+ type: ' input' ,
30+ inputType: ' text' ,
31+ model: ' name' ,
32+ label: ' Name'
33+ },
34+ {
35+ type: ' input' ,
36+ inputType: ' text' ,
37+ model: ' surname' ,
38+ label: ' Surname'
39+ },
40+ {
41+ type: ' input' ,
42+ inputType: ' number' ,
43+ model: ' age' ,
44+ label: ' Age'
45+ }
46+ ]
47+ }
48+ }
49+ ]
50+ }
51+ })
52+
53+ onBeforeMount (() => {
54+ if (props .addValidators ) {
55+ const fields = form .value .schema .fields [0 ].schema .fields
56+
57+ const minLengthThree = (value ) => value && value .length >= 3
58+ fields[0 ].validator = minLengthThree
59+ fields[1 ].validator = minLengthThree
60+ fields[2 ].validator = (value ) => value && value >= 18
61+ }
62+ })
63+ </script >
Original file line number Diff line number Diff line change 1+ # FieldObject
2+ ` FieldObject ` is a field that has its own ` schema ` , meaning the field itself
3+ renders other fields. These fields will return their values to the object inside
4+ the model that is assigned to the ` FieldObject ` component.
5+
6+ ### type ` object `
7+
8+ <script setup >
9+ import FieldObjectExample from ' /components/examples/fields/FieldObjectExample.vue'
10+ </script >
11+
12+ ## Basic example
13+ ::: details Code
14+ ``` js
15+ const form = ref ({
16+ model: {
17+ person: {
18+ name: ' ' ,
19+ surname: ' ' ,
20+ age: null
21+ }
22+ },
23+ schema: {
24+ fields: [
25+ {
26+ type: ' object' ,
27+ model: ' person' ,
28+ schema: {
29+ fields: [
30+ {
31+ type: ' input' ,
32+ inputType: ' text' ,
33+ model: ' name' ,
34+ label: ' Name'
35+ },
36+ {
37+ type: ' input' ,
38+ inputType: ' text' ,
39+ model: ' surname' ,
40+ label: ' Surname'
41+ },
42+ {
43+ type: ' input' ,
44+ inputType: ' number' ,
45+ model: ' age' ,
46+ label: ' Age'
47+ }
48+ ]
49+ }
50+ }
51+ ]
52+ }
53+ })
54+ ```
55+ :::
56+ <FieldObjectExample />
57+
58+ ## With validators
59+ ::: details Code
60+ ``` js
61+ function minLengthThree (value ) {
62+ return value && value .length >= 3
63+ }
64+
65+ function overEighteen (value ) {
66+ return value && value >= 18
67+ }
68+
69+ // ......
70+ fields: [
71+ {
72+ type: ' object' ,
73+ model: ' person' ,
74+ schema: {
75+ fields: [
76+ {
77+ type: ' input' ,
78+ inputType: ' text' ,
79+ model: ' name' ,
80+ label: ' Name' ,
81+ validator: minLengthThree
82+ },
83+ {
84+ type: ' input' ,
85+ inputType: ' text' ,
86+ model: ' surname' ,
87+ label: ' Surname' ,
88+ validator: minLengthThree
89+ },
90+ {
91+ type: ' input' ,
92+ inputType: ' number' ,
93+ model: ' age' ,
94+ label: ' Age' ,
95+ validator: overEighteen
96+ }
97+ ]
98+ }
99+ }
100+ ]
101+ ```
102+ :::
103+ ::: info
104+ In this example, ` name ` and ` surname ` must have a length of three letters or more, ` age ` must be at least 18.
105+ :::
106+ <FieldObjectExample add-validators />
107+
108+
109+ ## Properties
110+ | Property | Default | Type | Description |
111+ | ----------| ---------| ----------| ---------------------------------------------|
112+ | schema | ` {} ` | ` Object ` | A form schema, as seen in ` FormGenerator.vue ` |
You can’t perform that action at this time.
0 commit comments