Validations

Nicholas
3 min readSep 30, 2020

When writing a form there are many different types of data that you can receive, however not all of the data you receive may be data that you actually want. Of course when creating a form you can specify the kind of data you want through methods such as number_field, text_field, and email_field. Furthermore validations, is the best way to set the parameters for the data that you want.

The there are numerous validation methods that can be used in order set up the parameter wanted, i will go over a couple validations that I use the most frequently.

validates :username, uniqueness: true

Uniqueness is a method that that is used in order to made that the input that is being entered into the form does not have any duplicates in the database.

validates :username, length: { minimum: 6}

Length is a validation method that can set the minimum and maximum length of a string. In. this case it is setting the minimum length of the username to be at least 6. In other cases length can also be used in when you would want to have a phone number entered and would require it to be 10 characters long.

In order to fully set up the data for validations you need to set up or adjust three files in your code the model file, the controller file and the new.html.erb or edit.html.erb file for the class that is going to be created or updated. In order to set up the two validations above we would put them into the model of the class we are editing.

class User < ApplicationRecordvalidates :username, length: { minimum: 6}validates :username, uniqueness: trueend

By adding these two validations to this class it will make sure that there are no duplicates of that username and it is at least 6 characters long. Not so fast this code will not work just yet. In order to get this code to work the create method in the user_controller must be updated in order to adjust to the fact that creating a new element for the database might fail now. Before learning validations the create method might have looked something like this.

def createuser = User.create(user_params)redirect_to user_path(user)end

In order to update the model putting in an if..else statement should solve the problem.

def createuser = User.create(user_params)if user.valid?redirect_to user_path(user)elseflash[:my_errors] = user.errors.full_messagesredirect_to new_user_pathendend

Using this code will allow the new element to be forwarded to the user show page if the data is invalid the create method will redirect you to the User new page and display the errors that occurred while trying to enter the information into the database. The flash method allows you to save that information into a variable that can be transferred to a different section of the file. In this situation, we are saving the errors that occurred while the data was trying to be saved into the database. By doing this it moves us into the final part of showing the user the errors that occurred. In this part we will have to edit the new page of the User model. In order to do this. We link the error to the html page in order to do this you would create an if statement in front of the form_for.

<% if flash[:my_errors] %><% flash[:my_errors].each do |error| %><p style="color:red;"><%= error %></p><% end %><% end %><%=form_for @User do |f|%><%=f.label :name%><%=f.text_field :name%><%=f.submit%><%end%>

If any of the data that was put into the database does not meet the criteria of the validations the page will reroute the to the page and new page and display the validations they did not meet for the form. If the criteria was met it will direct them to the show page of the newly created user.

--

--