user142019
user142019

Reputation:

Type attribute for text_field

I have this code:

<%= f.text_field :email, :type => "email", :placeholder => "[email protected]" %>

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="[email protected]" size="30" type="text" />

which should be:

<input id="user_email" name="user[email]" placeholder="[email protected]" 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

Upvotes: 3

Views: 2684

Answers (2)

Martin Svalin
Martin Svalin

Reputation: 2267

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

Upvotes: 4

Russell
Russell

Reputation: 390

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

Upvotes: 4

Related Questions