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 ?
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 %>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With