kaiba-ns

kaiba-ns

Toggle disabled attribute on input field

Hello,

I need to toggle disabled attribute on an input field and have the following code:

def is_disabled(true, attrs), do: [{:disabled, true} | attrs]
def is_disabled(_, attrs), do: attrs

<%= form_for :price_form, "#", [phx_change: :price_change], fn f -> %>
     <%= text_input f, :price, is_disabled(true, [value: @total_price]) %>

The input field will be shown correctly - it will be disabled and the toggling will work. The problem is however that in the appropriate callback function, the form will just ignore that input field:

  def handle_event("price_change", params, socket) do
     IO.inspect params["price_form"]

The IO.inspect shows the following:

%{
  "comment" => "",
  "invoicing_entity" => "1",
}

All input fields are shown except for the :price one.

I confirmed that when I take out the toggling part, everything works fine.

Any help is highly appreciated!

First Post!

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Disabled fields do not have their value sent. If you want to send the value you probably want to create a hidden field when the text input is disabled with the same name that holds the value.

Most Liked

lucaong

lucaong

Or, alternatively, use readonly instead of disabled if you want to show the input (and submit it) but prevent users to change its value.

A readonly text input can be focused and is submitted as part of the form, but cannot be changed manually by the user.

kaiba-ns

kaiba-ns

Thanks for your response @benwilson512

I don’t want to send the value of the disabled field but the value of that field when it is enabled.
So, initially the field is disabled, but if the user clicks on some other field, it will be enabled and the user can type in new value. It is that new value I would like to send.

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
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
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31525 112
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New