Bug with select field in forms #158
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Hi,
for a select field in my GET form, I have default choice with no value like in this screenshot:
But when I submit it, that text goes into URL like
http://127.0.0.1:8000/admin/products?manufacturer=--%20Select%20manufacturer%20. My backend framework (Symfony) complains it is not a valid select option (and it isn't) which makes form to fail.It was an easy fix. Instead of this code:
I just replaced it with
and it works. Now my URL doesn't have incorrect values any more.
Can you merge that?
I believe this is the expected behavior.
From MDN:
What we can do, is prevent the option from being sent if the
disabledorhiddenattributes are set.@robinnorth What do you think?
I did some quick testing. Looks like the standard behavior is that the field does not get sent when the
disabledattribute is set.You are right; after reading MDN specs, I retested that page and Chrome was lying to me. In reality, generated HTML is
value=""but developer tools didn't display the value, only the attribute.So the value isn't missing from html, it is an empty string which is totally valid.
Correct. I think the best thing is to only add the option to the request if the
disabledattribute is not set.Yes, that should keep us in line with default browser behaviour.
But pls, add it even if it is an empty string.
opt.value || opt.textwill returnopt.texteven if the value is an empty string.Yes, that is why I created this issue. This code already exists and if value is empty string, pjax will send text value instead.
At that point, backend framework complains avoid invalid value; it didn't render option with such value i.e.
--Select manufacturer--is just something for user (which can be translated), not for backend.That is correct behaviour of Symfony, browser must send one of rendered values of it will consider someone is hacking.
I hope you understand my point. Having url like
?manufacturer_id=&other_id=13is perfectly valid. If you try same form outside this package, you will see blank value in url as well.Example, take a look at url in dev bar:
Ok, so this should be better:
This will send
opt.valueif thevalueattribute, no matter what value it has. If it doesn't exist, it will sendopt.text. I think this matches the spec.Well... I am not so good in JS but I think it will do the job. Maybe someone more skilled should give an opinion.
But to be honest, I don't care about text value at all as long as my empty string is working 😄