Skip to content
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

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

Open
petdance opened this issue Mar 25, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@petdance
Copy link

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.

@petdance petdance added the enhancement New feature or request label Mar 25, 2024
@oalders
Copy link
Member

oalders commented Mar 25, 2024

This sounds like something we'd accept a pull request for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants