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

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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
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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
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

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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
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
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
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement