Pre populating form field using changeset and accessing the same value in the submitted form

Hello all👋

Fairly new to Elixir and looking for a small help.

I am using changeset to construct the form. In the form, I want one of the fields to be pre populated with the value and that field disabled. Once the user submits the form, the value should be there in the form that is submitted.

I created the changeset with that field and I am able to populate the field and disable it. Only problem is that field doesn’t show up in the submitted form data.

This field is inside a form

Hello and welcome,

So it’s normal that the value is not sent, and not Phoenix specific.

You could always use a hidden input with the corresponding values.

2 Likes

Could you please explain why the value is not sent in the form?

Also, I don’t want to make it a hidden field because I want the value to be shown to the user but it should be disabled and I want this value back in the form thats submitted

Haven’t tested this but you could try a hidden and disabled input that both reference that same field.

1 Like

A disabled input is not sent in the request… as You have noticed.

That’s why You could add (and not replace) hidden input with value to be sent.

You could set the input to read-only instead, and it will be sent.

But if the server knows already what value to use, there is no need to have it in the request :slight_smile:

1 Like