Does AshPhoenix.Form trims string fields?

I have a component with a textarea input.

I noticed that if the text area has new lines in the start or end of its content and the component is re-rendered, it will remove these new lines.

When looking into the form structure, I noticed these fields:

%Phoenix.HTML.Form{
  source: #AshPhoenix.Form<
    ...
    params: %{"male_content" => "\n\nline\n\n"},
    source: #Ash.Changeset<
      attributes: %{
        ...
        male_content: "line",
        ...
      },
    >,
  ...
  params: %{"male_content" => "\n\nline\n\n"},
  ...
}

As you can see, the params field, both for Phoenix.HTML.Form and AshPhoenix.Form has the new lines correctly, but the Ash.Changeset doesn’t.

I’m guessing that when the re-render happens, the form will replace the textarea content with what is in the Ash.Changeset and remove the new lines.

Are you using the textarea from CoreComponents? The docs call out that normalize_value does something around this. I’m not actually sure why this is necessary (of course not saying that it isn’t I just don’t know) and is something I’ve never encountered before. Probably a cross-platform thing? Would love to know myself!

Just found the issue, seems like strings are trimmed by default in Ash adding the following to the field fixed it:

attribute :male_content, :string do
  allow_nil? false
  constraints trim?: false
end
1 Like

Yep! In ecto empty values are implemented at the changeset level Ecto.Changeset — Ecto v3.11.2

But in Ash the action/changeset has no bearing, only the type. It is up to the type whether or not an empty value (like "") should be transformed into nil. This does mean that additional work may need to be done in the form/action if you want to both allow nil values but turn empty ("") submissions from a form into nil, but I think generally simplifies things.