Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vuelidate date input field

I hope someone can clarify to me, how to properly use Vuelidate while validating Date object. I want to use the current date as a minimal date, so if a user inputs a date later, an error will be shown.

I have an example: https://jsfiddle.net/cheslavcc/fns8eh0f/1/

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

new Vue({
  el: "#app",
  data: {
    text: ''
  },
  validations: {
    text: {
        minValue: minValue(moment(new Date(), 'DD.MM.YYYY').format('DD.MM.YYYY')),
    }
  }
})

I'm using Moment.js as a date formatter, and minValue from the official Vuelidate documentation: https://monterail.github.io/vuelidate/#sub-builtin-validators

Any help appreciated, thanks a lot!

like image 495
Cheslav Avatar asked May 19 '18 14:05

Cheslav


1 Answers

You can define custom validator:

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

const currentDate = moment(new Date()).startOf('day')
const minDate = window.vuelidate.withParams({minDate: currentDate.format('DD.MM.YYYY')}, value => moment(value, 'DD.MM.YYYY', true).isSameOrAfter(currentDate))

new Vue({
	el: "#app",
  data: {
  	text: ''
  },
  validations: {
  	text: {
			minDate
    }
  }
})
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;
}
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>
<script src="https://unpkg.com/vuelidate/dist/validators.min.js"></script>
<script src="https://unpkg.com/vuelidate/dist/vuelidate.min.js"></script>
<div id="app">
  <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}"
  >
  <pre>{{ $v }}</pre>
  Text: {{ text }}
</div>
like image 168
ittus Avatar answered Oct 21 '22 23:10

ittus