Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formtastic, own :as input type

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 ?

like image 787
astropanic Avatar asked Apr 28 '10 17:04

astropanic


2 Answers

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 %>
like image 67
James A. Rosen Avatar answered Nov 17 '22 19:11

James A. Rosen


Don’t subclass Formtastic::FormBuilder anymore

It was previously recommended in Formtastic 1.x to subclass Formtastic::FormBuilder to add your own inputs. This is no longer recommended in Formtastic 2, and will not work as expected.

https://github.com/justinfrench/formtastic

http://justinfrench.com/notebook/formtastic-2-preview-custom-inputs

like image 37
Manuel Meurer Avatar answered Nov 17 '22 21:11

Manuel Meurer