π ΠΠ»ΠΎΠΆΠ΅Π½Π½ΡΠ΅ ΡΡΡΡΠΊΡΡΡΡ Π΄Π°Π½Π½ΡΡ β
ΠΠΈΠ±Π»ΠΈΠΎΡΠ΅ΠΊΠ° ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΈΠ²Π°Π΅Ρ Π²Π°Π»ΠΈΠ΄Π°ΡΠΈΡ Π΄ΠΈΠ½Π°ΠΌΠΈΡΠ΅ΡΠΊΠΈΡ ΠΌΠ°ΡΡΠΈΠ²ΠΎΠ² ΠΈ Π²Π»ΠΎΠΆΠ΅Π½Π½ΡΡ ΠΎΠ±ΡΠ΅ΠΊΡΠΎΠ² Ρ ΡΠΈΠΏΠΎΠ±Π΅Π·ΠΎΠΏΠ°ΡΠ½ΡΠΌ API.
ΠΠΈΠ½Π°ΠΌΠΈΡΠ΅ΡΠΊΠΈΠ΅ ΠΌΠ°ΡΡΠΈΠ²Ρ β
typescript
interface Contact {
name: string
email: string
role: string
}
const form = createForm(
{
teamName: '',
contacts: [] as Contact[],
},
r => ({
teamName: r.required(),
contacts: r.arrayMinLength(1),
'contacts.*.name': r.required(),
'contacts.*.email': r.required().email(),
'contacts.*.role': r.required(),
})
)
// Π£ΠΏΡΠ°Π²Π»Π΅Π½ΠΈΠ΅ ΠΌΠ°ΡΡΠΈΠ²ΠΎΠΌ
form.addArrayItem('contacts', { name: '', email: '', role: '' })
form.removeArrayItem('contacts', index)ΠΡΠΈΠΌΠ΅Ρ ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½ΡΠ°:
vue
<template>
<div v-for="(contact, index) in form.values.contacts" :key="index">
<input
v-model="contact.name"
@blur="form.touch(form.arrayPath('contacts', index, 'name'))"
/>
<span v-if="form.hasError(form.arrayPath('contacts', index, 'name'))">
{{ form.error(form.arrayPath('contacts', index, 'name')) }}
</span>
<button @click="form.removeArrayItem('contacts', index)">Π£Π΄Π°Π»ΠΈΡΡ</button>
</div>
<button
@click="form.addArrayItem('contacts', { name: '', email: '', role: '' })"
>
ΠΠΎΠ±Π°Π²ΠΈΡΡ ΠΊΠΎΠ½ΡΠ°ΠΊΡ
</button>
</template>ΠΠ»ΠΎΠΆΠ΅Π½Π½ΡΠ΅ ΠΎΠ±ΡΠ΅ΠΊΡΡ β
typescript
const form = createForm(
{
name: '',
address: { street: '', city: '', zipCode: '' },
profile: { bio: '', website: '' },
},
r => ({
name: r.required(),
'address.street': r.required(),
'address.city': r.required(),
'address.zipCode': r.required().regex(/^\d{5}$/, 'ZIP: 5 ΡΠΈΡΡ'),
'profile.bio': r.maxLength(200),
'profile.website': r.regex(/^https?:\/\/.+/, 'ΠΠ°ΡΠ½ΠΈΡΠ΅ Ρ http://'),
})
)ΠΡΠΈΠΌΠ΅Ρ ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½ΡΠ°:
vue
<template>
<fieldset>
<legend>ΠΠ΄ΡΠ΅Ρ</legend>
<!-- Π‘ΡΡΠΎΠΊΠΎΠ²ΡΠ΅ ΠΏΡΡΠΈ β ΠΏΡΠΎΡΡΠΎ ΠΈ Π½Π°Π³Π»ΡΠ΄Π½ΠΎ -->
<input
v-model="form.values.address.street"
@blur="form.touch('address.street')"
/>
<span v-if="form.hasError('address.street')">{{
form.error('address.street')
}}</span>
<!-- objectPath() β Ρ Π°Π²ΡΠΎΠ΄ΠΎΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ΠΌ TypeScript -->
<input
v-model="form.values.address.city"
@blur="form.touch(form.objectPath('address', 'city'))"
/>
<span v-if="form.hasError(form.objectPath('address', 'city'))">
{{ form.error(form.objectPath('address', 'city')) }}
</span>
</fieldset>
</template>