Skip to content

Instantly share code, notes, and snippets.

@sweemeng
Created March 10, 2018 04:08
Show Gist options
  • Save sweemeng/85b59fe552f7c21edc568b1046f8fdfa to your computer and use it in GitHub Desktop.
Save sweemeng/85b59fe552f7c21edc568b1046f8fdfa to your computer and use it in GitHub Desktop.
vue-json-schema regex validation, didn't work
{
"description": "A means of contacting an entity",
"id": "http://www.popoloproject.com/schemas/contact_detail.json#",
"properties": {
"birth_date": {
"pattern": "^[0-9]{4}(-[0-9]{2}){0,2}$",
"title": "A date of birth",
"type": "string"
}
},
"required": [
"birth_date"
],
"title": "Dummy",
"type": "object"
}
<template>
<el-card class="form">
<form-schema ref="formSchema" :schema="schema" v-model="model">
<el-button type="primary" @click="submit">Create</el-button>
</form-schema>
</el-card>
</template>
<script>
import FormSchema from 'vue-json-schema'
import schema from '../schema/dummy.json'
FormSchema.setComponent('form', 'el-form', ({ vm }) => {
const labelPosition = 'top'
const labelWidth = '120px'
const model = vm.data
const rule = {}
vm.fields.forEach((field) => {
const type = field.schemaType
const required = field.required
const message = field.title
const trigger = 'blur'
rule[field.name] = { type, required, message, trigger }
})
return { labelWidth, rule, model, labelPosition }
})
FormSchema.setComponent('label', 'el-form-item', ({ field }) => ({
prop: field.name
}))
FormSchema.setComponent('text', 'el-input')
export default {
data () {
return {
schema: schema,
model: {}
}
},
methods: {
submit (e) {
var loggedIn = this.$store.state.loggedIn
if (loggedIn) {
this.$refs.formSchema.form().validate((valid) => {
if (valid) {
console.log(this.model)
} else {
this.$refs.formSchema.setErrorMessage('Please Fill in the form')
return false
}
})
} else {
this.$route.push('/login')
}
}
},
components: {
FormSchema
}
}
</script>
<style scoped>
.form {
text-align: left;
width: 600px;
margin: auto;
}
.el-form {
text-align: left;
}
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment