-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Setting a radio button's value to an empty string results in the first item's value #26
Comments
Having one of the buttons checked initially doesn't seem to change things:
|
@randyl, thanks for reporting this. We should get this fixed. If you have the capacity right now to fix this, we'd be happy to accept a pull request. If not, that's ok too. :) |
I've investigated this. Internally, each possible value for a HTML::Form::ListInput has a name and a value. We've got the following comment at the top of that class. #select/option (val1, val2, ....)
#input/radio (undef, val1, val2,...)
#input/checkbox (undef, value)
#select-multiple/option (undef, value) In When we set I've turned this into a test. #!/usr/bin/env perl
use strict;
use warnings;
use HTML::Form;
use Test::More;
subtest 'radiobutons, no initial value' => sub {
my $form = HTML::Form->parse( <<'HTML', 'https://www.example.com' );
<form>
<input type="radio" name="foo" value="1">
<input type="radio" name="foo" value="2">
<input type="radio" name="foo" value="3">
</form>
HTML
my $input = $form->find_input('foo');
is $input->value, undef, 'start with no value';
$input->value('');
is $input->value, undef, 'no value when value set to empty string'; # FAIL
$input->value(undef);
is $input->value, undef, 'no value when value set to undef';
};
subtest 'radiobuttons, initial value' => sub {
my $form = HTML::Form->parse( <<'HTML', 'https://www.example.com' );
<form>
<input type="radio" name="foo" value="1">
<input type="radio" name="foo" value="2" checked="checked">
<input type="radio" name="foo" value="3">
</form>
HTML
my $input = $form->find_input('foo');
is $input->value, '2', 'initial value';
$input->value('');
is $input->value, undef, 'no value when value set to empty string'; # FAIL
$input->value(undef);
is $input->value, undef, 'no value when value set to undef';
};
subtest 'select, no initial value' => sub {
my $form = HTML::Form->parse( <<'HTML', 'https://www.example.com' );
<form>
<select name="foo">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="empty string"></option>
</form>
HTML
my $input = $form->find_input('foo');
is $input->value, 1, 'start with first';
$input->value('');
is $input->value, 'empty string', 'value set to empty string';
$input->value(undef);
is $input->value, undef, 'no value when value set to undef';
};
done_testing; The radio ones fail, like in the two example scripts. There's a third set of tests for the select, which passes. In this case, setting the value to the one that has no label, and therefore an empty string as its name makes sense.
I am not sure what the expected behaviour should be here, and if we should really fix it. It's been like this for probably over 20 years. What are we going to break by making it behave like when we pass Maybe we should just document this behaviour. (Please note I've not tried it for checkboxes.) |
If a radio button's value is set to an empty string, HTML::Form uses the value of the first item in the radio's menu. Here's a sample program demonstrating the behavior:
No matter what the initial value of the radio button is, setting it to an empty string always changes its value to the first item. It's not clear to me if this behavior is a bug, but I did find it surprising.
The text was updated successfully, but these errors were encountered: