ex_admin icon indicating copy to clipboard operation
ex_admin copied to clipboard

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

Open DylanGriffith opened this issue 8 years ago • 2 comments

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
  1. Add some content to the field with a hash on the first line like:
# Content that will disappear
  1. Save the model
  2. Edit the model and notice that the textarea is now empty

/cc @joshprice

DylanGriffith avatar Jan 10 '17 01:01 DylanGriffith

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

DylanGriffith avatar Jan 10 '17 03:01 DylanGriffith

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},

jfrolich avatar Jan 28 '19 07:01 jfrolich