Search code examples
ruby-on-railshamlsimple-form-for

Bind '.error' class to manually created haml wrapper


I have next group of controls:

.control-group.string.required.user_name
  = f.input_field :name, required: true, class: 'form-control'
  = f.label 'User Name'
  = f.error :name

Is there any way to bind '.control-group' to it's child input, so if input is invalid, wrapper will get class ".error" added?

Thanks


Solution

  • I've created a custom component for that, which looks as follows

    config.wrappers :perfect_forms, tag: 'div', class: 'control-group', required: true, error_class: 'error' do |b|
      b.use :html5
      b.use :input, class: 'fotm-control', required: true
      b.use :label
      b.use :error, wrap_with: { tag: 'span', class: 'help-inline' }
    end
    

    Now there is a question, if it's possible to detect type of an input inside this config? Because currently to explicitly set a type of an input, I'm writing "input_html: { type: 'text'}" for needed input, but that would be awesome to handle this case inside a wrapper config. Like if type is 'email' - then insert type 'text'.