Disallow Submitting an Empty Form with React Final Form 'pristine' property

Tomasz Łakomy
InstructorTomasz Łakomy
Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated 6 years ago

In this lesson we are going to see how you can use pristine property from React Final Form render prop to ensure that users won't be able to submit (or reset) an empty form.

Instructor: [00:00] We have a form created with React Final Form. We have two fields, one for first name, the other one for second name. There are also two buttons, one for submit and the other for resetting the form.

[00:13] Right now, we are able to submit the form even though both first name and second name are empty, which doesn't make sense. We would like to be able only to submit the form whenever the user has entered their name or second name.

[00:28] In order to do that, there's actually pristine property from React Final Form. The pristine property is a Boolean value which is true whenever the form is completely empty. It's false when at least something has been written, if the form has been touched in any way.

[00:48] We can use it to disable the submit button whenever pristine is set to true, so whenever there is nothing inside of the form. Right now, the submit button is disabled. We can do exactly the same for the reset button.

[01:06] This is a desired effect. Right now, I cannot submit or reset the form. If I put in my name and my second name, I can submit the form, and I can also reset it.

egghead
egghead

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today