The Who, What, Where, When, and Why on Validations

How to use custom and pre-built validations in your Rails projects

What are Validations?

Validations are a great tool for Rails developers to use when creating projects that include data that needs to be, well, validated! Okay, cool. So why should we use them? They’re mainly used for ensuring the accuracy of specific types of data input (full names, email addresses, usernames, passwords, user IDs, etc), and predominately doing so checking the accuracy of this data input before it is ultimately persisted into the database. In other words, a validation will run before the object is fully persisted into the system, to protect us from saving inconsistent data. We can check for presence of a certain attribute, the uniqueness (ex: do two users have the same username?), length (ex: length of a username/password), and the list continues. We have built in validation checkers, but we can also build custom validations that can go beyond the scope of what Active Record conveniently gives us (but more on that later).

Hopefully with the above explanation, you can quickly realize why the presence of validations are as useful as they are important. So, who can use validations? Anybody who is creating a Rails app! More importantly, anybody who knows how to implement them properly. Here we will discuss in detail on how to use built in validation methods that exist through Active Record, or how to write a custom method based on the data that you are attempting to validate.

This link will give you a comprehensive list of the built in methods that are available to you, but for the sake of time and this article, we will only be using a couple to showcase how to use them. For example, let’s say that we are creating a social media app, and a user cannot create a profile without a name, a city, or an email address. Rails has a built in method called “presence” that will check the presence of these values within your forms.

Where you place the validation is up to what you want to validate, and what class the attributes you want to validate are located. For our example, in the User class we will simply type “validates”, along with the attributes, and set the presence to “true”. Simple enough! In the terminal, you can check the validity of the instance you just created by tacking .valid? at the end of the user. But the average user of our app will not know how to open up the terminal and complete the same process we just did. So how do we show the user if there are any errors?

In the create method in your UserController, you can write a conditional statement that basically checks the validity, otherwise it will flash error messages to the user that something is not quite correct. The “error.full_messages” are helper methods given to us by Active Record, and will relay to the user the errors that arise.

Wherever a form input is invalid (or valid), you should redirect it to whatever you believe the happy path should be for the best user experience. In this case, we redirect to the new user path to refill the form, now that the user is aware of their errors!

But how do I write a custom validation?

Very simply! Active Record is very smart and does a lot of work for us, but there will be times where we want something specific to the project we are working on. First, we want to identify what we want to our custom validation to be. Let’s assume that I want to be the only “Sara” on this app. There can be Sarahs, but not another “Sara”! Before we take it back to step 1, we must write the method that we will ultimately use in the validation. In the User class, we will write a method containing a conditional that basically says that if we input the name “Sara”, you will see an error message that says “Name can’t be Sara.” Let’s check it out.

Above, we were also able to set a custom message that will appear if someone tries to set their name as Sara! Now we will follow the same steps as the original validations we discussed. However, instead of us using “validates”, we will use “validate”, followed by the method that we just created.

… that’s pretty much it! You can use an endless amount of validations on your application, and they can be customized to whatever suits your application the best. They ensure the accuracy of data that is being entered, and assist with the organization of that data, as well.

Resources : Great resource for all your validation inquiries. : Carbon Code Snippets — what I used in order to get the code snippets you see above.

Flatiron School alumni with a sociology and neuroscience background.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store