Vuelidate example

by doginthehat

HTML

<script src="https://unpkg.com/vuelidate/dist/vuelidate.min.js"></script>
<script src="https://unpkg.com/vuelidate/dist/validators.min.js"></script>
<div id="app">
   Trying to force error status.
  <input type="text" v-model="text"
    v-on:input="$v.text.$touch"
    v-bind:class="{error: $v.text.$error, valid: $v.text.$dirty && !$v.text.$invalid}"
  >
  <button @click="make_required">
  Force required
  </button>
  <pre>{{ $v }}</pre>
  
</div>

CSS

input {
  border: 1px solid silver;
  border-radius: 4px;
  background: white;
  padding: 5px 10px;
}

.error {
  border-color: red;
  background: #FDD;
}

.error:focus {
  outline-color: #F99;
}

.valid {
  border-color: #5A5;
  background: #EFE;
}

.valid:focus {
  outline-color: #8E8;
}

Babel + JSX

Vue.use(window.vuelidate.default)
const { required, minLength } = window.validators

new Vue({
	el: "#app",
  data: {
  	text: ''
  },
  validations: {
  	text: {
    	required,
      minLength: minLength(5)
    }
  },
  methods: {
    make_required:function() {
    	try {
        this.$v.text.required = true
        this.$v.text.$error = true
      }catch(e) {
        alert("Error: " +e)
      }
    }
  }
})