HTML5 'required' validation in Ruby on Rails forms

Ruby on-RailsHtmlValidation

Ruby on-Rails Problem Overview


I can't see this question anywhere else, it's hopefully a quick and easy one.

How can I use HTML5 validators, such as 'required', in my forms (ruby on rails)?

Eg, How would this basic form look if I used HTML5 validation in it?

<%=form_for @testimonial do |t|%> 
<dl>
  <dt><label for="testimonial_rating">Rating</label></dt>
  <dd><%=t.select :rating, Testimonial.ratings%></dd>
  <dt><label for="testimonial_content">Comments</label></dt> 
  <dd><%=t.text_area :content, :rows => 3%></dd>
  <dd><button class="button success">Submit Review</button></dd>
</dl>
<%end%>

It goes without saying that server side validation is still required.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Ah, it was easy :required => true

eg: <%=t.text_area :content, :rows => 3, :required => true%>

Solution 2 - Ruby on-Rails

Just to add on, if you have an email field, you can also use 'pattern' attribute to validate the format of email

<%=form.text_field :email, :required => true, :pattern => '[^@]+@[^@]+\.[a-zA-Z]{2,6}' %>

:)

Solution 3 - Ruby on-Rails

Addition to @prashantsahni answer. You can also use type = 'email' instead of regex pattern, then your erb-template will look like this:

<%= f.email_field :email, id: 'user_email', type:'email', required: true, placeholder: "Email" %>  

More info about form validations using html5

Solution 4 - Ruby on-Rails

This is a little example with the common attributes and for required you only add required:true, but dont forget apply this validations in your backend.

<%= f.text_field
    id: "yourID",
    class: "yourCLass",
    placeholder: "Your message",
    maxlength: 14,
    required: true
%>

Solution 5 - Ruby on-Rails

New Syntax <%= f.text_field :email, class: "form-control", required: true %>

Solution 6 - Ruby on-Rails

This could be easily done by adding :required => true parameter into your input fields:

For example

f.text_field :first_name, :required => true
text_field_tag :first_name, nil, :required => true

Pushing the boundary abit further, you could add in pattern matcher for your input, such as email:

f.email_field :email, 'Email', :required => true, :pattern => '[^@]+@[^@]+\.[a-zA-Z]{2,6}'

Solution 7 - Ruby on-Rails

For completing other answers, there is an awesome gem html5_validations which makes the most of the HTML5 validations reading from ActiveRecord Validations from the model. No extra code needed, just installing it.

Solution 8 - Ruby on-Rails

Use this if nothing works

include_blank: false, required: true

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLpLrichView Question on Stackoverflow
Solution 1 - Ruby on-RailsLpLrichView Answer on Stackoverflow
Solution 2 - Ruby on-RailsprashantsahniView Answer on Stackoverflow
Solution 3 - Ruby on-RailsDmitry DavydovView Answer on Stackoverflow
Solution 4 - Ruby on-RailsEzequiel GarcíaView Answer on Stackoverflow
Solution 5 - Ruby on-RailsgsumkView Answer on Stackoverflow
Solution 6 - Ruby on-RailsTrung LêView Answer on Stackoverflow
Solution 7 - Ruby on-RailsAlex CastañoView Answer on Stackoverflow
Solution 8 - Ruby on-RailsBilal A.AwanView Answer on Stackoverflow