Easy form handling for phoenix. Write <%= input f, :name %>
and get wrappers with labels and error messages.
Infers the type, comes with styles for bootstrap.
Usually you're styling your form the same across your app. Generators are okay but if you're writing the same code over and over again. But the better solution is to find a good abstraction. Simple form for phoenix does exactly that.
The package can be installed as:
-
Add phoenix_simple_form to your list of dependencies in
mix.exs
:def deps do [{:phoenix_simple_form, "~> 0.0.2"}] end
-
Ensure phoenix_simple_form is started before your application:
def application do [applications: [:phoenix_simple_form]] end
-
Run
mix deps.get
You can customize styles and inferrers.
The default style for phoenix_simple_form is bootstrap4. (Feel free to contribute styles other css frameworks)
If you want to customize the style, add the following setting to your config.exs
:
config :phoenix_simple_form,
style: YourProject.YourStyle
Copy this file into your project. Rename the module name accordingly to your config setting.
To add a new style, just create a new input function.
def input(:custom_checkbox, f, name, opts) do
content_tag :div, wrapper_html(opts, %{class: "checkbox"}) do
[
label(f, name, class: "checkbox") do
[
checkbox(f, name, input_html(opts, %{})), label_translation(f, name)]
end,
error_tag(f, name)
]
end
end
You can use this by passing the as: option to an input field. E.g. <%= input f, :admin, as: :custom_checkbox %>
.
If want to use this type every time a field is named admin or ends with _count
or whatever, you can extend the type inferrer.
See the next section for more information.
Inferrers try to infer the right input type for the available data. The default order is:
-
Is the type is set explicitly with
as:
, use this type. -
If not, try to infer the type from the parameters. E.g. if the options list contains
collection:
, the type is automatically set to select field. -
If nothing else matches, read the model type from the database. Then e.g.
:integer
is mapped to a number input.:boolean
is mapped to a checkbox.
If you want to customize the inferrer, add the following setting to your config.exs
:
config :phoenix_simple_form,
inferrer: YourProject.YourInferrerModule
Copy this file into your project. Rename the module name accordingly to your config setting.
The project is heavily inspired by Simple Form for Rails.
This project uses the C4.1 process for all code changes.
"Everyone, without distinction or discrimination, SHALL have an equal right to become a Contributor under the terms of this contract."
- Check for open issues or open a new issue to start a discussion around a problem.
- Issues SHALL be named as "Problem: description of the problem".
- Fork the phoenix_simple_form repository on GitHub to start making your changes
- If possible, write a test which shows that the problem was solved.
- Send a pull request.
- Pull requests SHALL be named as "Solution: description of your solution"
- Your pull request is merged and you are added to the list of contributors