This repository has been archived by the owner on Dec 25, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 431
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🆙 update(examples): update v-model integration examples [ci skip]
NOTICE: v-model integration is still buggy ... 🙇
- Loading branch information
Showing
5 changed files
with
257 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>component validation example</title> | ||
<script src="https://unpkg.com/[email protected]"></script> | ||
<script src="https://unpkg.com/[email protected]"></script> | ||
<script src="../../../node_modules/vue/dist/vue.js"></script> | ||
<script src="../../../dist/vue-validator.js"></script> | ||
<link href="https://unpkg.com/[email protected]/dist/css/select2.min.css" rel="stylesheet" type="text/css"> | ||
<style> | ||
input.invalid { border-color: red; } | ||
.errors { color: red; } | ||
</style> | ||
</head> | ||
<body> | ||
<div id="app"> | ||
<validity ref="validity" field="select" :validators="{ | ||
selected: { rule: true, message: 'not selected item!!' } | ||
}"> | ||
<select2 :options="options" v-model.validity="selected" @input="handleValidate"> | ||
<option value="0">----- Select one -----</option> | ||
</select2> | ||
</validity> | ||
<p class="errors" v-if="result.selected">{{result.selected}}</p> | ||
</div> | ||
<script> | ||
new Vue({ | ||
data: { | ||
selected: 0, | ||
options: [ | ||
{ id: 1, text: 'Hello' }, | ||
{ id: 2, text: 'World' } | ||
], | ||
result: {} | ||
}, | ||
validators: { | ||
selected: function (val) { | ||
return parseInt(val, 10) !== 0 | ||
} | ||
}, | ||
components: { | ||
// select2 wrap component | ||
select2: { | ||
template: '<select><slot></slot></select>', | ||
props: ['options', 'value'], | ||
mounted: function () { | ||
var vm = this | ||
$(this.$el) | ||
.val(this.value) | ||
.select2({ data: this.options }) | ||
.on('change', function () { | ||
// value: event out | ||
vm.$emit('input', this.value) | ||
}) | ||
}, | ||
watch: { | ||
value: function (value) { | ||
$(this.$el).select2('val', value) | ||
}, | ||
options: function (options) { | ||
$(this.$el).select2({ data: options }) | ||
} | ||
}, | ||
destroyed: function () { | ||
$(this.$el).off().select2('destroy') | ||
} | ||
} | ||
}, | ||
mounted () { | ||
// initial validation | ||
this.validate(this.selected) | ||
}, | ||
methods: { | ||
// wrapper method of $validity.validate | ||
validate: function (val, fn) { | ||
var self = this | ||
var $validity = this.$refs.validity | ||
$validity.validate({ validator: 'selected', value: val }, function () { | ||
var result = $validity.result | ||
fn && fn(result) | ||
self.result = result | ||
}) | ||
}, | ||
handleValidate: function (val) { | ||
var $validity = this.$refs.validity | ||
this.validate(val, function (result) { | ||
$validity.pass() | ||
}) | ||
} | ||
} | ||
}).$mount('#app') | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.