Subject: | Add 'disable_fields' method |
The patch that I have attached adds a 'disable_fields' option to the module. This allows someone to mark fields that they want to be disabled when the form is displayed.
Being that we use HTML::FillInForm to pre-fill in forms that get displayed to the user, it will allow the developer to disable certain fields depending on data in their application.
diff -Nru HTML-FillInForm-1.04-orig/lib/HTML/FillInForm.pm HTML-FillInForm-1.04/lib/HTML/FillInForm.pm
--- HTML-FillInForm-1.04-orig/lib/HTML/FillInForm.pm 2004-03-08 08:15:51.000000000 -0800
+++ HTML-FillInForm-1.04/lib/HTML/FillInForm.pm 2004-05-18 03:07:38.000000000 -0700
@@ -38,6 +38,11 @@
? @{ $option{ignore_fields} } : $option{ignore_fields} if exists( $option{ignore_fields} );
$self->{ignore_fields} = \%ignore_fields;
+ my %disable_fields;
+ %disable_fields = map { $_ => 1 } ( ref $option{'disable_fields'} eq 'ARRAY' )
+ ? @{ $option{disable_fields} } : $option{ignore_fields} if exists( $option{disable_fields} );
+ $self->{disable_fields} = \%disable_fields;
+
if (my $fdat = $option{fdat}){
# Copy the structure to prevent side-effects.
my %copy;
@@ -120,6 +125,13 @@
$self->{output} .= '>';
delete $self->{option_no_value};
}
+
+ # Check if we need to disable this field
+ $attr->{disable} = 1
+ if exists $attr->{'name'} and
+ exists $self->{disable_fields}{ $attr->{'name'} } and
+ $self->{disable_fields}{ $attr->{'name'} } and
+ not ( exists $attr->{disable} and $attr->{disable} );
if ($tagname eq 'input'){
my $value = exists $attr->{'name'} ? $self->_get_param($attr->{'name'}) : undef;
# force hidden fields to have a value
@@ -413,6 +425,12 @@
fobject => $q,
ignore_fields => ['prev','next']);
+To disable the form from being edited, use the C<disable_fields> options:
+
+ $output = $fif->fill(scalarref => \$html,
+ fobject => $q,
+ disable_fields => [ 'uid', 'gid' ]);
+
Note that this module does not clear fields if you set the value to undef.
It will clear fields if you set the value to an empty array or an empty string. For example:
diff -Nru HTML-FillInForm-1.04-orig/t/19_disable_fields.t HTML-FillInForm-1.04/t/19_disable_fields.t
--- HTML-FillInForm-1.04-orig/t/19_disable_fields.t 1969-12-31 16:00:00.000000000 -0800
+++ HTML-FillInForm-1.04/t/19_disable_fields.t 2004-05-18 03:20:15.000000000 -0700
@@ -0,0 +1,28 @@
+#!/usr/local/bin/perl
+
+# contributed by Trevor Schellhorn
+
+use strict;
+use warnings FATAL => 'all';
+
+use Test::More tests => 3;
+
+use_ok('HTML::FillInForm');
+
+my $html = qq[
+<form>
+<input type="text" name="one" value="not disturbed">
+<input type="text" name="two" value="not disturbed">
+</form>
+];
+
+my $result = HTML::FillInForm->new->fill(
+ scalarref => \$html,
+ fdat => {
+ two => "new val 2",
+ },
+ disable_fields => [qw(two)],
+ );
+
+ok($result =~ /not disturbed.+one/,'don\'t disable 1');
+ok($result =~ /new val 2.+two.+disable="1"/,'disable 2');