Re: radio inputs and disabled method

Gisle Aas <[email protected]>
Newsgroups gmane.comp.lang.perl.modules.lwp
Message-ID <[email protected]>
Joao Lopes <[email protected]> writes:

> Hi, I modified Form.pm overriding method "disabled" in subclass
> "HTML::Form::ListInput" (and form.t) and it seems to work as I would
> expect.
> Do you think attached patch is a good approach?

Looks basically good.  I modified into this one to make it do the
right thing for select/option stuff as well.  Thanks! To appear in
LWP-5.805.

Regards,
Gisle


Index: lib/HTML/Form.pm
===================================================================
RCS file: /cvsroot/libwww-perl/lwp5/lib/HTML/Form.pm,v
retrieving revision 1.53
retrieving revision 1.54
diff -u -p -r1.53 -r1.54
--- lib/HTML/Form.pm	7 Dec 2005 09:33:34 -0000	1.53
+++ lib/HTML/Form.pm	7 Dec 2005 14:32:27 -0000	1.54
@@ -1,13 +1,13 @@
 package HTML::Form;
 
-# $Id: Form.pm,v 1.53 2005/12/07 09:33:34 gisle Exp $
+# $Id: Form.pm,v 1.54 2005/12/07 14:32:27 gisle Exp $
 
 use strict;
 use URI;
 use Carp ();
 
 use vars qw($VERSION);
-$VERSION = sprintf("%d.%03d", q$Revision: 1.53 $ =~ /(\d+)\.(\d+)/);
+$VERSION = sprintf("%d.%03d", q$Revision: 1.54 $ =~ /(\d+)\.(\d+)/);
 
 my %form_tags = map {$_ => 1} qw(input textarea button select option);
 
@@ -1035,13 +1035,13 @@ sub add_to_form
 	if $type eq "checkbox";
 
     if ($type eq "option" && exists $self->{multiple}) {
-	$self->{disabled} ||= $self->{option_disabled};
+	$self->{disabled} ||= delete $self->{option_disabled};
 	return $self->SUPER::add_to_form($form);
     }
 
     die "Assert" if @{$self->{menu}} != 1;
     my $m = $self->{menu}[0];
-    $m->{disabled}++ if $self->{option_disabled};
+    $m->{disabled}++ if delete $self->{option_disabled};
 
     my $prev = $form->find_input($self->{name}, $self->{type});
     return $self->SUPER::add_to_form($form) unless $prev;
@@ -1060,6 +1060,29 @@ sub fixup
     $self->{menu}[$self->{current}]{seen}++ if exists $self->{current};
 }
 
+sub disabled
+{
+    my $self = shift;
+    my $type = $self->type;
+
+    my $old = $self->{disabled} || _menu_all_disabled(@{$self->{menu}});
+    if (@_) {
+	my $v = shift;
+	$self->{disabled} = $v;
+        for (@{$self->{menu}}) {
+            $_->{disabled} = $v;
+        }
+    }
+    return $old;
+}
+
+sub _menu_all_disabled {
+    for (@_) {
+	return 0 unless $_->{disabled};
+    }
+    return 1;
+}
+
 sub value
 {
     my $self = shift;



> 
> > For radio inputs, disabled method is always returning false (meaning
> > "enabled"), even if all radio inputs are disabled.
> > I think it should return something like: false if at least one radio
> > with same name is not disabled...; true otherwise.
> >
> > Also when we try to enable the radio, and then modify the
> > input->value, we don't succeed.
> > I think it would be better if the method input->disabled(...) would
> > enable/disable all radio buttons whithin the input..
> >
> > I'm using libwww-perl-5.803...
> >
> > Sample code:
> >
> >    use HTML::Form;
> >
> >    my $f = HTML::Form->parse(<<EOT, "http://www.example.com");
> >    <form>
> >    <input type=radio name=r0 value=1 disabled>one
> >    <input type=radio name=r0 value=2 disabled>two
> >    </form>
> >    EOT
> >    # here we see radio as not disabled
> >    print $f->dump;
> >    # try to enable it
> >    $f->find_input("r0")->disabled(0);
> >    # then we get the exception: The value '1' has been disabled for
> >    field 'r0' at
> >    $f->value("r0", 1);
> >
> >
> > Has anyone had similar feeling/experience or am I missing something ?
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.