Valid?
Tags: rails, webdev, beginners, programming
Published: 2023-02-05 21:34:25


**This is what your Database looks like without Validations!**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/odfnn1g51d3govnoqi8c.png)




Validations are extremely important when creating a web application, as a developer you want to make sure that the information that is being input is correct before it is saved. There are several instances that could harm an application if data was input incorrectly in a database. For example, if users had the ability to create accounts you would want to make sure that everyone had a different username or that their password wasn't just 1234, **or that they even entered in a password at all!** It is important to protect your database so your application works at its best but you also want to make sure you are protecting those using the application as well. Luckily Rails provides us with several validations that make sure our database is getting the correct data saved.

**Listed below are some of the built-in validators that rails has:**

**Confirmation**- This validation confirms that text inputs in separate fields are the exact match. An example of this would be the confirmation of a User’s phone number, if you application depends heavily on having this point of contact it is best that you make sure that the user is providing the correct number.
**Format**- This validation confirms that the input matches a given regular expression. An example of this would be making sure a User can only use upper and lower case letters in their name. This can be important if you don't want any unnecessary symbols in the name of the user ie: !@#$%^. 
**Length**- This validation confirms that the given length is met whether it is more or less. An example of this would be password creation. In today's climate, there is no reason that a Password should be less than 5 characters and even that could be considered low. 
**Presence**- This validation confirms that there is something being passed through to the given attribute's values. An example of this is making sure that all information when creating a profile is complete. I mean imagine if a User could create a profile without even having a password this would cause major disruption in an application .
**Uniqueness**- This validation checks and attribute against all other instance that has been saved and makes sure that it is the only one of its kind. An example of this would be making sure that a User only uses an email for one account.
**Others**- Validates With, Comparison, Exclusion, Numericality, Inclusion, Abensense, Acceptance. Validates Associated


Implementing Validations in Rails

Implementing validations in Rails is easy. The first step is to define the validator in the model. An example of that would be using the Presence built-in validator on a User model to make sure that when created it has both a Username and Password.

```
class User < ApplicationRecord
  validates :username, presence: true, uniqueness: true
  validates :password, presence: true
end
```

When the user tries to save the new record, Rails will automatically run the validation defined in the model and prevent the record from being saved if any of the validations fail.

It also returns an error message as well that can be returned on the client side for users to know what they did wrong and you can customize as well per validation 

```
class User < ApplicationRecord
  validates :username, presence: { message: "Username can't be blank" }, uniqueness: { message: "Username has already been taken" }
  validates :password, presence: { message: "Password can't be blank" }
end
```

Best Practices for Validations

Data consistency: Validations are important for assuring that data in the database is consistent and reliable. This will help prevent any issues from arising.

Keep it simple: Validations should be simple and easy to understand so that developers can quickly determine what they are checking for and why.

In conclusion this is just scratching the surface of using validations in rails and how it can protect your database from bad data!