我想在自定义组件上使用Vuetify custom validation。
例如,我创建了一个日期时间选择器组件
DateTimePicker.vue
<template>
<v-menu v-model="menu" :close-on-content-click="false" full-width max-width="290" transition="scale-transition">
<!-- Text field -->
<v-text-field slot="activator" :label="label" append-icon="date_range" solo
:value="formattedDate" readonly></v-text-field>
<!-- Date picker -->
<v-date-picker v-model="selectedDate" locale="fr-fr" v-if="datePicker" :min="minDate">
<v-spacer></v-spacer>
<v-btn flat color="primary" @click="menu = false">{{ $t('cancel') }}</v-btn>
<v-btn flat color="primary" @click="chooseDate">{{ $t('ok') }}</v-btn>
</v-date-picker>
<!-- Time picker -->
<v-time-picker v-if="!datePicker" v-model="selectedTime" format="24hr" :min="minTime" :allowed-minutes="allowedStep">
<v-spacer></v-spacer>
<v-btn flat color="primary" @click="menu = false">{{ $t('cancel') }}</v-btn>
<v-btn flat color="primary" @click="chooseTime">{{ $t('ok') }}</v-btn>
</v-time-picker>
</v-menu>
</template>
我想对父组件中的自定义组件应用规则验证。
Parent.vue
<v-container fluid grid-list-xl>
<v-layout row wrap class="pt-4">
<v-flex xs12 md3>
<v-autocomplete ref="city" v-model="city" :items="locations" item-text="description" :label="$t('stores')" solo
:rules="[rules.required]"></v-autocomplete>
</v-flex>
<!-- Start date picker -->
<v-flex xs12 md3>
<date-time-picker ref="startDate" v-model="startDate" :label="$t('start_date')" :rules="[rules.required]"></date-time-picker>
</v-flex>
<!-- End date picker -->
<v-flex xs12 md3>
<date-time-picker v-model="endDate" :label="$t('end_date')"></date-time-picker>
</v-flex>
<v-flex xs12 md3>
<v-btn class="primary btn-bigger" @click="valdiate" block> {{ $t('search') }}</v-btn>
</v-flex>
</v-layout>
</v-container>
我想做的事情与我为v-autocomplete
做的事情一样。我试图在自己的<date-time-picker>
上使用规则属性,并在自定义组件中的<v-text-field>
上使用它,但是它不起作用,我得到了这个错误:
_this2.$refs[f].validate is not a function
我使用了与documentation相同的代码,因此它适用于我的<v-autocomplete>
,但我不知道该如何为我的自定义组件做事。
答案 0 :(得分:1)
您需要将规则数组作为道具从父组件发送到孩子(DateTimePicker),然后将其绑定到v文本字段组件(在子组件内部:DateTimePicker)。
props: {
rules: {
type: Array,
required: false
}
}
<v-text-field slot="activator" :label="label" append-icon="date_range" solo
:value="formattedDate" readonly
:rules="rules">
</v-text-field>
<v-flex xs12 md3>
<date-time-picker ref="startDate" v-model="startDate" :label="$t('start_date')"
:rules="[rules.required]">
</date-time-picker>
</v-flex>