pihentagy asked:

Form helpers

Form controls exists. I’d like to know how?

Can I ask nitro just to render a checkbox, an input field, whatever? (of course without messy html…)

(2 attempts)

Kashia answered:

They are actually impractical to use, if you just want to render a single form field.

# This gets the object of which you want the control
obj = Foo[1]

# this gets the first Property of that objects class
prop = obj.class.properties.values[1]

# this gets a Property with known :symbol
prop = obj.class.properties[:update_time]

# this creates a new Property with given values
prop = Property.new({:control=>:textarea, :klass=>Time, :symbol=>:update_time})
# you can leave out the :control =>, the standard will be used

# this will create a raw control
@formcon =  Form::Control.fetch(obj, prop, options).render

# this creates a structure around the form control 
control = Form::Control.fetch(obj, prop, options).render
@formcon = FormHelper::FormBuilder.element(prop, control)

Use in the template like this:

<!-- <form .. -->
  #{@formcon}
<!-- </form> -->

Rating: 5

manveru answered:

Well, based on the Hints Kashia gave, i've made this littly nifty method for you controller:

# options:
# :properties => [:update_time, :creation_time]
def control_for(obj, *options)
  options = options.first
  props = options[:properties].map do |prop|
    obj.class.properties[prop]
  end
  @props = props.map do |prop|
    control = Nitro::Form::Control.fetch(obj, prop, options).render
    Nitro::FormHelper::FormBuilder.element(prop, control)
  end
end

you can call this in your template with this:

#{control_for User[0], :properties => [:nick, :level, :oid]}

but you still have to do a

tag around it and submit/cancel buttons, maybe we can automate that as well, but it's no big deal anyway...

Hope that helps :)

Rating: 5