After jumping the wagon with Twitter Bootstrap for a new client app we are working on, I found myself in to some problem trying to implement this type of input:

We only have one input let’s say name and we wan’t to validate it’s presence when the check is true. The check is a virtual attribute named name_check. So using simple_form gem for this form.
We need to create the custom input element first, we do this by creating an inputs folder under the app folder. We name our file prepend_checkbox_input.rb
class PrependCheckboxInput < SimpleForm::Inputs::Base
def input
"<div class='input-prepend'>
<label class='add-on active' title='#{options[:tooltip]}' rel='tooltip' >
#{@builder.check_box(attribute_name.to_s + '_check')}
</label>
#{@builder.text_field(attribute_name, input_html_options)}
<span class='help-inline'>
#{options[:help]}
</span>
</div>".html_safe
end
end
The input will include an option for tooltip, it will automatically append ‘_check’ to the name of the input to create the check box element, the text field with all the regular HTML options and a help line under it.
This will be how we call it under our views:
<%= f.input :name, :as => :prepend_checkbox, :tooltip => "Has name?", :help => "Please insert your name" %>
Now we include validations, for custom validators we need implement this one under our initializers:
ActiveRecord::Base.class_eval do
def self.validates_checkbox(*attrs)
attrs.each do |attr_name|
validates attr_name, :presence => true, :if => "#{attr_name.to_s + '_check'} == '1'"
end
end
end
We user
*attrs as our input so that we can declare our validations on a single line like so:
validates_checkbox :name, :phone, :number