user9441470
user9441470

Reputation:

Vuetify stepper alert message when blanks not filled

I have gotten some help in making this Vuetify stepper code but I have one last question. I have understood that there are rules you can set, so that those blanks must be filled out. For example in my code, when you haven't filled the blanks in step 3 and want to continue, it does not let you. But when you have not filled the blanks in step 2 it just lets you finish.

Is there a way to let the user know under which step the requirements have not been met by for example displaying alert message which should be something like this :rules="[() => false]" and when user has filled the blanks to switch the icon back to normal?

Here is the code:

Codepen

        <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>

The official Vuetify stepper tutorial page Vuetify stepper

I just dont seem to understand how if statements work in vuetify. And how to change the icons and such.

Upvotes: 2

Views: 3313

Answers (1)

ittus
ittus

Reputation: 22403

For more complicated validation, I prefer to use library like Vee Validate

Added to your script

Vue.use(VeeValidate);

Basically, each step will become:

<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>

Note that how v-validate and errors.has are used. You can checkout demo at https://codepen.io/ittus/pen/ZoRyKv

Upvotes: 1

Related Questions