views:

284

answers:

1

How I can add my own field types to formtastic ?

For exemple, I need to have a custom datetime input, and I want something like this:

<%= f.input :start_date , :as => :my_date %>

This obviously doesn't work because formtastic doesn't know the :my_date (only :boolean, :string, :datetime and so on...)

But how can I add additional input types ?

+7  A: 

You need to add a custom input method:

class MyCustomFormtasticFormBuilder < Formtastic::SemanticFormBuilder
  protected
  def my_date_input(method, options)
    basic_input_helper(:text_field, :my_date, method, options)
  end
end

That's perfect for, say the new HTML5 input types. You use it like so:

<% form_form @model, :builder => MyCustomFormtasticFormBuilder  do |f| %>
   <%= f.input :start_date, :as => :my_date
<% end %>
James A. Rosen
Perfect answer. My only addition is that you then need to configure Formtastic to us MyCustomFormtasticFormBuilder instead of Formtastic::SemanticFormBuilder, which can be done in the config initializer supplied with Formtastic.
Justin French