4

I'm trying to make a simple form validation page work with vee-validate. Something appears to be broken though, and I am unsure what exactly I am doing wrong.

The span tag appears as raw html:

enter image description here

Markup:

<!DOCTYPE html>
<html>
   <head>
      <script type='text/JavaScript' src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
      <script type='text/JavaScript' src="https://cdn.jsdelivr.net/npm/vee-validate@latest/dist/vee-validate.js"></script>
   </head>
   <body>
      <script type='text/JavaScript'>
         Vue.use(VeeValidate); // good to go.
         new Vue({
           el: '#app',
           data: {
              email_primary: null
           }
         });
      </script>


      <div id="app">
         <form action='#' method='POST'>
            <input v-validate="'required|email'" :class="{'input': true, 'is-danger': errors.has('email_primary') }" name="email_primary" type="text" placeholder="email_primary">
            <span v-show="errors.has('email_primary')" class="help is-danger">{{ errors.first('email_primary') }}</span>
            <button class="button is-link" name='submitform' value='go'>Submit</button>
         </form>
      </div>

    </body>
</html>

Fiddle.

What do I need to do to make vee-validate work as expected?

1 Answer 1

3

The issue(s)

It appears to have been a few things, but the main culprit was that the type attribute on all the script tags were set to text/JavaScript which is invalid. Its best to either not set the type, or if you do, set it to text/javascript.

Also, since you're utilizing div#app as a template rather than just the root element, I added in the proper attributes.

Finally, always load your javascript after your html.

Fixed Code

<!DOCTYPE html>
<html>
   <head>
      
   </head>
   <body>
       <div id="app">
         <form action='#' method='POST'>
            <input v-validate="'required|email'" :class="{'input': true, 'is-danger': errors.has('email_primary') }" name="email_primary" type="text" placeholder="email_primary">
            <span v-show="errors.has('email_primary')" class="help is-danger">{{ errors.first('email_primary') }}</span>
            <button class="button is-link" name='submitform' value='go'>Submit</button>
         </form>
      </div>
      <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
      <script src="https://cdn.jsdelivr.net/npm/vee-validate@latest/dist/vee-validate.js"></script>
      <script>
         Vue.use(VeeValidate); // good to go.
         new Vue({
           el: "#app",
           template: '#app',
           data() {
            return {
              email_primary: null
            };
           }
         });
      </script>
    </body>
</html>

Also, a working jsfiddle.

I hope this helps!

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks so much, this got me pointed in right direction. Can you explain a little about this: div#app as a template rather than just the root element. What do you mean by root element? Is there a better way to code it in?
@acoder well typically, you'd build it out so that all your components were self-contained in their own file - that includes HTML, CSS and the vue logic. A great tool for starting off is the vue-cli. It will scaffold you a new project with example logic so you can get a feel for it
What you described above (one file with dedicated form validation) is what I am looking for (I'm using Bulma html framework fwiw). Clearly, I am new to vee-validate so this is helpful.
Thanks again for the answer, just implemented and functions and looks very nice with Bulma.
Just found this similar UI extension for Bulma: buefy.github.io
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.