$HTML::FormHandler::Render::Simple::VERSION
=
'0.40068'
;
requires(
'sorted_fields'
,
'field'
);
sub
render {
my
$self
=
shift
;
my
$output
=
$self
->render_start;
$output
.=
$self
->render_form_errors;
foreach
my
$field
(
$self
->sorted_fields ) {
$output
.=
$self
->render_field(
$field
);
}
$output
.=
$self
->render_end;
return
$output
;
}
sub
render_form_errors {
my
$self
=
shift
;
return
''
unless
$self
->has_form_errors;
my
$output
=
"\n<div class=\"form_errors\">"
;
$output
.=
qq{\n<span class="error_message">$_</span>}
for
$self
->all_form_errors;
$output
.=
"\n</div>"
;
return
$output
;
}
sub
render_field {
my
(
$self
,
$field
) =
@_
;
if
(
ref
( \
$field
) eq
'SCALAR'
) {
$field
=
$self
->field(
$field
);
}
die
"must pass field to render_field"
unless
(
defined
$field
&&
$field
->isa(
'HTML::FormHandler::Field'
) );
my
$widget
= ucc_widget(
$field
->widget);
return
''
if
$widget
eq
'no_render'
;
my
$rendered_field
;
my
$form_render
=
'render_'
.
$widget
;
if
(
$self
->can(
$form_render
) ) {
$rendered_field
=
$self
->
$form_render
(
$field
);
}
elsif
(
$field
->can(
'render'
) ) {
$rendered_field
=
$field
->render;
}
else
{
die
"No widget method found for '$widget' in H::F::Render::Simple"
;
}
return
$self
->wrap_field(
$field
,
$rendered_field
);
}
sub
wrap_field {
my
(
$self
,
$field
,
$rendered_field
) =
@_
;
return
"\n$rendered_field"
if
$field
->uwrapper eq
'none'
;
return
"\n$rendered_field"
if
!
$field
->do_wrapper;
my
$output
=
"\n"
;
my
$wrapper_tag
=
$field
->get_tag(
'wrapper_tag'
);
$wrapper_tag
||=
$field
->has_flag(
'is_repeatable'
) ?
'fieldset'
:
'div'
;
my
$attrs
= process_attrs(
$field
->wrapper_attributes);
$output
.=
qq{<$wrapper_tag$attrs>}
;
if
(
$wrapper_tag
eq
'fieldset'
) {
$output
.=
'<legend>'
.
$field
->loc_label .
'</legend>'
;
}
elsif
( !
$field
->get_tag(
'label_none'
) &&
$field
->do_label &&
length
(
$field
->label ) > 0 ) {
$output
.=
"\n"
.
$self
->render_label(
$field
);
}
$output
.=
"\n$rendered_field"
;
$output
.=
qq{\n<span class="error_message">$_</span>}
for
$field
->all_errors;
$output
.=
"\n</$wrapper_tag>"
;
return
"$output"
;
}
sub
render_text {
my
(
$self
,
$field
) =
@_
;
my
$output
=
'<input type="'
.
$field
->input_type .
'" name="'
;
$output
.=
$field
->html_name .
'"'
;
$output
.=
' id="'
.
$field
->id .
'"'
;
$output
.=
' size="'
.
$field
->size .
'"'
if
$field
->size;
$output
.=
' maxlength="'
.
$field
->maxlength .
'"'
if
$field
->maxlength;
$output
.=
' value="'
.
$field
->html_filter(
$field
->fif) .
'"'
;
$output
.= process_attrs(
$field
->element_attributes);
$output
.=
' />'
;
return
$output
;
}
sub
render_password {
my
(
$self
,
$field
) =
@_
;
my
$output
=
'<input type="password" name="'
;
$output
.=
$field
->html_name .
'"'
;
$output
.=
' id="'
.
$field
->id .
'"'
;
$output
.=
' size="'
.
$field
->size .
'"'
if
$field
->size;
$output
.=
' maxlength="'
.
$field
->maxlength .
'"'
if
$field
->maxlength;
$output
.=
' value="'
.
$field
->html_filter(
$field
->fif) .
'"'
;
$output
.= process_attrs(
$field
->element_attributes);
$output
.=
' />'
;
return
$output
;
}
sub
render_hidden {
my
(
$self
,
$field
) =
@_
;
my
$output
=
'<input type="hidden" name="'
;
$output
.=
$field
->html_name .
'"'
;
$output
.=
' id="'
.
$field
->id .
'"'
;
$output
.=
' value="'
.
$field
->html_filter(
$field
->fif) .
'"'
;
$output
.= process_attrs(
$field
->element_attributes);
$output
.=
' />'
;
return
$output
;
}
sub
render_select {
my
(
$self
,
$field
) =
@_
;
my
$multiple
=
$field
->multiple;
my
$id
=
$field
->id;
my
$output
=
'<select name="'
.
$field
->html_name .
'"'
;
$output
.=
qq{ id="$id"}
;
$output
.=
' multiple="multiple"'
if
$multiple
== 1;
$output
.=
' size="'
.
$field
->size .
'"'
if
$field
->size;
my
$html_attributes
= process_attrs(
$field
->element_attributes);
$output
.=
$html_attributes
;
$output
.=
'>'
;
my
$index
= 0;
if
(
defined
$field
->empty_select ) {
$output
.=
'<option value="">'
.
$field
->_localize(
$field
->empty_select) .
'</option>'
;
}
my
$fif
=
$field
->fif;
my
%fif_lookup
;
@fif_lookup
{
@$fif
} = ()
if
$multiple
;
foreach
my
$option
( @{
$field
->{options} } ) {
my
$value
=
$option
->{value};
$output
.=
'<option value="'
.
$field
->html_filter(
$value
)
.
qq{" id="$id.$index"}
;
if
(
defined
$option
->{disabled} &&
$option
->{disabled} ) {
$output
.=
' disabled="disabled"'
;
}
if
(
defined
$fif
) {
if
(
$multiple
&&
exists
$fif_lookup
{
$value
} ) {
$output
.=
' selected="selected"'
;
}
elsif
(
$fif
eq
$value
) {
$output
.=
' selected="selected"'
;
}
}
$output
.=
$html_attributes
;
my
$label
=
$option
->{label};
$label
=
$field
->_localize(
$label
)
if
$field
->localize_labels;
$output
.=
'>'
. (
$field
->html_filter(
$label
) ) .
'</option>'
;
$index
++;
}
$output
.=
'</select>'
;
return
$output
;
}
sub
render_checkbox {
my
(
$self
,
$field
) =
@_
;
my
$output
=
'<input type="checkbox" name="'
.
$field
->html_name .
'"'
;
$output
.=
' id="'
.
$field
->id .
'"'
;
$output
.=
' value="'
.
$field
->html_filter(
$field
->checkbox_value) .
'"'
;
$output
.=
' checked="checked"'
if
$field
->fif eq
$field
->checkbox_value;
$output
.= process_attrs(
$field
->element_attributes);
$output
.=
' />'
;
return
$output
;
}
sub
render_radio_group {
my
(
$self
,
$field
) =
@_
;
my
$output
=
" <br />"
;
my
$index
= 0;
foreach
my
$option
( @{
$field
->options } ) {
my
$id
=
$field
->id .
".$index"
;
$output
.=
qq{<label for="$id"><input type="radio" value="}
.
$field
->html_filter(
$option
->{value}) .
'"'
;
$output
.=
' name="'
.
$field
->html_name .
'" id="'
. "
$id
\
""
;
$output
.=
' checked="checked"'
if
$option
->{value} eq
$field
->fif;
$output
.=
' />'
;
$output
.=
$field
->html_filter(
$option
->{label}) .
'</label><br />'
;
$index
++;
}
return
$output
;
}
sub
render_textarea {
my
(
$self
,
$field
) =
@_
;
my
$fif
=
$field
->fif ||
''
;
my
$id
=
$field
->id;
my
$cols
=
$field
->cols || 10;
my
$rows
=
$field
->rows || 5;
my
$name
=
$field
->html_name;
my
$output
=
qq(<textarea name="$name" id="$id" )
. process_attrs(
$field
->element_attributes)
.
qq(rows="$rows" cols="$cols">)
.
$field
->html_filter(
$fif
)
.
q(</textarea>)
;
return
$output
;
}
sub
render_upload {
my
(
$self
,
$field
) =
@_
;
my
$output
;
$output
=
'<input type="file" name="'
;
$output
.=
$field
->html_name .
'"'
;
$output
.=
' id="'
.
$field
->id .
'"'
;
$output
.= process_attrs(
$field
->element_attributes);
$output
.=
' />'
;
return
$output
;
}
sub
render_label {
my
(
$self
,
$field
) =
@_
;
my
$attrs
= process_attrs(
$field
->label_attributes );
my
$label
=
$field
->html_filter(
$field
->loc_label);
$label
.=
$field
->get_tag(
'label_after'
)
if
(
$field
->tag_exists(
'label_after'
) );
my
$label_tag
=
$field
->tag_exists(
'label_tag'
) ?
$field
->get_tag(
'label_tag'
) :
'label'
;
return
qq{<$label_tag$attrs for="}
.
$field
->id .
qq{">$label</$label_tag>}
;
}
sub
render_compound {
my
(
$self
,
$field
) =
@_
;
my
$output
=
''
;
foreach
my
$subfield
(
$field
->sorted_fields ) {
$output
.=
$self
->render_field(
$subfield
);
}
return
$output
;
}
sub
render_submit {
my
(
$self
,
$field
) =
@_
;
my
$output
=
'<input type="submit" name="'
;
$output
.=
$field
->html_name .
'"'
;
$output
.=
' id="'
.
$field
->id .
'"'
;
$output
.= process_attrs(
$field
->element_attributes);
$output
.=
' value="'
.
$field
->html_filter(
$field
->_localize(
$field
->value)) .
'" />'
;
return
$output
;
}
sub
render_reset {
my
(
$self
,
$field
) =
@_
;
my
$output
=
'<input type="reset" name="'
;
$output
.=
$field
->html_name .
'"'
;
$output
.=
' id="'
.
$field
->id .
'"'
;
$output
.= process_attrs(
$field
->element_attributes);
$output
.=
' value="'
.
$field
->html_filter(
$field
->value) .
'" />'
;
return
$output
;
}
sub
render_captcha {
my
(
$self
,
$field
) =
@_
;
my
$output
.=
'<img src="'
.
$self
->captcha_image_url .
'"/>'
;
$output
.=
'<input id="'
.
$field
->id .
'" name="'
;
$output
.=
$field
->html_name .
'"/>'
;
return
$output
;
}
1;