Reputation: 2869
Does Rails 4 support all new HTML5 <input> attributes? I noticed that HTML5 <input> attributes such as min & max and step are supported in Rails 4:
<%= number_field(:product, :price, in: 1.0..20.0, step: 0.5) %>
which produces the following output:
<input id="product_price" max="20.0" min="1.0" name="product[price]"
step="0.5" type="number" />
But when it comes to new HTML5 <input> attributes such as autofocus and required:
<input type="text" name="fname" autofocus>
<input type="text" name="usrname" required>
then I haven't been able find any examples on how to apply those attributes in Rails 4. If anyone knows how to use autofocus and required attributes in Rails 4, then that info would be greatly appreciated as some of those new HTML5 <input> attributes would minimize the need for JavaScript in form validation in some cases.
Upvotes: 1
Views: 327
Reputation: 53038
Use this:
<%= number_field(:product, :price, in: 1.0..20.0, step: 0.5, required: true, autofocus: true) %>
Pass required
and autofocus
as options to the input fields with value true
.
Above will generate HTML element as below:
<input autofocus="autofocus" id="product_price" max="20.0" min="1.0" name="product[price]" required="required" step="0.5" type="number">
Upvotes: 1