Skip to content

Setting a radio button's value to an empty string results in the first item's value #26

Open
@randyl

Description

@randyl

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:

#!/usr/bin/env perl

use 5.010;
use strict;
use warnings;
use HTML::Form;

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');
say "initial value='" . ($input->value() // 'undef') . "'";

$input->value('');
say "empty string value='" . ($input->value() // 'undef') . "'";

$input->value(undef);
say "undef value='" . ($input->value() // 'undef') . "'";

# Output:
# initial value='undef'
# empty string value='1'
# undef value='undef'

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions