Skip to content

Commit ae8b7e6

Browse files
authored
only accepts exist value (#40)
1 parent 578fe5b commit ae8b7e6

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

src/useForm.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,10 @@ export class FormStore {
431431
nameList?: NamePath[],
432432
options?: ValidateOptions,
433433
) => {
434-
const namePathList: InternalNamePath[] | undefined = nameList && nameList.map(getNamePath);
434+
const provideNameList = !!nameList;
435+
const namePathList: InternalNamePath[] | undefined = provideNameList
436+
? nameList.map(getNamePath)
437+
: [];
435438

436439
// Collect result in promise list
437440
const promiseList: Promise<{
@@ -440,13 +443,20 @@ export class FormStore {
440443
}>[] = [];
441444

442445
this.getFieldEntities().forEach((field: FieldEntity) => {
446+
// Add field if not provide `nameList`
447+
if (!provideNameList) {
448+
namePathList.push(field.getNamePath());
449+
}
450+
451+
// Skip if without rule
443452
if (!field.props.rules || !field.props.rules.length) {
444453
return;
445454
}
446455

447456
const fieldNamePath = field.getNamePath();
448457

449-
if (!namePathList || containsNamePath(namePathList, fieldNamePath)) {
458+
// Add field validate rule in to promise list
459+
if (!provideNameList || containsNamePath(namePathList, fieldNamePath)) {
450460
const promise = field.validateRules({
451461
validateMessages: {
452462
...defaultValidateMessages,
@@ -485,15 +495,15 @@ export class FormStore {
485495
.then(
486496
(): Promise<Store | string[]> => {
487497
if (this.lastValidatePromise === summaryPromise) {
488-
return Promise.resolve(this.store);
498+
return Promise.resolve(this.getFieldsValue(namePathList));
489499
}
490500
return Promise.reject<string[]>([]);
491501
},
492502
)
493503
.catch((results: { name: InternalNamePath; errors: string[] }[]) => {
494504
const errorList = results.filter(result => result && result.errors.length);
495505
return Promise.reject({
496-
values: this.store,
506+
values: this.getFieldsValue(namePathList),
497507
errorFields: errorList,
498508
outOfDate: this.lastValidatePromise !== summaryPromise,
499509
});

tests/validate.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,5 +271,36 @@ describe('Form.Validate', () => {
271271
expect(form.getFieldValue('title')).toBe('1');
272272
expect(form.getFieldError('title')).toEqual(['Title should be 3+ characters']);
273273
});
274+
275+
it('validate only accept exist fields', async () => {
276+
let form;
277+
const onFinish = jest.fn();
278+
279+
const wrapper = mount(
280+
<div>
281+
<Form
282+
onFinish={onFinish}
283+
ref={instance => {
284+
form = instance;
285+
}}
286+
initialValues={{ user: 'light', pass: 'bamboo' }}
287+
>
288+
<InfoField name="user">
289+
<Input />
290+
</InfoField>
291+
<button type="submit">submit</button>
292+
</Form>
293+
</div>,
294+
);
295+
296+
// Validate callback
297+
expect(await form.validateFields(['user'])).toEqual({ user: 'light' });
298+
expect(await form.validateFields()).toEqual({ user: 'light' });
299+
300+
// Submit callback
301+
wrapper.find('button').simulate('submit');
302+
await timeout();
303+
expect(onFinish).toHaveBeenCalledWith({ user: 'light' });
304+
});
274305
});
275306
/* eslint-enable no-template-curly-in-string */

0 commit comments

Comments
 (0)