Back in July, I sent a proposed patch against 0.03001 that allowed more
control over reversing the order of labels and fields:
http://lists.scsys.co.uk/pipermail/html-formfu/2008-July/001249.html
I still have a need for both of these options, so I have created a new
patch against 0.03005 (attached).
This patch does the following:
Adds a reverse attribute to _Field, which defaults to false. If reverse
is true, the label will follow the field; otherwise it will precede the
field. (If the label tag is 'legend', then reverse is ignored; the
legend always appears as the first tag within the container tag.)
Adds a reverse_group attribute to Checkboxgroup and Radiogroup, which
defaults to true. If reverse_group is true, the labels will follow the
fields; otherwise they will precede the fields.
Adds tests for the new behavior. Tests pass for string and template
rendering.
For all three reverse attributes (reverse, reverse_multi, reverse_group)
a true value means the label should follow the field. The default
values for reverse and reverse_group in this patch preserve the existing
behavior of FormFu.
thanks,
Ronald
diff -rN -x '*~' -u HTML-FormFu-0.03005.orig/MANIFEST
HTML-FormFu-0.03005/MANIFEST
--- HTML-FormFu-0.03005.orig/MANIFEST 2008-09-08 09:56:24.000000000 -0400
+++ HTML-FormFu-0.03005/MANIFEST 2008-11-17 11:21:19.000000000 -0500
@@ -386,6 +386,7 @@
t/elements/repeatable_counter_name.t
t/elements/repeatable_counter_name.yml
t/elements/reset.t
+t/elements/reverse.t
t/elements/select.t
t/elements/select_empty_first_label.t
t/elements/select_force_default.t
diff -rN -x '*~' -u
HTML-FormFu-0.03005.orig/lib/HTML/FormFu/Element/Checkboxgroup.pm
HTML-FormFu-0.03005/lib/HTML/FormFu/Element/Checkboxgroup.pm
--- HTML-FormFu-0.03005.orig/lib/HTML/FormFu/Element/Checkboxgroup.pm
2008-09-03 10:26:06.000000000 -0400
+++ HTML-FormFu-0.03005/lib/HTML/FormFu/Element/Checkboxgroup.pm
2008-11-17 11:06:03.000000000 -0500
@@ -8,7 +8,7 @@
use HTML::FormFu::Util qw( append_xml_attribute process_attrs );
use List::MoreUtils qw( any );
-__PACKAGE__->mk_item_accessors(qw/ input_type /);
+__PACKAGE__->mk_item_accessors(qw/ reverse_group input_type /);
sub new {
my $self = shift->next::method(@_);
@@ -18,6 +18,7 @@
$self->label_tag ( 'legend' );
$self->container_tag ( 'fieldset' );
$self->multi_value ( 1 );
+ $self->reverse_group ( 1 );
$self->input_type('checkbox');
return $self;
@@ -112,6 +113,7 @@
my $render = $self->next::method( {
field_filename => $self->field_filename,
+ reverse_group => $self->reverse_group,
input_type => $self->input_type,
$args ? %$args : (),
} );
@@ -139,37 +141,59 @@
;
for my $item ( @{ $option->{group} } ) {
- $html .= sprintf
- qq{<span>\n<input name="%s" type="%s" value="%s"%s />},
+ $html .= "<span>\n";
+
+ my $label = sprintf
+ "<label%s>%s</label>\n",
+ process_attrs( $item->{label_attributes} ),
+ $item->{label},
+ ;
+
+ my $input = sprintf
+ qq{<input name="%s" type="%s" value="%s"%s />\n},
$render->{nested_name},
$render->{input_type},
$item->{value},
process_attrs( $item->{attributes} ),
;
- $html .= sprintf
- "\n<label%s>%s</label>\n</span>\n",
- process_attrs( $item->{label_attributes} ),
- $item->{label},
- ;
+ if ( $render->{reverse_group} ) {
+ $html .= $input . $label;
+ }
+ else {
+ $html .= $label . $input;
+ }
+
+ $html .= "</span>\n";
}
$html .= "</span>\n";
}
else {
- $html .= sprintf
- qq{<span>\n<input name="%s" type="%s" value="%s"%s />},
+ $html .= "<span>\n";
+
+ my $label = sprintf
+ "<label%s>%s</label>\n",
+ process_attrs( $option->{label_attributes} ),
+ $option->{label},
+ ;
+
+ my $input = sprintf
+ qq{<input name="%s" type="%s" value="%s"%s />\n},
$render->{nested_name},
$render->{input_type},
$option->{value},
process_attrs( $option->{attributes} ),
;
- $html .= sprintf
- "\n<label%s>%s</label>\n</span>\n",
- process_attrs( $option->{label_attributes} ),
- $option->{label},
- ;
+ if ( $render->{reverse_group} ) {
+ $html .= $input . $label;
+ }
+ else {
+ $html .= $label . $input;
+ }
+
+ $html .= "</span>\n";
}
}
@@ -233,6 +257,18 @@
name: foo
auto_id: "%n_%c"
+=head2 reverse_group
+
+If true, then the label for each checkbox in the checkbox group should
+be rendered to the right of the field control. Otherwise, the label
+is rendered to the left of the field control.
+
+The default value is C<true>, causing each label to be rendered to the
+right of its field control (or to be explicit: the markup for the
+label comes after the field control in the source).
+
+Default Value: C<true>
+
=head1 SEE ALSO
Is a sub-class of, and inherits methods from
diff -rN -x '*~' -u
HTML-FormFu-0.03005.orig/lib/HTML/FormFu/Element/Radiogroup.pm
HTML-FormFu-0.03005/lib/HTML/FormFu/Element/Radiogroup.pm
--- HTML-FormFu-0.03005.orig/lib/HTML/FormFu/Element/Radiogroup.pm
2008-09-03 10:26:05.000000000 -0400
+++ HTML-FormFu-0.03005/lib/HTML/FormFu/Element/Radiogroup.pm 2008-11-17
10:37:22.000000000 -0500
@@ -75,9 +75,14 @@
name: foo
auto_id: "%n_%c"
+=head2 reverse_group
+
+See L<HTML::FormFu::Element::Checkboxgroup/reverse_group>.
+
=head1 SEE ALSO
Is a sub-class of, and inherits methods from
+L<HTML::FormFu::Element::Checkboxgroup>,
L<HTML::FormFu::Element::_Group>,
L<HTML::FormFu::Element::_Field>,
L<HTML::FormFu::Element>
diff -rN -x '*~' -u HTML-FormFu-0.03005.orig/lib/HTML/FormFu/Element/_Field.pm
HTML-FormFu-0.03005/lib/HTML/FormFu/Element/_Field.pm
--- HTML-FormFu-0.03005.orig/lib/HTML/FormFu/Element/_Field.pm 2008-09-04
10:31:01.000000000 -0400
+++ HTML-FormFu-0.03005/lib/HTML/FormFu/Element/_Field.pm 2008-11-17
11:31:47.000000000 -0500
@@ -49,8 +49,8 @@
label_filename label_tag
retain_default force_default
javascript non_param
- reverse_multi multi_value
- original_name
+ reverse reverse_multi
+ multi_value original_name
) );
__PACKAGE__->mk_output_accessors( qw( comment label value ) );
@@ -508,6 +508,7 @@
label_filename => $self->label_filename,
label_tag => $self->label_tag,
container_tag => $self->container_tag,
+ reverse => $self->reverse,
reverse_multi => $self->reverse_multi,
javascript => $self->javascript,
$args ? %$args : (),
@@ -846,7 +847,8 @@
}
}
- if ( defined $render->{label} && $render->{label_tag} ne 'legend' ) {
+ if ( defined $render->{label} && $render->{label_tag} ne 'legend' &&
+ !$render->{reverse} ) {
$html .= sprintf "\n%s", $self->_string_label($render);
}
@@ -879,6 +881,11 @@
my $html = '';
+ if ( defined $render->{label} && $render->{label_tag} ne 'legend' &&
+ $render->{reverse} ) {
+ $html .= "\n" . $self->_string_label($render);
+ }
+
if ( defined $render->{comment} ) {
$html .= sprintf "\n<span%s>\n%s\n</span>",
process_attrs( $render->{comment_attributes} ),
@@ -1239,11 +1246,28 @@
Default Value: C<false>
+=head2 reverse
+
+If true, then the field's label should be rendered to the right of the
+field control. (When the field is used within a
+L<Multi|HTML::FormFu::Element::Multi> block, the position of the label
+is controlled by the L</reverse_multi> option instead.)
+
+The default value is C<false>, causing the label to be rendered to the left
+of the field control (or to be explicit: the markup for the label comes
+before the field control in the source).
+
+Exception: If the label tag is 'legend', then the reverse attribute is
+ignored; the legend always appears as the first tag within the
+container tag.
+
+Default Value: C<false>
+
=head2 reverse_multi
If true, then when the field is used within a
L<Multi|HTML::FormFu::Element::Multi> block, the field's label should be
-rendered to the right of the field control
+rendered to the right of the field control.
The default value is C<false>, causing the label to be rendered to the left
of the field control (or to be explicit: the markup for the label comes
diff -rN -x '*~' -u
HTML-FormFu-0.03005.orig/share/templates/tt/xhtml/checkboxgroup_tag
HTML-FormFu-0.03005/share/templates/tt/xhtml/checkboxgroup_tag
--- HTML-FormFu-0.03005.orig/share/templates/tt/xhtml/checkboxgroup_tag
2008-09-03 10:25:58.000000000 -0400
+++ HTML-FormFu-0.03005/share/templates/tt/xhtml/checkboxgroup_tag
2008-11-17 11:13:58.000000000 -0500
@@ -1,12 +1,14 @@
<span[% process_attrs(self.attributes) %]>
[% FOREACH option = self.options %][% IF option.group %]<span[%
process_attrs(option.attributes) %]>
[% FOREACH item = option.group %]<span>
-<input name="[% self.nested_name %]" type="[% self.input_type %]" value="[%
item.value %]"[% process_attrs(item.attributes) %] />
-<label[% process_attrs(item.label_attributes) %]>[% item.label %]</label>
+[% IF self.reverse_group %]<input name="[% self.nested_name %]" type="[%
self.input_type %]" value="[% item.value %]"[% process_attrs(item.attributes)
%] />
+<label[% process_attrs(item.label_attributes) %]>[% item.label %]</label>[%
ELSE %]<label[% process_attrs(item.label_attributes) %]>[% item.label %]</label>
+<input name="[% self.nested_name %]" type="[% self.input_type %]" value="[%
item.value %]"[% process_attrs(item.attributes) %] />[% END %]
</span>
[% END %]</span>
[% ELSE %]<span>
-<input name="[% self.nested_name %]" type="[% self.input_type %]" value="[%
option.value %]"[% process_attrs(option.attributes) %] />
-<label[% process_attrs(option.label_attributes) %]>[% option.label %]</label>
+[% IF self.reverse_group %]<input name="[% self.nested_name %]" type="[%
self.input_type %]" value="[% option.value %]"[%
process_attrs(option.attributes) %] />
+<label[% process_attrs(option.label_attributes) %]>[% option.label
%]</label>[% ELSE %]<label[% process_attrs(option.label_attributes) %]>[%
option.label %]</label>
+<input name="[% self.nested_name %]" type="[% self.input_type %]" value="[%
option.value %]"[% process_attrs(option.attributes) %] />[% END %]
</span>
[% END %][% END %]</span>
\ No newline at end of file
diff -rN -x '*~' -u HTML-FormFu-0.03005.orig/share/templates/tt/xhtml/field
HTML-FormFu-0.03005/share/templates/tt/xhtml/field
--- HTML-FormFu-0.03005.orig/share/templates/tt/xhtml/field 2008-05-29
05:23:14.000000000 -0400
+++ HTML-FormFu-0.03005/share/templates/tt/xhtml/field 2008-11-17
11:16:54.000000000 -0500
@@ -1,8 +1,10 @@
[% IF self.container_tag.defined %]<[% self.container_tag %][%
process_attrs(self.container_attributes) %]>[% END %][% IF self.label.defined
&& self.label_tag == 'legend' %]
[% INCLUDE $self.label_filename %][% END %][% IF self.errors %][% FOREACH
error = self.errors %]
-<span class="error_message [% error.class %]">[% error.message %]</span>[% END
%][% END %][% IF self.label.defined && self.label_tag != 'legend' %]
+<span class="error_message [% error.class %]">[% error.message %]</span>[%
+END %][% END %][% IF self.label.defined && self.label_tag != 'legend' &&
!self.reverse %]
[% INCLUDE $self.label_filename %][% END %][% IF self.container_tag.defined %]
-[% END %][% content %][% IF self.comment.defined %]
+[% END %][% content %][% IF self.label.defined && self.label_tag != 'legend'
&& self.reverse %]
+[% INCLUDE $self.label_filename %][% END %][% IF self.comment.defined %]
<span[% process_attrs(self.comment_attributes) %]>
[% self.comment %]
</span>[% END %][% IF self.container_tag.defined %]
diff -rN -x '*~' -u HTML-FormFu-0.03005.orig/t/elements/checkboxgroup.t
HTML-FormFu-0.03005/t/elements/checkboxgroup.t
--- HTML-FormFu-0.03005.orig/t/elements/checkboxgroup.t 2008-05-29
05:22:59.000000000 -0400
+++ HTML-FormFu-0.03005/t/elements/checkboxgroup.t 2008-11-17
10:52:10.000000000 -0500
@@ -1,24 +1,28 @@
use strict;
use warnings;
-use Test::More tests => 4;
+use Test::More tests => 5;
use HTML::FormFu;
my $form = HTML::FormFu->new({ tt_args => { INCLUDE_PATH =>
'share/templates/tt/xhtml' } });
-my $field = $form->element('Checkboxgroup')->name('foo')->value(2)
+my $field1 = $form->element('Checkboxgroup')->name('foo')->value(2)
->options( [ [ 1 => 'One' ], [ 2 => 'Two' ] ] );
+# add element to test non-reversed labels
+my $field2 = $form->element('Checkboxgroup')->name('foo2')
+ ->options( [ [ 'a' => 'A' ], [ 'b' => 'B' ] ] )->reverse_group(0);
+
# add more elements to test accessor output
-$form->element('Checkboxgroup')->name('foo2')->options( [
+$form->element('Checkboxgroup')->name('foo3')->options( [
{ label => 'Ein', value => 1 },
{ label => 'Zwei', value => 2, attributes => { class => 'foobar' } },
] );
$form->element('Checkboxgroup')->name('bar')->values( [qw/ one two three /] )
->value('two')->label('My Bar');
-my $field_xhtml = qq{<fieldset class="checkboxgroup">
+my $field1_xhtml = qq{<fieldset class="checkboxgroup">
<span>
<span>
<input name="foo" type="checkbox" value="1" />
@@ -31,19 +35,36 @@
</span>
</fieldset>};
-is( "$field", $field_xhtml );
+is( "$field1", $field1_xhtml );
+
+my $field2_xhtml = qq{<fieldset class="checkboxgroup">
+<span>
+<span>
+<label>A</label>
+<input name="foo2" type="checkbox" value="a" />
+</span>
+<span>
+<label>B</label>
+<input name="foo2" type="checkbox" value="b" />
+</span>
+</span>
+</fieldset>};
+
+is( "$field2", $field2_xhtml );
+
my $form_xhtml = <<EOF;
<form action="" method="post">
-$field_xhtml
+$field1_xhtml
+$field2_xhtml
<fieldset class="checkboxgroup">
<span>
<span>
-<input name="foo2" type="checkbox" value="1" />
+<input name="foo3" type="checkbox" value="1" />
<label>Ein</label>
</span>
<span>
-<input name="foo2" type="checkbox" value="2" class="foobar" />
+<input name="foo3" type="checkbox" value="2" class="foobar" />
<label>Zwei</label>
</span>
</span>
diff -rN -x '*~' -u HTML-FormFu-0.03005.orig/t/elements/radiogroup.t
HTML-FormFu-0.03005/t/elements/radiogroup.t
--- HTML-FormFu-0.03005.orig/t/elements/radiogroup.t 2008-05-29
05:22:59.000000000 -0400
+++ HTML-FormFu-0.03005/t/elements/radiogroup.t 2008-11-17 10:54:50.000000000
-0500
@@ -1,24 +1,28 @@
use strict;
use warnings;
-use Test::More tests => 4;
+use Test::More tests => 5;
use HTML::FormFu;
my $form = HTML::FormFu->new({ tt_args => { INCLUDE_PATH =>
'share/templates/tt/xhtml' } });
-my $field = $form->element('Radiogroup')->name('foo')->value(2)
+my $field1 = $form->element('Radiogroup')->name('foo')->value(2)
->options( [ [ 1 => 'One' ], [ 2 => 'Two' ] ] );
+# add element to test non-reversed labels
+my $field2 = $form->element('Radiogroup')->name('foo2')
+ ->options( [ [ 'a' => 'A' ], [ 'b' => 'B' ] ] )->reverse_group(0);
+
# add more elements to test accessor output
-$form->element('Radiogroup')->name('foo2')->options( [
+$form->element('Radiogroup')->name('foo3')->options( [
{ label => 'Ein', value => 1 },
{ label => 'Zwei', value => 2, attributes => { class => 'foobar' } },
] );
$form->element('Radiogroup')->name('bar')->values( [qw/ one two three /] )
->value('two')->label('My Bar');
-my $field_xhtml = qq{<fieldset class="radiogroup">
+my $field1_xhtml = qq{<fieldset class="radiogroup">
<span>
<span>
<input name="foo" type="radio" value="1" />
@@ -31,19 +35,35 @@
</span>
</fieldset>};
-is( "$field", $field_xhtml );
+is( "$field1", $field1_xhtml );
+
+my $field2_xhtml = qq{<fieldset class="radiogroup">
+<span>
+<span>
+<label>A</label>
+<input name="foo2" type="radio" value="a" />
+</span>
+<span>
+<label>B</label>
+<input name="foo2" type="radio" value="b" />
+</span>
+</span>
+</fieldset>};
+
+is( "$field2", $field2_xhtml );
my $form_xhtml = <<EOF;
<form action="" method="post">
-$field_xhtml
+$field1_xhtml
+$field2_xhtml
<fieldset class="radiogroup">
<span>
<span>
-<input name="foo2" type="radio" value="1" />
+<input name="foo3" type="radio" value="1" />
<label>Ein</label>
</span>
<span>
-<input name="foo2" type="radio" value="2" class="foobar" />
+<input name="foo3" type="radio" value="2" class="foobar" />
<label>Zwei</label>
</span>
</span>
diff -rN -x '*~' -u HTML-FormFu-0.03005.orig/t/elements/reverse.t
HTML-FormFu-0.03005/t/elements/reverse.t
--- HTML-FormFu-0.03005.orig/t/elements/reverse.t 1969-12-31
19:00:00.000000000 -0500
+++ HTML-FormFu-0.03005/t/elements/reverse.t 2008-11-17 10:53:50.000000000
-0500
@@ -0,0 +1,17 @@
+use strict;
+use warnings;
+
+use Test::More tests => 1;
+
+use HTML::FormFu;
+
+my $form = HTML::FormFu->new({ tt_args => { INCLUDE_PATH =>
'share/templates/tt/xhtml' } });
+
+my $field = $form->element('Text')->name('foo')->label('My Foo')->reverse(1);
+
+my $field_xhtml = qq{<div class="text label">
+<input name="foo" type="text" />
+<label>My Foo</label>
+</div>};
+
+is( "$field", $field_xhtml );
_______________________________________________
HTML-FormFu mailing list
HTML-FormFu@lists.scsys.co.uk
http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/html-formfu