Skip to content
Browse files

Rendering '0'

  • Loading branch information...
1 parent a883cea commit a817da8bade95ed730337f8911b3aa412b72a061 Zbigniew Lukasiak committed Jun 9, 2009
Showing with 44 additions and 42 deletions.
  1. +4 −4 lib/HTML/FormHandler/Field.pm
  2. +31 −37 lib/HTML/FormHandler/Render/Simple.pm
  3. +9 −1 t/render.t
View
8 lib/HTML/FormHandler/Field.pm
@@ -512,10 +512,10 @@ has 'fif_from_value' => ( isa => 'Str', is => 'rw',
sub _build_fif {
my $self = shift;
- return if( defined $self->password && $self->password == 1 );
+ return '' if( defined $self->password && $self->password == 1 );
if ( $self->has_input && !$self->fif_from_value )
{
- return $self->input;
+ return defined $self->input ? $self->input : '';
}
my $parent = $self->parent;
if ( defined $parent &&
@@ -532,9 +532,9 @@ sub _build_fif {
}
if ( $self->fif_from_value )
{
- return $self->input;
+ return defined $self->input ? $self->input : '';
}
- return;
+ return '';
}
has 'accessor' => (
View
68 lib/HTML/FormHandler/Render/Simple.pm
@@ -117,11 +117,10 @@ sub render_text
# label
my $output = $self->_label( $field );
# input
- my $fif = $field->fif || '';
- $output .= "<input type=\"text\" name=\"";
- $output .= $field->html_name . "\"";
- $output .= " id=\"" . $field->id . "\"";
- $output .= " value=\"" . $fif . "\">";
+ $output .= '<input type="text" name="';
+ $output .= $field->html_name . '"';
+ $output .= ' id="' . $field->id . '"';
+ $output .= ' value="' . $field->fif . '">';
# value
return $output;
}
@@ -137,12 +136,11 @@ sub render_password
my ( $self, $field ) = @_;
# label
my $output = $self->_label( $field );
- my $fif = $field->fif || '';
# input
- $output .= "<input type=\"password\" name=\"";
- $output .= $field->html_name . "\"";
- $output .= " id=\"" . $field->id . "\"";
- $output .= " value=\"" . $fif . "\">";
+ $output .= '<input type="password" name="';
+ $output .= $field->html_name . '"';
+ $output .= ' id="' . $field->id . '"';
+ $output .= ' value="' . $field->fif . '">';
# value
return $output;
}
@@ -157,11 +155,10 @@ sub render_hidden
{
my ( $self, $field ) = @_;
# input
- my $fif = $field->fif || '';
- my $output = "<input type=\"hidden\" name=\"";
- $output .= $field->html_name . "\"";
- $output .= " id=\"" . $field->id . "\"";
- $output .= " value=\"" . $fif . "\">";
+ my $output = '<input type="hidden" name="';
+ $output .= $field->html_name . '"';
+ $output .= ' id="' . $field->id . '"';
+ $output .= ' value="' . $field->fif . '">';
# value
return $output;
}
@@ -176,16 +173,15 @@ sub render_select
{
my ( $self, $field ) = @_;
- my $fif = $field->fif || '';
my $output = $self->_label( $field );
- $output .= "<select name=\"" . $field->html_name . "\"";
- $output .= " multiple=\"multiple\" size=\"5\"" if $field->multiple == 1;
- $output .= "\">";
+ $output .= '<select name="' . $field->html_name . '"';
+ $output .= ' multiple="multiple" size="5"' if $field->multiple == 1;
+ $output .= '">';
foreach my $option ( $field->options )
{
- $output .= "<option value=\"" . $option->{value} . "\" ";
+ $output .= '<option value="' . $option->{value} . '" ';
- if ($fif)
+ if ($field->fif)
{
if ( $field->multiple == 1 )
{
@@ -198,19 +194,19 @@ sub render_select
}
foreach my $optval ( @fif )
{
- $output .= " selected=\"selected\""
+ $output .= ' selected="selected"'
if $optval == $option->{value};
}
}
else
{
- $output .= "selected=\"selected\""
- if $option->{value} eq $fif;
+ $output .= 'selected="selected"'
+ if $option->{value} eq $field->fif;
}
}
- $output .= ">" . $option->{label} . "</option>";
+ $output .= '>' . $option->{label} . '</option>';
}
- $output .= "</select>";
+ $output .= '</select>';
return $output;
}
@@ -227,12 +223,11 @@ sub render_checkbox
{
my ( $self, $field ) = @_;
- my $fif = $field->fif || '';
my $output = $self->_label( $field );
- $output .= "<input type=\"checkbox\" name=\"";
+ $output .= '<input type="checkbox" name="';
$output .= $field->html_name . '" value="' . $field->checkbox_value . '"';
- $output .= " checked=\"checked\"" if $fif eq $field->checkbox_value;
- $output .= "/>";
+ $output .= ' checked="checked"' if $field->fif eq $field->checkbox_value;
+ $output .= '/>';
return $output;
}
@@ -250,14 +245,13 @@ sub render_radio_group
my ( $self, $field ) = @_;
my $output = "\n";
- my $fif = $field->fif || '';
foreach my $option ( $field->options )
{
- $output = "<label class=\"label\" for=\"";
- $output .= $field->html_name . "\">" . $option->{label} . ": </label>";
- $output .= "<input type=\"radio\" value=\"" . $option->{value} . "\"";
- $output .= " name=\"" . $field->html_name;
- $output .= " selected=\"selected\"" if $option->{value} eq $fif;
+ $output = '<label class="label" for="';
+ $output .= $field->html_name . '">' . $option->{label} . ': </label>';
+ $output .= '<input type="radio" value="' . $option->{value} . '"';
+ $output .= ' name="' . $field->html_name;
+ $output .= ' selected="selected"' if $option->{value} eq $self->fif;
$output .= " />\n";
}
return $output;
@@ -311,7 +305,7 @@ sub render_compound
{
$output .= $self->render_field($subfield);
}
- $output .= "</fieldset>";
+ $output .= '</fieldset>';
}
=head2 render_submit
View
10 t/render.t
@@ -1,4 +1,4 @@
-use Test::More tests => 11;
+use Test::More tests => 12;
use HTML::FormHandler::Field::Text;
@@ -15,6 +15,7 @@ use HTML::FormHandler::Field::Text;
label => 'TEST',
id => 'f99',
);
+ has_field 'number';
has_field 'fruit' => ( type => 'Select' );
has_field 'vegetables' => ( type => 'Multiple' );
has_field 'active' => ( type => 'Checkbox' );
@@ -66,6 +67,7 @@ ok( $form, 'create form');
my $params = {
test_field => 'something',
+ number => 0,
fruit => 2,
vegetables => [2,4],
active => 'now',
@@ -80,6 +82,12 @@ my $params = {
$form->process( $params );
+is( $form->render_field( $form->field('number') ),
+ '
+<div><label class="label" for="number">Number: </label><input type="text" name="number" id="renderformnumber" value="0"></div>
+',
+ "value '0' is rendered"
+);
my $output1 = $form->render_field( $form->field('test_field') );
is( $output1,

0 comments on commit a817da8

Please sign in to comment.
Something went wrong with that request. Please try again.