radio inputs and disabled method

radio inputs and disabled method

am 18.11.2005 22:53:19 von developer

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(<


one
two

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 ?

Re: radio inputs and disabled method

am 21.11.2005 19:10:58 von developer

--------------020104090607080205000609
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

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(< >


> one
> two
>

> 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 ?
>
>
>
>
>


--------------020104090607080205000609
Content-Type: text/plain;
name="patch-test.txt"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="patch-test.txt"

--- ./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);

--------------020104090607080205000609
Content-Type: text/plain;
name="patch.txt"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="patch.txt"

--- ./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;

--------------020104090607080205000609--

Re: radio inputs and disabled method

am 07.12.2005 15:37:07 von gisle

Joao Lopes 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(< > >


> > one
> > two
> >

> > 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 ?