Rails Validations: how I used them on my app.

Christian Bermeo
3 min readNov 8, 2021

--

It’s important to add validations to any project. there are multiple forms of validations that you can add, one of the most basic validation is if you want to type an email to log in, but instead of typing the email on the input it's typed a random name or a bunch of numbers, the request will not go through.

Why validations are handy and important to use? using validations we can ensure the correct use of the app, avoiding incorrect information typed on the input fields preventing fraudulent data. Another example is that if you need to type some information, and one of those fields needs to be filled with your phone number, to finish the form you are required to add a number with 10 digits, but instead of typing the required length of the number you type less than that. this data is considered invalid and the information is not going to be submitted, this is exactly the type of data that we want to avoid.

There are a lot of kinds of validations that we can use. from the uniqueness of the name, case_sensitive when filling the data, and length of the information typed. If you want to find the list of validations and other types of validations and error handling, you can check this documentation https://guides.rubyonrails.org/active_record_validations.html#validation-helpers

In the User model, you can add all the validations that you are going to use. You can find this file inside the model's folder with the name of the model that you're created.

to validate and display errors, we will need to create methods to display those errors, we can do it on the application controller file.

the next step is to our js file to render those errors by iterating over the errors.

Finally, you're going to check those errors on the browser when you type the incorrect information.

--

--

Christian Bermeo
0 Followers

Software Engineer Student at Flatiron School