flp

flp

Correct Display of Errors in Phoenix.HTML with Bootstrap

Bootstrap requires since Bootstrap 4.0 is-invalid as field class to display the field error.
I found a solution presented in the following to display the errors again, but I don’t really know what’s the best way to integrate it. I add for each field with errors in a generic way the is_invalid class.

def password_input(form, field, opts \\ []) do
  opts = if !is_atom(form) && Keyword.has_key?(form.errors, field), do: Keyword.update(opts, :class, "is-invalid", &("#{&1} is-invalid")), else: opts
  ...

I wonder now how I can integrate this in the best way into the Phoenix.HTML helpers with touching them as least as possible, so I hopefully have the least trouble to stay up to date with the helpers.

In a way like this to just not import the functions I need to “override”?

First 4 of 4 Posts! Switch mode

drl123

drl123

I haven’t tried it yet, but expect I’m going to be in the same boat as you with a side project I’m starting that is planned to use BS 4. There haven’t been updates to this in a while, but if the BS markup hasn’t changed, it may not need any.

https://github.com/GBH/phoenix_bootstrap_form

flp

flp

Thanks for this great hint to your code. I adopted some of your stuff and solved other things a bit differently, that I want to share here, too. And I have some feedback to your idea.

I needed to remove your form = %Form{} match from

  [...]
  |> Enum.each(fn method ->
    def unquote(method)(form, field, opts \\ [])

since you can also call this function with an atom instead of a form. You might adapt this as well.

I did not want to change anything of my code, so I created a new ViewHelper:

defmodule MyApp.BootstrapHelpers do

  defmacro __using__(_opts) do
    quote do
      import Phoenix.HTML
      import Phoenix.HTML.Form, except: [text_input: 3, file_input: 3, email_input: 3, password_input: 3, textarea: 3, telephone_input: 3, number_input: 3, date_input: 3, date_select: 3, datetime_select: 3, select: 4, time_select: 3, time_input: 3, url_input: 3, multiple_select: 4, range_input: 3]
      import Phoenix.HTML.Link
      import Phoenix.HTML.Tag
      import Phoenix.HTML.Format
      import SportsConnected.BootstrapHelpers
    end
  end

  [:text_input, :file_input, :email_input, :password_input, :textarea, :telephone_input, :number_input, :date_input, :date_select, :datetime_select, :time_select, :time_input, :url_input, :range_input]
  |> Enum.each(fn method ->
    def unquote(method)(form, field, opts \\ []) when is_atom(field) do
      opts = add_is_invalid_to_invalid_input(form, field, opts)
      elem(Code.eval_string("Phoenix.HTML.Form.#{Atom.to_string(elem(__ENV__.function, 0))}(form, field, opts)", [form: form, field: field, opts: opts]), 0)
    end
  end)

  [:select, :multiple_select]
  |> Enum.each(fn method ->
    def unquote(method)(form, field, options, opts \\ []) when is_atom(field) do
      opts = add_is_invalid_to_invalid_input(form, field, opts)
      elem(Code.eval_string("Phoenix.HTML.Form.#{Atom.to_string(elem(__ENV__.function, 0))}(form, field, options, opts)", [form: form, field: field, options: options, opts: opts]), 0)
    end
  end)

  defp add_is_invalid_to_invalid_input(form, field, opts) do
    if !is_atom(form) && Keyword.has_key?(form.errors, field), do: Keyword.update(opts, :class, "is-invalid", &("#{&1} is-invalid")), else: opts
  end

end

Of course, you need to change the import in your web.ex from Phoenix.HTML to your new module.

gorav

gorav

I follow the following:

  def input(form, field, opts \\ []) do
  ...
  opts =
      put_in(
        opts[:class],
        String.trim("form-control #{input_state_class(form, field)} #{opts[:class]}")
      )
  ...
  end

and

  def input_state_class(form, field) do
    cond do
      # The form was not yet submitted
      !form.source.action ->
        ""

      form.errors[field] ->
        "is-invalid"

      true ->
        "is-valid"
    end
  end

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement