How to create custom forms with validation and scroll to invalid logic in React Native (Part one: Validation)
When we started our first rewrite into React Native in late 2017, we knew our app was going to have quite a few forms that our providers would need to fill out — especially when first onboarding onto our platform.
On the web, we were pretty familiar with a variety of the web APIs and libraries out there that offer form validation and an ability to scroll to the first invalid input—something that we insisted on having due to the existence of a few long forms — but with React Native there wasn’t much available at the time, which is how we ended up rolling our own.
Over these next few posts, we’ll go over how we built our logic, some of the caveats to be aware of in this approach, and finally some of the improvements that can be made upon it.
If you want to jump ahead and see the demo app that this tutorial created, you can do so here.
The first step in setting up Validate.js is to create a dictionary of all the different unique types of input fields you’ll have and the validation rules for each. Reading though the documentation on this will definitely help — but here’s an example of some of our rules to give you some context:
Now that we have our dictionary setup, let’s look at what the local state on one of our forms will look like.
As you can see, we’ve nested all of our inputs within an inputs object on our state to keep it organized. Then, each individual input has its own object with two key pieces nested within it. First is the type, which is a string representation linked to the type of validation we want to perform on the input. Next is the value, which is where the input’s value will be stored. Also note that the key of each input is uniquely tied to the input in reference.
Next, let’s create some helper methods that we’ll utilize to validate our inputs. For ease of explaining how these helper methods will work — imagine we’re validating an input defined as day which permits integers from 1 to 31 and the user has just typed 32 into the input.
First, we’ll setup the main helper that’s going to be validating our input called validateInput. This method will take two parameters, type (‘day’) and value (32). Within this method, we immediately call Validate.js with two parameters — the first is an object of our type and value, and the second is an object of the validation we want to check against from our dictionary (make sure your validationDictionary exists for this helper to reference). If that call produces a result — which in this case it would because our dictionary enforces numbers between 1 and 31, the error of “Must be valid” will be returned.
Next, we’ll create a helper method called getInputValidationState. This method will be updating the state.inputs.day object by first spreading out all of the existing parameters, and then explicitly updating the errorLabel parameter (as long as the input hasn’t been flagged as optional) by calling the validateInput method we just created.
Finally, we need a helper method that we’ll link directly into the change event on our input (i.e the onChangeText prop on a TextInput). This method will not only update state.inputs.day.value, but also actually perform the validation and return all information in regards to it. To do this, we’ll create an onInputChange helper that will take three parameters. First, an id that is a string representation of the input’s unique key — ‘day’ in this instance. Next, the value (32) of the input. And finally, an optional callback method. We will need to bind this method to our constructor so it has access to state, which will allow us to pull the entire object for that specific input off.
Now that we’ve got our helpers setup, let’s go back to the setup of our form. The beauty of the service we just created is that it can validate any structure of data as long as your dictionary is setup to parse it (again, refer to the documentation for more assistance with complex rules). But for the sake of simplicity, I’ll validate a few TextInput components using our onInputChange method.
Here’s a snippet of what our form looks like when it’s all set up. Essentially, we’ve got four inputs that all have validation checks, and a helpful renderError method that looks to see if the errorLabel key on any of our individual inputs is truthy. If it is, we render the error.
Great, now that we have individual inputs validating —but what about if we wanted to validate the entire form all at once? Let’s create a new helper method called getFormValidation to do that.
For this helper method, we’re going to iterate over the entire inputs object and run a validation check on each input. As we run this validation, we rebuild the inputs object and then set it to state. Make sure you bind this one to the constructor too, then throw a button on your form and test calling this method.
Now our form is setup with our base validation logic.
That’s all for this first part. In part two, we’ll go over setting up “scroll to invalid” logic so we can ensure that if we have a long form and our user forgets to enter in a field on the top input — we bring them right to that point on the form.