# Does AshPhoenix.Form trims string fields?

**URL:** <https://forum.elixirforum.com/t/does-ashphoenix-form-trims-string-fields/62176>\
**Category:** Questions\
**Tags:** ash, ash-phoenix\
**Created:** [March 8, 2024, 4:36pm UTC](https://forum.elixirforum.com/t/does-ashphoenix-form-trims-string-fields/62176 "2024-03-08T16:36:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sezaru](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sezaru/32/36113_2.png) [@sezaru](https://forum.elixirforum.com/u/sezaru)\
**Post date:** [March 8, 2024, 4:36pm UTC](https://forum.elixirforum.com/t/does-ashphoenix-form-trims-string-fields/62176/1 "2024-03-08T16:36:12Z")

</div>

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:

```elixir
%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.

---

<div class="post-metadata">

**Author:** ![sodapopcan](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sodapopcan/32/34668_2.png) [@sodapopcan](https://forum.elixirforum.com/u/sodapopcan)\
**Post date:** [March 8, 2024, 4:45pm UTC](https://forum.elixirforum.com/t/does-ashphoenix-form-trims-string-fields/62176/2 "2024-03-08T16:45:05Z")

</div>

Are you using the textarea from `CoreComponent`s? The [docs](https://hexdocs.pm/phoenix_html/Phoenix.HTML.Form.html#normalize_value/2) 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!

---

<div class="post-metadata">

**Author:** ![sezaru](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sezaru/32/36113_2.png) [@sezaru](https://forum.elixirforum.com/u/sezaru)\
**Post date:** [March 8, 2024, 4:58pm UTC](https://forum.elixirforum.com/t/does-ashphoenix-form-trims-string-fields/62176/3 "2024-03-08T16:58:16Z")

</div>

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

```elixir
attribute :male_content, :string do
  allow_nil? false
  constraints trim?: false
end

```

---

<div class="post-metadata">

**Author:** ![zachdaniel](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zachdaniel/32/31980_2.png) [@zachdaniel](https://forum.elixirforum.com/u/zachdaniel)\
**Post date:** [March 8, 2024, 6:17pm UTC](https://forum.elixirforum.com/t/does-ashphoenix-form-trims-string-fields/62176/4 "2024-03-08T18:17:52Z")

</div>

Yep! In ecto empty values are implemented at the changeset level [Ecto.Changeset — Ecto v3.14.0](https://hexdocs.pm/ecto/Ecto.Changeset.html#module-empty-values)

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.
