1

I'm trying to use vue.js to do form validation and when the element is valid to submit it via ajax. But I can't get past the validation part.

My html:

<div id='form' 'v-on'="change:updateForm">
<form>
    <select id="selects" name="selected_id" v-model='form.selected_id | selectValidator'>
        <option value="">Please choose one
            <option value='1'>Select 1</option>
            <option value='2'>Select 2</option>
    </select>
</form>
<br>
<div v-show="!validation.selected_id">Select cannot be blank</div>
<div v-show="validation.selected_id">{{form.selected_id}}</div>
<div v-show="validation.selected_id">{{validation.selected_id}}</div>

My javascript:

var app = new Vue({
el: '#form',
filters: {
    selectValidator: function (val) {
        this.validation.selected_id = !! val
        return val
    }
},
data: {
    form: {
        selected_id: ''
    },
    validation: {
        selected_id: false
    }
},
methods: {
    updateForm: function (e) {
        console.log(this.validation.selected_id)
        if (this.validation.selected_id) {
            console.log("make ajax call")
        }
    }
}

})

When looking into the console I see this.validation.selected_id as false instead of what it really is when it's printed out: true

Here's a jsfiddle for testing, but it doesn't show the console log.

4
  • There is now a vue-validator plugin available for Vue. It works a lot like the one for AngularJS. Commented Mar 3, 2015 at 2:47
  • Thank you for sending me the link Commented Apr 15, 2015 at 3:01
  • Have you seen the vue-contextable? Contextable.js is build for server-side and client-side validation. Commented Nov 13, 2016 at 8:20
  • Thanks @xpepermint - I'll have to look into it, but as of now I'm no longer working on Vue.js - things have probably gotten much better now. Commented Nov 14, 2016 at 22:28

1 Answer 1

0

The first line has single quotes around 'v-on' when there shouldn't be any:

<div id='form' v-on="change:updateForm">

Here's a JSFiddle with the working version

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

1 Comment

If you create a local html file with these items and run it, you'll see the console will log false when you select either one, then select the 2nd one and it will log true and also log this output: "make ajax call" - unfortunately your answer does not fix the problem.

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.