sezaru

sezaru

Set_attribute makes input field empty when updating resource

I have a resource that has a field that can be nil:

attribute :cma_url, :string

Now I want to have a create and a update action that will need this field to not be nil.

To achieve that, I did this:

create :create do
  argument :cma_url, :string, allow_nil?: false

  change set_attribute(:cma_url, arg(:cma_url))
end

update :update do
  argument :cma_url, :string, allow_nil?: false

  change set_attribute(:cma_url, arg(:cma_url))
end

This works great for my create action, when I use it in AshPhoenix.Form.for_create.

The problem is with the update action, when I use it with AshPhoenix.Form.for_update, the cma_url input field will be empty even if that fields has already some value to it.

If I replace it with:

update :update do
  accept [:cma_url]
end

Now it works, but now the cma_url field will not be required anymore in the form.

How can I fix that?

Marked As Solved

zachdaniel

zachdaniel

Creator of Ash

The easiest way is instead of adding an attribute to use require_attributes [:cma_url] in your action. That allows you to express that an attribute is required even if it typically is not.

Alternatively, you could use a validation in the action

validate present(:cma_url) # <- put this in the action you want to require it on

Last Post!

sezaru

sezaru

Damn! I was not aware of require_attributes, amazing as always!

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
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
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
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

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36820 110
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement