Skip to content

Commit 6ee27c3

Browse files
Merge pull request #12 from balazsorban44/dev
Fix validation retrieval error
2 parents dbe2714 + e73dd94 commit 6ee27c3

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/__tests__/useForm.test.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ describe('handleSubmit validates', () => {
173173
setLoading: expect.any(Function),
174174
notify: expect.any(Function)
175175
})
176-
177176
})
178177

179178
it('validation should fail', () => {
@@ -187,6 +186,17 @@ describe('handleSubmit validates', () => {
187186
expect(form.onNotify).toBeCalledWith('validationErrors', ['input2'])
188187
})
189188

189+
it('when calculating validations from validators, should be able to access fields ', () => {
190+
const validators = jest.fn((fields, submitting) => ({
191+
input1: typeof fields.input1 === 'string',
192+
input2: !submitting
193+
}))
194+
render(<Component validators={validators}/>)
195+
fireEvent.click(screen.getByText(/submit/i))
196+
expect(form.onSubmit).not.toBeCalled()
197+
expect(validators).toHaveBeenNthCalledWith(1, form.initialState, false)
198+
expect(form.onNotify).toBeCalledWith('validationErrors', ['input2'])
199+
})
190200
})
191201

192202

src/submitHandler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default function submitHandler({
1313
name = options?.formName || name
1414

1515

16-
const validations = Object.keys(validators({}))
16+
const validations = Object.keys(validators(form, false))
1717

1818
if (process.env.NODE_ENV !== 'production') {
1919
const invalidValidators = validations.filter(validation =>

0 commit comments

Comments
 (0)