All Projects → jakub-zawislak → Formex

jakub-zawislak / Formex

Licence: mit
A better form library for Phoenix

Programming Languages

elixir
2628 projects

Projects that are alternatives of or similar to Formex

one plus n detector
Elixir library to help you detect 1+n queries in applications using Ecto
Stars: ✭ 20 (-90.29%)
Mutual labels:  phoenix, ecto
Query
Query adds tools to aid the use of Ecto in web settings.
Stars: ✭ 23 (-88.83%)
Mutual labels:  ecto, phoenix
Machinery
State machine thin layer for structs (+ GUI for Phoenix apps)
Stars: ✭ 367 (+78.16%)
Mutual labels:  ecto, phoenix
ex sieve
Implement dynamic filtering and sorting API for Ecto queries
Stars: ✭ 37 (-82.04%)
Mutual labels:  phoenix, ecto
Filterable
Filtering from incoming params in Elixir/Ecto/Phoenix with easy to use DSL.
Stars: ✭ 83 (-59.71%)
Mutual labels:  ecto, phoenix
pretty print formatter
Pretty Print Formatter for Elixir Logger module -- Colorize Ecto's SQL ouput 🖌️
Stars: ✭ 22 (-89.32%)
Mutual labels:  phoenix, ecto
Kaffy
Powerfully simple admin package for phoenix applications
Stars: ✭ 617 (+199.51%)
Mutual labels:  ecto, phoenix
ecto profiler
Project for Ecto DB profiling
Stars: ✭ 16 (-92.23%)
Mutual labels:  phoenix, ecto
Polymorphic embed
Polymorphic embeds in Ecto
Stars: ✭ 80 (-61.17%)
Mutual labels:  ecto, phoenix
Ecto morph
morph your Ecto capabilities into the s t r a t o s p h e r e !
Stars: ✭ 72 (-65.05%)
Mutual labels:  ecto, phoenix
guardian trackable
A Guardian hook to track user sign ins.
Stars: ✭ 25 (-87.86%)
Mutual labels:  phoenix, ecto
Mipha
Proj Elixir Forum build with phoenix 1.5.
Stars: ✭ 153 (-25.73%)
Mutual labels:  ecto, phoenix
ecto nested changeset
Helpers for manipulating nested Ecto changesets
Stars: ✭ 23 (-88.83%)
Mutual labels:  phoenix, ecto
ecto generator
Generate Ecto schemas from existing database in Phoenix - Elixir
Stars: ✭ 20 (-90.29%)
Mutual labels:  phoenix, ecto
contextual
🌈 Generate your Ecto contexts using this macro and eliminate boilerplate
Stars: ✭ 18 (-91.26%)
Mutual labels:  phoenix, ecto
Paper trail
Track and record all the changes in your database with Ecto. Revert back to anytime in history.
Stars: ✭ 380 (+84.47%)
Mutual labels:  ecto, phoenix
phoenix pagination
Simple pagination for Ecto and Phoenix that uses plain EEx templates.
Stars: ✭ 20 (-90.29%)
Mutual labels:  phoenix, ecto
querie
Compose Ecto query from the client side
Stars: ✭ 20 (-90.29%)
Mutual labels:  phoenix, ecto
Wallaby
Concurrent browser tests with elixir
Stars: ✭ 1,143 (+454.85%)
Mutual labels:  ecto, phoenix
Phoenix live dashboard
Realtime dashboard with metrics, request logging, plus storage, OS and VM insights
Stars: ✭ 1,657 (+704.37%)
Mutual labels:  ecto, phoenix

Formex

Formex is an extensible form library for Phoenix. With this library you don't write changeset (as in Ecto), but a separate module that declares fields of form (like in Symfony).

You can also use it with Ecto - see formex_ecto. That library will build changeset and additional Ecto queries for itself.

Formex doesn't validate data for itself - it uses validation libraries instead.

Formex comes with helper functions for templating. For now there is only a Bootstrap 3 form template, but you can easily create your own templates.

TL;DR

Installation

In addition to the main library, you have to install some validator adapter. In this example we will use Vex. List of available adapters

mix.exs

def deps do
  [{:formex, "~> 0.6.0"},
   {:formex_vex, "~> 0.1.0"}]
end

def application do
  [applications: [:formex]]
end

config/config.exs

config :formex,
  validator: Formex.Validator.Vex,
  translate_error: &AppWeb.ErrorHelpers.translate_error/1,  # optional, from /lib/app_web/views/error_helpers.ex
  template: Formex.Template.BootstrapHorizontal,            # optional, can be overridden in a template
  template_options: [                                       # optional, can be overridden in a template
    left_column: "col-sm-2",
    right_column: "col-sm-10"
  ]

web/web.ex

def controller do
  quote do
    use Formex.Controller
  end
end

def view do
  quote do
    use Formex.View
  end
end

Usage

Let's create a form for article.

Model

# /web/model/article.ex
defmodule App.Article do
  defstruct [:title, :content, :hidden]
end

Form Type

# /web/form/article_type.ex
defmodule App.ArticleType do
  use Formex.Type

  def build_form(form) do
    form
    |> add(:title, :text_input, label: "Title", validation: [presence: true])
    |> add(:content, :textarea, label: "Content", phoenix_opts: [
      rows: 4
    ], validation: [presence: true])
    |> add(:hidden, :checkbox, label: "Is hidden?", required: false)
    |> add(:save, :submit, label: "Submit", phoenix_opts: [
      class: "btn-primary"
    ])
  end
end

Please note that required option is used only to generate an asterisk. Any validation must be done via validation option.

The :text_input and so on are function names from Phoenix.HTML.Form

Controller

def new(conn, _params) do
  form = create_form(App.ArticleType, %Article{})
  render(conn, "form.html", form: form)
end

def create(conn, %{"article" => article_params}) do
  App.ArticleType
  |> create_form(%Article{}, article_params)
  |> handle_form
  |> case do
    {:ok, article} ->
      # do something with a new article struct
    {:error, form} ->
      # display errors
      render(conn, "form.html", form: form)
  end
end

Template

form.html.eex

<%= formex_form_for @form, article_path(@conn, :create), [class: "form-horizontal"], fn f -> %>
  <%= if @form.submitted? do %>Oops, something went wrong!<% end %>

  <%= formex_row f, :title %>
  <%= formex_row f, :content %>
  <%= formex_row f, :hidden %>
  <%= formex_row f, :save %>

  <%# or generate all fields at once: formex_rows f %>
<% end %>

Put an asterisk to required fields:

.required .control-label:after {
  content: '*';
  margin-left: 3px;
}

The final effect after submit:

Documentation

https://hexdocs.pm/formex

Basic usage

Custom fields

Templating

Guides

Extensions

Validation adapters

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].