foxhound87 / mobx-react-form

Reactive MobX Form State Management
https://foxhound87.github.io/mobx-react-form
MIT License
1.09k stars 129 forks source link

Field.invalidate() and validate for field with nested field not blocked form submit #519

Closed Desteel closed 1 year ago

Desteel commented 5 years ago

Hello. I use structure

mainField
 - nestedAnswerField
 - nestedCommentField

And i need validate mainField. I set validation for mainField, i get hasError as true and error message, but form can submit and form.errors not display that errors. And if i use Field.invalidate(), i also get hasError as true and error message, but form can submit.

I also tested this with your example from docs

export function isEmail({ field, validator }) {
  if (validator.isEmail(field.value)) return true;
  return field.invalidate(`The ${field.label} should be an email address.`);
}
foxhound87 commented 5 years ago

can you provide a codesandbox please?

Desteel commented 5 years ago

can you provide a codesandbox please?

Sandbox

Thank you.

foxhound87 commented 5 years ago

Thank you for reporting this issue.

for now you can use this workaround to make it work until I fix it:

const weekValidator = ({ field, form }) => {
  const hasAnswer = Object.keys(field.value).length;

  console.log('week hasAnswer ', hasAnswer);

  if (!hasAnswer) form.invalidate(); // <---

  return [hasAnswer, 'Error message: the week is empty, but form can submit'];
};
Desteel commented 5 years ago

Thank you!

github-actions[bot] commented 1 year ago

:tada: This issue has been resolved in version 5.10.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

foxhound87 commented 1 year ago

@Desteel updated sandbox: https://codesandbox.io/s/mobx-react-form-issue-519-forked-e29qi9 now should work as expected

Note: retrieveOnlyEnabledFields has been renamed to retrieveOnlyEnabledFieldsValues in next releases.

github-actions[bot] commented 1 year ago

:tada: This issue has been resolved in version 5.10.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket: