HTML5 'required' validation in Ruby on Rails forms HTML5 'required' validation in Ruby on Rails forms ruby-on-rails ruby-on-rails

HTML5 'required' validation in Ruby on Rails forms


Ah, it was easy :required => true

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


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}' %>

:)


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