smpallen99 / ex_admin

ExAdmin is an auto administration package for Elixir and the Phoenix Framework
MIT License
1.2k stars 272 forks source link

Input type of `:text` containing `#` does not appear in edit form #274

Open DylanGriffith opened 7 years ago

DylanGriffith commented 7 years ago

We're trying to build a markdown editing field and when we add a heading using # the text area appears empty after saving then going back to edit it.

We've noticed that there is no explicit documentation for text areas (ie input :field_name, type: :text). Is this supported?

Everything else seems to work just fine, we get a textarea and can pass through a rows attribute but editing is empty when there is a hash at the start of the first line.

Note that the hash does not cause this issue if there is anything other than whitespace before it.

Reproduction Steps

  1. Create a field with type text like so:
    input :field_name, type: :text
  2. Add some content to the field with a hash on the first line like:
    # Content that will disappear
  3. Save the model
  4. Edit the model and notice that the textarea is now empty

/cc @joshprice

DylanGriffith commented 7 years ago

We were able to workaround our issue by always adding a newline to the front of the trimmed text field before saving like so:

  def changeset(struct, params \\ %{}) do
    params =
      params
      |> workaround_broken_markdown_in_exadmin("article")

    struct
    |> cast(params, [:article])
    |> validate_required([:article])
  end

  # https://github.com/smpallen99/ex_admin/issues/274
  # A leading newline seems to workaround the issue
  defp workaround_broken_markdown_in_exadmin(params, field) do
    Map.update(params, field, "", &("\n" <> String.trim(&1)))
  end
jfrolich commented 5 years ago

For people that are looking for a fix, I patched xain in the following repo: https://github.com/jfrolich/xain/tree/shortcut-bug-fix

just add this to your mix.exs

 {:xain, github: "jfrolich/xain", branch: "shortcut-bug-fix", override: true},