开发者

Can't create Custom inputs for some (Text, Booleans, ...) types, with SimpleForm

开发者 https://www.devze.com 2023-04-04 23:31 出处:网络
I can\'t figure out why this is not working as it should - or - I\'m missing something important ? Here\'s the list of the mapped types from simple_form / lib / simple_form / form_b开发者_JAVA技巧uil

I can't figure out why this is not working as it should - or - I'm missing something important ?

Here's the list of the mapped types from simple_form / lib / simple_form / form_b开发者_JAVA技巧uilder.rb:

map_type :text,                                :to => SimpleForm::Inputs::TextInput
map_type :file,                                :to => SimpleForm::Inputs::FileInput
map_type :string, :email, :search, :tel, :url, :to => SimpleForm::Inputs::StringInput
map_type :password,                            :to => SimpleForm::Inputs::PasswordInput
map_type :integer, :decimal, :float,           :to => SimpleForm::Inputs::NumericInput
map_type :range,                               :to => SimpleForm::Inputs::RangeInput
map_type :select, :radio, :check_boxes,        :to => SimpleForm::Inputs::CollectionInput
map_type :date, :time, :datetime,              :to => SimpleForm::Inputs::DateTimeInput
map_type :country, :time_zone,                 :to => SimpleForm::Inputs::PriorityInput
map_type :boolean,                             :to => SimpleForm::Inputs::BooleanInput

First problem, I can extend StringInput class as:

#app/inputs/string_input.rb
class StringInput < SimpleForm::Inputs::StringInput
  def input
    if @builder.show?
      content_tag(:p, @builder.object[attribute_name], :class => :show)
    else
      super
    end
  end
end

(I've extended the builder with a show? method to detect the context: action == 'show')

This is working most of the time but not when :as => :string is present :

<%= f.input :estimated_duration_rendered, 
  :as => :string,
  :label => mt(:estimated_duration), 
  :hint => mt(:estimated_duration_hint),
  :error => false,
  :required => false,
  :input_html => { :class => :digits_11, :placeholder => mt(:estimated_duration_placeholder), :value => format_duration(resource.estimated_duration, true) }
%>

Second problem, I can create custom inputs for StringInput, DateTimeInput, CollectionInput and BooleanInput but all the others are not working. For example:

#app/inputs/text_input.rb
class TextInput < SimpleForm::Inputs::TextInput
  def input
    if @builder.show?
      "I will never show..."
    else
      super
    end
  end
end

Even if I have this helper in my form:

<%= f.input :description, 
            :label => mt(:description), 
            :hint => mt(:description_hint, :max => MyModel::DESC_MAX_LENGTH), 
            :input_html => { :class => :large, :rows => 8, :cols => 1, :maxlength => MyModel::DESC_MAX_LENGTH, :placeholder => mt(:description_placeholder) }, 
            :error => false
%>

Of course, description has a text data type.

What Am I doing wrong ?

Thank you.

Fro


Well, to use this functionality you just need the 1.5.1 version of SimpleForm. :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消