Skip to content

Commit

Permalink
Merge pull request #43 from 14nrv/dev
Browse files Browse the repository at this point in the history
Fix: add required rule
  • Loading branch information
14nrv authored Oct 24, 2020
2 parents ccc45d7 + 91d817c commit e01ffef
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ Object.keys(rules).forEach(rule => {
```html
<template lang="pug">
#app.section
form-json(:formFields="jsonFields",
:formName="'userProfil'"
form-json(:btnReset="{value: 'Reset'}",
:btnSubmit="{value: 'Submit'}",
:btnReset="{value: 'Reset'}")
:formFields="jsonFields",
formName="userProfil")
div(slot="slotNameAddedInJsonFields")
p Your slot content
</template>
Expand All @@ -91,11 +91,11 @@ Object.keys(rules).forEach(rule => {
components: {
formJson
},
data: () => ({
jsonFields
}),
mounted () {
this.$root.$on('formSubmitted', values => alert(JSON.stringify(values)))
},
computed: {
jsonFields: () => jsonFields
}
}
</script>
Expand Down
12 changes: 6 additions & 6 deletions src/App.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<template lang="pug">
#app.section
app-form(:formFields="jsonFields",
formName="userProfil",
app-form(:btnReset="{value: 'Reset'}",
:btnSubmit="{value: 'Submit'}",
:btnReset="{value: 'Reset'}")
:formFields="jsonFields",
formName="userProfil")
div(slot="boxSlot")
.box
article
Expand All @@ -24,11 +24,11 @@ export default {
components: {
appForm: Form
},
data: () => ({
jsonFields
}),
mounted () {
this.$root.$on('formSubmitted', values => alert(JSON.stringify(values)))
},
computed: {
jsonFields: () => jsonFields
}
}
</script>
Expand Down
2 changes: 2 additions & 0 deletions src/components/Fields/Control.vue
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ export default {
},
getRules () {
const { rules = {}, pattern } = this.item
rules.required = this.item.isRequired !== false
let validation
pattern && (validation = { regex: new RegExp(pattern) })
return { ...rules, ...validation }
Expand Down
7 changes: 7 additions & 0 deletions src/components/Form/Form.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,13 @@ describe('Form', () => {
expect($inputSubmit).toHaveAttribute('disabled', 'disabled')
})

it('has submit btn disabled if no default value in field', async () => {
await wrapper.setProps({ formFields: [{ label: 'a label' }] })
await flush()

expect($inputSubmit).toHaveAttribute('disabled', 'disabled')
})

it('enables submit input if all fields are valid', async () => {
await flush()
expect($inputSubmit).toHaveAttribute('disabled', 'disabled')
Expand Down
5 changes: 4 additions & 1 deletion src/components/Form/fields.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
[
{
"label": "Email",
"type": "email"
"type": "email",
"rules": {
"email": true
}
},
{
"label": "Phone Number",
Expand Down

0 comments on commit e01ffef

Please sign in to comment.