sezaru

sezaru

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.

Marked As Solved

sezaru

sezaru

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

Also Liked

zachdaniel

zachdaniel

Creator of Ash

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

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.

Last Post!

zachdaniel

zachdaniel

Creator of Ash

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

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.

Where Next?

Popular in Questions Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

Other popular topics Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54996 245
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

We're in Beta

About us Mission Statement