Re: radio inputs and disabled method

Joao Lopes <[email protected]>
Newsgroups gmane.comp.lang.perl.modules.lwp
Message-ID <[email protected]>
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?

> 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 ?
>
>
>
>
>
patch-test.txt (text/plain, 1.3 KB)
--- ./t/html/form.t	Sat Dec 11 15:13:25 2004
+++ ../libwww-perl-5.803-mod/t/html/form.t	Mon Nov 21 17:36:20 2005
@@ -3,7 +3,7 @@
 use strict;
 use Test qw(plan ok);
 
-plan tests => 103;
+plan tests => 114;
 
 use HTML::Form;
 
@@ -358,18 +358,34 @@
 
 EOT
 #print $f->dump;
-ok(!$f->find_input("r0")->disabled);
+ok($f->find_input("r0")->disabled);
+ok(!defined($f->value("r0")));
 ok(!eval {$f->value("r0", 1);});
 ok($@ && $@ =~ /^The value '1' has been disabled for field 'r0'/);
+ok($f->find_input("r0")->disabled(0));
+ok(!$f->find_input("r0")->disabled());
+eval {$f->value("r0", 1);};
+ok(!$@);
+ok($f->value("r0"), 1);
+
 ok(!$f->find_input("r1")->disabled);
 ok($f->value("r1", 2), undef);
 ok($f->value("r1"), 2);
 ok(!eval {$f->value("r1", 1);});
 ok($@ && $@ =~ /^The value '1' has been disabled for field 'r1'/);
+
 ok(!eval {$f->value("r2", 2);});
 ok($@ && $@ =~ /^The value '2' has been disabled for field 'r2'/);
 ok(!eval {$f->value("r2", "two");});
 ok($@ && $@ =~ /^The value 'two' has been disabled for field 'r2'/);
+ok(!$f->find_input("r2")->disabled(1));
+ok(!eval {$f->value("r2", 1);});
+ok($@ && $@ =~ /^The value '1' has been disabled for field 'r2'/);
+ok($f->find_input("r2")->disabled(0));
+ok(!$f->find_input("r2")->disabled());
+eval {$f->value("r2", 2);};
+ok(!$@);
+
 
 ok(!$f->find_input("s0")->disabled);
 ok(!$f->find_input("s1")->disabled);
patch.txt (text/plain, 792 B)
--- ./lib/html/form.pm	Sat Dec 11 15:13:22 2004
+++ ../libwww-perl-5.803-mod/lib/html/form.pm	Mon Nov 21 18:06:43 2005
@@ -1023,6 +1023,31 @@
     push(@{$prev->{menu}}, $m);
 }
 
+sub disabled
+{
+    my $self = shift;
+
+    if ( $self->type ne "radio" ) { 
+        return $self->SUPER::disabled(@_);
+    }
+    else{
+        my $new_disabled;
+        $new_disabled = shift if @_;
+
+        my $all_disabled = 1;
+        for (@{$self->{menu}}) {
+            $all_disabled = 0 if !$_->{disabled} ;
+        }
+        if (defined $new_disabled) {
+            for (@{$self->{menu}}) {
+                $_->{disabled} = $new_disabled;
+            }	
+            $self->{disabled} = $new_disabled;
+        }
+		return $all_disabled;	
+    }
+}
+
 sub fixup
 {
     my $self = shift;
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.