An open API service indexing awesome lists of open source software.

https://github.com/mjc-gh/form_buildable

Declaratively build FormBuilder classes in Rails. Goes great with TailwindCSS!
https://github.com/mjc-gh/form_buildable

Last synced: 29 days ago
JSON representation

Declaratively build FormBuilder classes in Rails. Goes great with TailwindCSS!

Awesome Lists containing this project

README

          

# FormBuildable

[![FormBuildable](https://github.com/mjc-gh/form_buildable/actions/workflows/actions.yml/badge.svg)](https://github.com/mjc-gh/form_buildable/actions/workflows/actions.yml)

Declaratively build
[FormBuilder](https://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html)
classes in Rails. Goes great with TailwindCSS!

## Usage

```ruby
module ApplicationHelper
include FormBuildable

define_form_builder :default do
html_classes do
button {
"inline-flex items-center justify-center rounded border shadow-sm font-medium px-4 py-2 text-sm "\
" border-transparent text-white bg-blue-600 md:text-base lg:text-lg hover:bg-blue-800 "
}

form { "space-y-4" }
field { "flex flex-col gap-2" }

check_box_input { "w-6 h-6 text-blue-600 bg-gray-100 border-gray-300 rounded" }
check_box { "flex gap-2 items-center" }
check_box_label { "text-md" }

input { "rounded-lg border-slate-300 p-2 text-slate-900 " }

error_container { "flex justify-start" }
error { "text-sm text-red-700 font-bold whitespace-pre" }
end
end

define_form_builder :inline do
# some other classes...
end
end
```

Now use in a view with `default_form_with` or `default_form_for`

```erb
<%= default_form_with model: @user, url: registrations_path do |form| -%>
<%= form.email_field :email -%>
<%= form.check_box :accepted_terms -%>
<%= form.button -%>
<% end -%>
```

Will produce some HTML like:

```html


Email





Accepted terms


Create User

```

And when there is an error, the model's errors are automatically added
to the markup

```html


Address

Address is invalid





Accepted terms

Accepted terms must be accepted



Create Email

```

## TODO

So much! Maybe?

- Radio buttons, selects, and other fields
- Customize HTML of wrapper methods
- Customize error and label HTML placement with the inputs
- Form hints, placeholders, and more I18n customizations
- Document other new form builder methods like `error_tag` and `field`

## Installation
Add this line to your application's Gemfile:

```ruby
gem "form_buildable"
```

And then execute:
```bash
$ bundle
```

Or install it yourself as:
```bash
$ gem install form_buildable
```

## Contributing
Contribution directions go here.

## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).