简体   繁体   中英

Type attribute for text_field

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 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

实际上,在Rails 3中有为此目的的表单助手:telephone_field和email_field就像text_field一样工作,但是按照你的预期设置它们的类型属性。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM