Created
July 22, 2017 00:35
-
-
Save Kelin2025/fbc51e4c56c88f3b6af96d2b0522d504 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { required, numeric } from 'vuelidate/lib/validators' | |
import { buildFromValidator } from '@/common/utils/forms' | |
const form = { | |
name: { required }, | |
weapon: { required }, | |
price: { numeric }, | |
image: { required }, | |
nested: { | |
item: { required, numeric } | |
} | |
} | |
export default { | |
data: () => ({ | |
form: buildFromValidator(form) | |
}), | |
validations: { form }, | |
methods: { | |
validate () { | |
this.$v.form.$touch() | |
return this.$v.form.$invalid | |
}, | |
reset () { | |
this.form = buildFromValidator(form) | |
} | |
} | |
} |
This file contains hidden or 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
import map from 'lodash/map' | |
import omitBy from 'lodash/omitBy' | |
import zipObject from 'lodash/zipObject' | |
export const buildFromValidator = form => | |
zipObject( | |
Object.keys(form), | |
map( | |
omitBy(form, item => typeof item === 'function'), | |
item => | |
item === null || | |
typeof item === 'function' | |
? null | |
: buildFromValidator(item) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment