How to generate an input in phoenix forms without id or name fields?

How to generate a datetime_select without an :id or a :name?

When I use

<%= datetime_select nil, nil %>

it still generates a name and an id

1 Like

Hi @alsaraha can you elaborate why you are trying to do this? The HTML spec requires that all inputs have names, and ids are also generally a good idea. What issue are they causing you?

3 Likes

I need to make a template of this element and insert a new id and name

I don’t understand what this means, can you show more code?

1 Like

OK, what I am trying to achieve is dynamic field addition/removal with javascript only (i.e. without using liveview)

So, I have something like this

  <div id="datetime-template" data-name-template="datetime-input-template">
    <div>
      <%= datetime_select nil, nil, name: "datetime-input-template", id: "datetime_input_template" %>
    </div>
  </div>

I replace the name which matches dataset.nameTemplate with a new name generated with a function that gets a suitable name for this field,

e.g.datetime-input-template[hour] is replaced with suitable-name[hour], the id is generated by converting the name into id, so I don’t really need it to be present in the “template”,

the issue is in the id because this might result in duplicated ids, all of this would be easier if I removed the id attribute all together

why are IDs included by default when generating input elements? I removed this and things seem to be working fine.

The reason I removed the IDs is because I am showing multiple resources of the same type on the page. They are all editable, so if you toggle edit state on two of them at the same time, the inputs will have duplicate IDs across the two resources. I could “fix” this by appending the resource ID to the input’s id value… which I did, but I didn’t see the value in it and it was additional code. So I opted to remove the ID altogether.

Anyone have an opposing point of view I’m not considering?

Update: According to Phoenix documentation, the ID value on an input will be used in case of form recovery on LiveView disconnects.