开发者

Type attribute for text_field

开发者 https://www.devze.com 2022-12-25 05:24 出处:网络
I have this code: <%= f.text_field :email, :type => \"email\", :placeholder => \"john@example.com\" %>

I have this code:

<%= f.text_field :email, :type => "email", :placeholder => "john@example.com" %>

So people can enter their email on an iPhone with the email keyboard instead of the开发者_StackOverflow社区 ASCII keyboard. However, the output is:

<input id="user_email" name="user[email]" placeholder="john@example.com" size="30" type="text" />

which should be:

<input id="user_email" name="user[email]" placeholder="john@example.com" size="30" type="email" />

I also want this for the tel type, so people get the telephone number keyboard.

Is there a way to force Rails to use the email type instead of text, or must I use HTML directly? Thanks


It is not possible to do this with the current helpers (http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html) "Returns an input tag of the "text" type tailored for accessing a specified attribute..."

I'd write a helper to keep your code nicer. You could just write something with the HTML and options you need, or you could add something to work with FormBuilder like the snippet below.

module ActionView
  module Helpers
    class FormBuilder
      def tel_field(method, options = {})
        InstanceTag.new(@object_name, method, self, options.delete(:object)).to_input_field_tag("tel", options)
      end
    end
  end
end


Actually, in Rails 3 there are form helpers for this purpose: telephone_field and email_field work just like text_field, but set their type attributes as you would expect..

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号