未填充空白时验证步进器警报消息
Vuetify stepper alert message when blanks not filled
我在编写此 Vuetify stepper
代码时得到了一些帮助,但我还有最后一个问题。我了解到有rules
可以设置,所以那些blanks
必须填写。例如在我的代码中,当你还没有填写 step 3
中的 blanks
并想继续时,它不会让你。但是当你还没有填写 step 2
中的 blanks
时,它只会让你完成。
有没有办法让用户知道 step
requirements
还没有满足,例如显示警告消息应该是这样的 :rules="[() => false]"
以及什么时候用户已填写 blanks
以将图标切换回正常状态?
代码如下:
<html>
<head>
<link href='https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Material+Icons' rel="stylesheet">
<link href="https://unpkg.com/vuetify/dist/vuetify.min.css" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
</head>
<body>
<div id="app">
<v-app>
<v-content>
<v-container>
<v-stepper v-model="step" non-linear vertical>
<v-stepper-step :complete="step > 1" step="1" editable>
Person
</v-stepper-step>
<v-stepper-content step="1">
<v-text-field label="Name" v-model="registration.name" required></v-text-field>
<v-text-field label="Email" v-model="registration.email" required></v-text-field>
<v-btn color="primary" @click.native="step = 2">Continue</v-btn>
</v-stepper-content>
<v-stepper-step :complete="step > 2" step="2" editable>
Person
</v-stepper-step>
<v-stepper-content step="2">
<v-text-field label="Street" v-model="registration.street" required></v-text-field>
<v-text-field label="City" v-model="registration.city" required></v-text-field>
<v-text-field label="State" v-model="registration.state" :rules="
[v => !!v || 'Item is required']" required></v-text-field>
<v-btn flat @click.native="step = 1">Previous</v-btn>
<v-btn color="primary" @click.native="step = 3">Continue</v-btn>
</v-stepper-content>
<v-stepper-step step="3" editable>Misc Info</v-stepper-step>
<v-stepper-content step="3">
<v-form ref="form" v-model="valid" lazy-validation>
<v-text-field label="Number of Tickets" type="number"
v-model="registration.numtickets"
:rules="[v => !!v || 'Item is required']"></v-text-field>
<v-select label="Shirt Size" v-model="registration.shirtsize"
:items="sizes"
:rules="[v => !!v || 'Item is required']"></v-select>
<v-btn flat @click.native="step = 2" >Previous</v-btn>
<v-btn color="primary" @click="submit">Save</v-btn>
</v-form>
</v-stepper-content>
</v-stepper>
</v-container>
</v-content>
</v-app>
<br/>
<br/>Debug: {{registration}}
</div>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vuetify/dist/vuetify.js"></script>
<script>
new Vue({
el: '#app',
data: () => ({
step:1,
registration:{
name:null,
email:null,
street:null,
city:null,
state:null,
numtickets:0,
shirtsize:'XL'
},
sizes:['S','M','L','XL']
}),
methods:{
submit() {
alert('This is the post. Blah');
}
}
})
</script>
</body>
</html>
官方Vuetify
stepper tutorial page
Vuetify stepper
我似乎不明白 if
语句在 vuetify
中是如何工作的。以及如何更改 icons
等。
对于更复杂的验证,我更喜欢使用像Vee Validate
这样的库
已添加到您的脚本
Vue.use(VeeValidate);
基本上每一步都会变成:
<v-stepper-step :complete="step > 1"
step="1" editable
:rules="[() => !errors.has('name') && !errors.has('email')]">
Person
</v-stepper-step>
<v-stepper-content step="1">
<v-text-field name="name"
label="Name"
v-model="registration.name"
v-validate="'required'"
:error-messages="errors.collect('name')">
</v-text-field>
<v-text-field name="email"
label="Email"
v-model="registration.email"
v-validate="'required|email'"
:error-messages="errors.collect('email')">
</v-text-field>
<v-btn color="primary" @click.native="step = 2">Continue</v-btn>
</v-stepper-content>
请注意 v-validate
和 errors.has
的用法。
您可以在 https://codepen.io/ittus/pen/ZoRyKv
查看演示
我在编写此 Vuetify stepper
代码时得到了一些帮助,但我还有最后一个问题。我了解到有rules
可以设置,所以那些blanks
必须填写。例如在我的代码中,当你还没有填写 step 3
中的 blanks
并想继续时,它不会让你。但是当你还没有填写 step 2
中的 blanks
时,它只会让你完成。
有没有办法让用户知道 step
requirements
还没有满足,例如显示警告消息应该是这样的 :rules="[() => false]"
以及什么时候用户已填写 blanks
以将图标切换回正常状态?
代码如下:
<html>
<head>
<link href='https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Material+Icons' rel="stylesheet">
<link href="https://unpkg.com/vuetify/dist/vuetify.min.css" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
</head>
<body>
<div id="app">
<v-app>
<v-content>
<v-container>
<v-stepper v-model="step" non-linear vertical>
<v-stepper-step :complete="step > 1" step="1" editable>
Person
</v-stepper-step>
<v-stepper-content step="1">
<v-text-field label="Name" v-model="registration.name" required></v-text-field>
<v-text-field label="Email" v-model="registration.email" required></v-text-field>
<v-btn color="primary" @click.native="step = 2">Continue</v-btn>
</v-stepper-content>
<v-stepper-step :complete="step > 2" step="2" editable>
Person
</v-stepper-step>
<v-stepper-content step="2">
<v-text-field label="Street" v-model="registration.street" required></v-text-field>
<v-text-field label="City" v-model="registration.city" required></v-text-field>
<v-text-field label="State" v-model="registration.state" :rules="
[v => !!v || 'Item is required']" required></v-text-field>
<v-btn flat @click.native="step = 1">Previous</v-btn>
<v-btn color="primary" @click.native="step = 3">Continue</v-btn>
</v-stepper-content>
<v-stepper-step step="3" editable>Misc Info</v-stepper-step>
<v-stepper-content step="3">
<v-form ref="form" v-model="valid" lazy-validation>
<v-text-field label="Number of Tickets" type="number"
v-model="registration.numtickets"
:rules="[v => !!v || 'Item is required']"></v-text-field>
<v-select label="Shirt Size" v-model="registration.shirtsize"
:items="sizes"
:rules="[v => !!v || 'Item is required']"></v-select>
<v-btn flat @click.native="step = 2" >Previous</v-btn>
<v-btn color="primary" @click="submit">Save</v-btn>
</v-form>
</v-stepper-content>
</v-stepper>
</v-container>
</v-content>
</v-app>
<br/>
<br/>Debug: {{registration}}
</div>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vuetify/dist/vuetify.js"></script>
<script>
new Vue({
el: '#app',
data: () => ({
step:1,
registration:{
name:null,
email:null,
street:null,
city:null,
state:null,
numtickets:0,
shirtsize:'XL'
},
sizes:['S','M','L','XL']
}),
methods:{
submit() {
alert('This is the post. Blah');
}
}
})
</script>
</body>
</html>
官方Vuetify
stepper tutorial page
Vuetify stepper
我似乎不明白 if
语句在 vuetify
中是如何工作的。以及如何更改 icons
等。
对于更复杂的验证,我更喜欢使用像Vee Validate
这样的库已添加到您的脚本
Vue.use(VeeValidate);
基本上每一步都会变成:
<v-stepper-step :complete="step > 1"
step="1" editable
:rules="[() => !errors.has('name') && !errors.has('email')]">
Person
</v-stepper-step>
<v-stepper-content step="1">
<v-text-field name="name"
label="Name"
v-model="registration.name"
v-validate="'required'"
:error-messages="errors.collect('name')">
</v-text-field>
<v-text-field name="email"
label="Email"
v-model="registration.email"
v-validate="'required|email'"
:error-messages="errors.collect('email')">
</v-text-field>
<v-btn color="primary" @click.native="step = 2">Continue</v-btn>
</v-stepper-content>
请注意 v-validate
和 errors.has
的用法。
您可以在 https://codepen.io/ittus/pen/ZoRyKv