Subject: | [patch] add ability to apply a custom class to a label |
The current behavior in Rose::HTML::Form::Field->label_object either
removes or overwrites the class attribute. Attached is a patch to make
it play nicely with user defined classes.
Subject: | checkbox_label_class.patch |
diff -Naur Rose-HTML-Objects-0.616.orig//lib/Rose/HTML/Form/Field/Checkbox.pm Rose-HTML-Objects-0.616//lib/Rose/HTML/Form/Field/Checkbox.pm
--- Rose-HTML-Objects-0.616.orig//lib/Rose/HTML/Form/Field/Checkbox.pm 2010-04-27 08:14:39.000000000 -0700
+++ Rose-HTML-Objects-0.616//lib/Rose/HTML/Form/Field/Checkbox.pm 2012-12-29 22:02:11.000000000 -0800
@@ -168,6 +168,20 @@
Returns the XHTML serialization of the checkbox field only (i.e., without any label or error message)
+=item B<label_object>
+
+Returns the object representing the L<label|Rose::HTML::Label> for the checkbox.
+
+Example:
+
+ $field =
+ Rose::HTML::Form::Field::Checkbox->new(
+ label => 'Run tests',
+ name => 'tests',
+ value => 'yes');
+
+ $field->label_object->add_class('checkbox_label');
+
=back
=head1 AUTHOR
diff -Naur Rose-HTML-Objects-0.616.orig//lib/Rose/HTML/Form/Field.pm Rose-HTML-Objects-0.616//lib/Rose/HTML/Form/Field.pm
--- Rose-HTML-Objects-0.616.orig//lib/Rose/HTML/Form/Field.pm 2012-05-03 06:48:08.000000000 -0700
+++ Rose-HTML-Objects-0.616//lib/Rose/HTML/Form/Field.pm 2012-12-29 13:42:22.000000000 -0800
@@ -780,20 +780,13 @@
$label->for($self->html_attr('id'));
}
- my @classes =
- (
- ($self->required ? 'required' : ()),
- (defined $self->error ? 'error' : ()),
- );
+ $self->required ?
+ $label->add_class('required') :
+ $label->delete_class('required');
- if(@classes)
- {
- $label->html_attr(class => "@classes");
- }
- else
- {
- $label->delete_html_attr('class');
- }
+ defined $self->error ?
+ $label->add_class('error') :
+ $label->delete_class('error');
if(@_)
{
diff -Naur Rose-HTML-Objects-0.616.orig//t/form-field-checkbox.t Rose-HTML-Objects-0.616//t/form-field-checkbox.t
--- Rose-HTML-Objects-0.616.orig//t/form-field-checkbox.t 2008-10-22 10:44:44.000000000 -0700
+++ Rose-HTML-Objects-0.616//t/form-field-checkbox.t 2012-12-29 13:54:43.000000000 -0800
@@ -2,7 +2,7 @@
use strict;
-use Test::More tests => 26;
+use Test::More tests => 32;
BEGIN
{
@@ -70,3 +70,16 @@
is($field->html_checkbox, '<input checked class="foo" id="bar" name="tests" style="baz" type="checkbox" value="yep">', 'html_checkbox() 3');
is($field->xhtml_checkbox, '<input checked="checked" class="foo" id="bar" name="tests" style="baz" type="checkbox" value="yep" />', 'xhtml_checkbox() 3');
+
+is($field->html_label, '<label for="bar">Run tests</label>','html label before add class');
+is($field->xhtml_label, '<label for="bar">Run tests</label>','xhtml label before add class');
+
+$field->label_object->add_class(q{test_class});
+
+is($field->html_label, '<label class="test_class" for="bar">Run tests</label>', 'html label after add class');
+is($field->xhtml_label, '<label class="test_class" for="bar">Run tests</label>', 'xhtml label after add class');
+
+$field->label_object->delete_class(q{test_class});
+
+is($field->html_label, '<label class="" for="bar">Run tests</label>', 'html label after delete class');
+is($field->xhtml_label, '<label class="" for="bar">Run tests</label>', 'xhtml label after delete class');