Skip to content

Provide a method to get the value of the <label> associated with an <input> #49

Open
@petdance

Description

@petdance

I just discovered accidentally that HTML::Form::TextInput scrapes the value of its associated <label> tag and stores it in a hash entry for "value_name".

use HTML::Form;
use Data::Dumper;
$Data::Dumper::Indent = $Data::Dumper::Terse = $Data::Dumper::Sortkeys = 1;

my ($form) = HTML::Form->parse( <<'HTML', 'https://example.com' );
<form>
    <label for="id-cpw">Current Password</label>
    <input id="id-cpw" name="curr_password" value="" type="password">

    <!-- input is before label here. -->
    <input id="id-npw" name="new_password" value="" type="password" disabled="disabled">
    <label for="id-npw">New Password</label>
</form>
HTML

for my $name ( qw( curr_password new_password ) ) {
    my $input = $form->find_input( "$name" );
    print Dumper( $input);
}

gives this output:

bless( {
  'id' => 'id-cpw',
  'name' => 'curr_password',
  'type' => 'password',
  'value' => '',
  'value_name' => 'Current Password'
}, 'HTML::Form::TextInput' )
bless( {
  'disabled' => 'disabled',
  'id' => 'id-npw',
  'name' => 'new_password',
  'type' => 'password',
  'value' => '',
  'value_name' => ''
}, 'HTML::Form::TextInput' )

Unfortunately there's no documented method on the object to access that. Also, note that the field is not populated unless the <label> comes before the <input>, even though that's not a requirement in HTML. (It's the order that causes this, not that the 2nd field in this example is disabled)

It would be great if there was a documented method that would let the user get at the <label> tag value for any input field: Text, password, checkbox, radio. It would make it super easy to validate that all fields on a form had proper <label>s, which are one of the lowest of low-hanging fruit for making web forms more accessible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions