Skip to content

Commit d6312bf

Browse files
Set fields touched before calling submit callback (#62)
* Added TouchFields() method in FormStore. * Updated actions. Added emit action in method TouchFields.
2 parents af6061f + 69a2ef4 commit d6312bf

File tree

3 files changed

+41
-36
lines changed

3 files changed

+41
-36
lines changed

packages/simplr-forms-dom/src/components/form.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ export class Form extends BaseForm<FormProps, {}> {
2727
return;
2828
}
2929

30-
// TODO: Touch all fields to validate
30+
this.FormStore.TouchFields();
3131

3232
if (this.props.onSubmit == null) {
3333
return;
3434
}
3535

36-
// Persist synthetic event, because it's passed into another method
36+
// Persist synthetic event, because it's passed into another method.
3737
event.persist();
3838

39-
// Pass onSubmit result to FormStore for further processing
39+
// Pass onSubmit result to FormStore for further processing.
4040
const result = this.props.onSubmit(event, this.FormStore);
4141
this.FormStore.SubmitForm(result);
4242
}

packages/simplr-forms/src/actions/form-store.ts

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@ export abstract class FormAction {
1010
}
1111
}
1212

13-
export class StateChanged extends FormAction { }
14-
15-
export class FieldRegistered extends FormAction {
16-
constructor(protected formId: string, private fieldId: string) {
13+
export abstract class FieldAction extends FormAction {
14+
constructor(formId: string, protected fieldId: string) {
1715
super(formId);
1816
}
1917

20-
public get FieldId(): string {
18+
public get FieldId() {
2119
return this.fieldId;
2220
}
2321
}
2422

23+
export class StateChanged extends FormAction { }
24+
25+
export class FieldRegistered extends FieldAction { }
26+
2527
export class FieldsGroupRegistered extends FormAction {
2628
constructor(protected formId: string, private fieldsGroupId: string) {
2729
super(formId);
@@ -42,36 +44,14 @@ export class FieldsArrayRegistered extends FormAction {
4244
}
4345
}
4446

45-
export class ValueChanged extends FormAction {
46-
constructor(protected formId: string, private fieldId: string) {
47-
super(formId);
48-
}
49-
50-
public get FieldId(): string {
51-
return this.fieldId;
52-
}
53-
}
54-
55-
export class FieldPropsChanged extends FormAction {
56-
constructor(protected formId: string, private fieldId: string) {
57-
super(formId);
58-
}
47+
export class ValueChanged extends FieldAction { }
5948

60-
public get FieldId(): string {
61-
return this.fieldId;
62-
}
63-
}
49+
export class FieldPropsChanged extends FieldAction { }
6450

6551
export class FormPropsChanged extends FormAction { }
6652

67-
export class FormDisabled extends FormAction {
68-
constructor(protected formId: string) {
69-
super(formId);
70-
}
71-
}
53+
export class FormDisabled extends FormAction { }
7254

73-
export class FormEnabled extends FormAction {
74-
constructor(protected formId: string) {
75-
super(formId);
76-
}
77-
}
55+
export class FormEnabled extends FormAction { }
56+
57+
export class FieldTouched extends FieldAction { }

packages/simplr-forms/src/stores/form-store.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,31 @@ export class FormStore extends ActionEmitter {
379379
}
380380
}
381381

382+
public TouchFields(fieldsIds?: string[]): void {
383+
if (fieldsIds == null) {
384+
fieldsIds = this.state.Fields.keySeq().toArray();
385+
}
386+
387+
this.State = this.State.withMutations(state => {
388+
389+
fieldsIds!.forEach(fieldId => {
390+
const fieldState = state.Fields.get(fieldId);
391+
392+
if (fieldState != null) {
393+
state.Fields = state.Fields.set(fieldId, fieldState.merge({
394+
Touched: true
395+
} as FieldStoreState));
396+
}
397+
});
398+
399+
return this.RecalculateDependentFormStatuses(state);
400+
});
401+
402+
fieldsIds.forEach(fieldId => {
403+
this.emit(new Actions.FieldTouched(this.FormId, fieldId));
404+
});
405+
}
406+
382407
public async ValidateForm(validationPromise: Promise<never>): Promise<void> {
383408
const form = this.State.Form;
384409

0 commit comments

Comments
 (0)