-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from formly-js/validation-messages
Implemented validation messages.
- Loading branch information
Showing
6 changed files
with
240 additions
and
60 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
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 |
---|---|---|
@@ -1,25 +1,26 @@ | ||
import Components from './components/index'; | ||
import Filters from './filters/index'; | ||
import Util,{getTypes, addType} from './util'; | ||
import Util,{getTypes, addType, addValidationMessage} from './util'; | ||
|
||
|
||
let Formly = { | ||
getTypes, | ||
addType, | ||
install(Vue, options){ | ||
|
||
//install our components | ||
Components(Vue); | ||
Filters(Vue); | ||
getTypes, | ||
addType, | ||
addValidationMessage, | ||
install(Vue, options){ | ||
|
||
//install our components | ||
Components(Vue); | ||
Filters(Vue); | ||
|
||
Vue.$formly = {getTypes, addType}; | ||
} | ||
Vue.$formly = {getTypes, addType, addValidationMessage}; | ||
} | ||
}; | ||
|
||
//auto install | ||
if (typeof window !== 'undefined' && window.Vue) { | ||
window.Vue.use(Formly); | ||
//expose formly functions if auto installed | ||
window.Vue.$formly = {getTypes, addType}; | ||
window.Vue.use(Formly); | ||
//expose formly functions if auto installed | ||
window.Vue.$formly = {getTypes, addType, addValidationMessage}; | ||
} | ||
export default Formly; |
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
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 |
---|---|---|
@@ -1,47 +1,73 @@ | ||
import {expect} from 'chai'; | ||
|
||
import VueFormly from 'src/index'; | ||
import Util, {addType, getTypes, setError} from 'src/util'; | ||
import Util, {addType, getTypes, setError, addValidationMessage, parseValidationString} from 'src/util'; | ||
|
||
|
||
describe('module', () => { | ||
|
||
it('module props', () => { | ||
expect(VueFormly).to.exist; | ||
expect(VueFormly.install).to.be.a('function'); | ||
expect(VueFormly.addType).to.be.a('function'); | ||
expect(VueFormly.getTypes).to.be.a('function'); | ||
expect(addType).to.be.a('function'); | ||
expect(getTypes).to.be.a('function'); | ||
expect(setError).to.be.a('function'); | ||
expect(Util.formlyFields).to.be.a('object'); | ||
}); | ||
|
||
it('should add fields to Vue', () => { | ||
|
||
//mock vue | ||
window.Vue = { | ||
component(){}, | ||
filter(){} | ||
}; | ||
VueFormly.install(Vue); | ||
|
||
let newField = { | ||
foo: 'bar' | ||
}; | ||
addType('test', newField); | ||
expect(getTypes().formly_test).to.deep.equal(newField); | ||
}); | ||
|
||
it('should handle errors',()=>{ | ||
let form = { | ||
$errors: {} | ||
}; | ||
setError(form, 'fname', 'required', true); | ||
expect(form.$errors.fname.required).to.be.true; | ||
|
||
setError(form, 'fname', 'required', false); | ||
expect(form.$errors.fname.required).to.be.false; | ||
}); | ||
it('module props', () => { | ||
expect(VueFormly).to.exist; | ||
expect(VueFormly.install).to.be.a('function'); | ||
expect(VueFormly.addType).to.be.a('function'); | ||
expect(VueFormly.getTypes).to.be.a('function'); | ||
expect(VueFormly.addValidationMessage).to.be.a('function'); | ||
expect(addType).to.be.a('function'); | ||
expect(getTypes).to.be.a('function'); | ||
expect(setError).to.be.a('function'); | ||
expect(Util.formlyFields).to.be.a('object'); | ||
}); | ||
|
||
it('addType()', () => { | ||
|
||
//mock vue | ||
window.Vue = { | ||
component(){}, | ||
filter(){} | ||
}; | ||
VueFormly.install(Vue); | ||
|
||
let newField = { | ||
foo: 'bar' | ||
}; | ||
addType('test', newField); | ||
expect(getTypes().formly_test).to.deep.equal(newField); | ||
}); | ||
|
||
it('setError()',()=>{ | ||
let form = { | ||
$errors: {} | ||
}; | ||
setError(form, 'fname', 'required', true); | ||
expect(form.$errors.fname.required).to.be.true; | ||
|
||
setError(form, 'fname', 'required', false); | ||
expect(form.$errors.fname.required).to.be.false; | ||
|
||
setError(form, 'fname', 'required', true, 'Hey there'); | ||
expect(form.$errors.fname.required).to.equal('Hey there'); | ||
|
||
setError(form, 'fname', 'required', false, 'Hey there'); | ||
expect(form.$errors.fname.required).to.be.false; | ||
}); | ||
|
||
it('addValidationMessage()', () => { | ||
addValidationMessage('test', 'testing'); | ||
expect(Util.validationMessages.test).to.equal('testing'); | ||
}); | ||
|
||
it('parseValidationString()', () => { | ||
addValidationMessage('parseTest', '%l just %v testing'); | ||
let message = '%l just %v testing'; | ||
let expected = 'label just value testing'; | ||
let output = parseValidationString('parseTest', false, 'label', 'value'); | ||
expect(output).to.equal(expected); | ||
|
||
output = parseValidationString(false, message, 'label', 'value'); | ||
expect(output).to.equal(expected); | ||
|
||
output = parseValidationString('blahblahblah', false, '', ''); | ||
expect(output).to.be.false; | ||
}); | ||
|
||
}); |