jason.o

jason.o

Default value for a text input in an Ash.Form

I’m trying to provide a default value (which is a name retrieved from a DB) to an input tag. When the form is mounted, the default value in the “name” field input box is populated as expected. Then the user enters a different name to replace the default value, and it changes with no problem. However, when the user then enters a value to the other input (“Code” field), the Name field goes back to the initial default value. How can I make the form keep the user input (after the form validation)? Or is there a better way to provide a default input value?

<.form :let={f} phx-change="validate">
<.input field={{f, :name}} type="text" label="Name" value={@dynamic_default_name}>

<.input field={{f, :code}} type="text" label="Code" >
</.form>
def handle_event("validate", %{"form" => form_params}, socket) do
   form = AshPhoenix.Form.validate(socket.assigns.form, form_params)

  socket
   |> assign(:form, form)
  |> noreply()
end

Marked As Solved

zachdaniel

zachdaniel

Creator of Ash

There are three ways to do it. The first, is to set the default in the action logic

change fn changeset, _ -> 
  if ...(check if argument or attribute set w/ your name) do
    Ash.Changeset.change_attribute(:name, dynamic_default)
  else
    changeset
  end
end

The second one:

<.input field={{f, :name}} type="text" label="Name" value={AshPhoenix.Form.value(f, :name) || @dynamic_default_name}>

The third one, when creating the initial form

form = AshPhoenix.Form.for_create(...)
AshPhoenix.Form.validate(form, %{...params with deafult})

Also Liked

zachdaniel

zachdaniel

Creator of Ash

Try doing AshPhoenix.Form.validate(form, %{...params}, errors: false)

Where Next?

Popular in Questions Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement