Skip to content

Commit c7da8f0

Browse files
author
Martynas Žilinskas
committed
Added SetFieldsTouched() method in FormStore.
1 parent af6061f commit c7da8f0

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
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.SetFieldsTouched();
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/stores/form-store.ts

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

382+
public SetFieldsTouched(fieldsIds?: string[]): void {
383+
this.State = this.State.withMutations(state => {
384+
if (fieldsIds == null) {
385+
fieldsIds = state.Fields.keySeq().toArray();
386+
}
387+
388+
fieldsIds.forEach(fieldId => {
389+
const fieldState = state.Fields.get(fieldId);
390+
391+
if (fieldState != null) {
392+
state.Fields = state.Fields.set(fieldId, fieldState.merge({
393+
Touched: true
394+
} as FieldStoreState));
395+
}
396+
});
397+
398+
return this.RecalculateDependentFormStatuses(state);
399+
});
400+
}
401+
382402
public async ValidateForm(validationPromise: Promise<never>): Promise<void> {
383403
const form = this.State.Form;
384404

0 commit comments

Comments
 (0)