rubymonolith / superform

Build highly customizable forms in Rails
MIT License
281 stars 15 forks source link

README customization code does not work #20

Closed macklinu closed 7 months ago

macklinu commented 7 months ago

Hello, I am trying to get started with Superform and Phlex, but the example in the Customization section is not working with superform v0.4.5. I'd love to figure out what is needed here, because being able to override different form component as mentioned in the superform README styles for usage with Tailwind makes much more sense to me conceptually than messing around with Rails FormBuilder.

Steps to reproduce:

<%= render Users::Form.new @user %>


<%= link_to "Back to users", users_path %>
- `rails s`
- Visit http://localhost:3000/users/new

**Expected**: `Users::Form` renders
**Actual**: Rendering error

ActionView::Template::Error (wrong number of arguments (given 2, expected 0)): 1:

New user

2: 3: <%= render Users::Form.new @user %> 4: 5:
6:

app/views/forms/application_form.rb:15:in input' app/views/users/form.rb:3:intemplate' app/views/users/new.html.erb:3


![image](https://github.com/rubymonolith/superform/assets/2344137/e884582b-9332-4baa-ba1d-d6743f5ce1ea)
macklinu commented 7 months ago

This is what I've been able to get working. The only thing I'm not sure about is where errors? and errors come from.

class ApplicationForm < Superform::Rails::Form
  class MyInputComponent < Superform::Rails::Components::InputComponent
    def template(&)
      div class: "form-field" do
        input(**attributes)
        # if field.errors?
        #   p(class: "form-field-error") { field.errors.to_sentence }
        # end
      end
    end
  end

  class Field < Superform::Rails::Form::Field
    def input(**attributes)
      MyInputComponent.new(self, attributes:)
    end
  end

  def labeled(component)
    div class: "form-row" do
      render component.field.label
      render component
    end
  end

  def submit(text)
    button(type: :submit) { text }
  end
end

If I uncomment the line with field.errors?, I get an undefined method errors? error.