Re: HTML::Form -- override select options?
Jonathan Kamens <[email protected]>
| Newsgroups | gmane.comp.lang.perl.modules.lwp |
|---|---|
| Message-ID | <[email protected]> |
As promised (see attached message), here's a patch to HTML::Form to
allow the user to add a valid option to a <select> element so that the
element can be set to that value even if it wasn't originally listed
on the form. This is necessary because form pages can have JavaScript
which sets such elements to valid that aren't explicitly listed, and
Perl programmers thus need to be able to do the same thing.
--- lib/HTML/Form.pm.orig 2005-08-02 08:58:29.000000000 -0400
+++ lib/HTML/Form.pm 2005-08-02 09:15:36.000000000 -0400
@@ -174,19 +174,8 @@
next if $tag =~ m,/?optgroup,;
next if $tag eq "/option";
if ($tag eq "option") {
- my %a = %{$t->[0]};
- # rename keys so they don't clash with %attr
- for (keys %a) {
- next if $_ eq "value";
- $a{"option_$_"} = delete $a{$_};
- }
- while (my($k,$v) = each %$attr) {
- $a{$k} = $v;
- }
- $a{value_name} = $p->get_trimmed_text;
- $a{value} = delete $a{value_name}
- unless defined $a{value};
- $f->push_input("option", \%a);
+ $f->add_option($attr, shift @$t,
+ $p->get_trimmed_text);
}
else {
Carp::carp("Bad <select> tag '$tag'") if $^W;
@@ -399,6 +388,35 @@
}
+=item $form->add_option($select_attr, $option_attr, $text)
+
+This method adds an <option> to a <select> element. Use this to allow
+you to subsequently set the value of the element to one that was not
+listed as valid in the original form.
+
+Specify a reference to a hash of the attributes of the select element
+to modify (most importantly, "name"), a reference to a hash of the
+attributes of the option element (most importantly, "value"), and the
+text of the option element.
+
+=cut
+
+sub add_option {
+ my($f, $attr, $a, $text) = @_;
+ # rename keys so they don't clash with %attr
+ for (keys %$a) {
+ next if $_ eq "value";
+ $a->{"option_$_"} = delete $a->{$_};
+ }
+ while (my($k,$v) = each %$attr) {
+ $a->{$k} = $v;
+ }
+ $a->{value_name} = $text;
+ $a->{value} = delete $a->{value_name} unless defined $a->{value};
+ $f->push_input("option", $a);
+}
+
+
=item $value = $form->value( $name )
=item $form->value( $name, $new_value )
Jonathan Kamens
(unnamed)
(message/rfc822, 1.9 KB) - not displayed