$HTML::FormFu::Element::Select::VERSION
=
'2.07'
;
__PACKAGE__->mk_attr_accessors(
qw( multiple size )
);
after
BUILD
=>
sub
{
my
$self
=
shift
;
$self
->layout_field_filename(
'field_layout_select_field'
);
$self
->multi_value(1);
return
;
};
sub
_prepare_attrs {
my
(
$self
,
$submitted
,
$value
,
$default
,
$option
) =
@_
;
if
(
$submitted
&&
defined
$value
&& (
ref
$value
eq
'ARRAY'
? any {
$_
eq
$option
->{value} }
@$value
:
$value
eq
$option
->{value} ) )
{
$option
->{attributes}{selected} =
'selected'
;
}
elsif
(
$submitted
&&
$self
->retain_default
&& ( !
defined
$value
||
$value
eq
$EMPTY_STR
)
&&
defined
(
$self
->value )
&&
$self
->value eq
$option
->{value} )
{
$option
->{attributes}{selected} =
'selected'
;
}
elsif
(
$submitted
) {
delete
$option
->{attributes}{selected};
}
elsif
(
defined
$default
&& (
ref
$default
eq
'ARRAY'
? any {
$_
eq
$option
->{value} }
@$default
:
$default
eq
$option
->{value} ) )
{
$option
->{attributes}{selected} =
'selected'
;
}
return
;
}
sub
_string_field {
my
(
$self
,
$render
) =
@_
;
my
$html
.=
sprintf
qq{<select name="%s"%s>\n}
,
$render
->{nested_name},
process_attrs(
$render
->{attributes} );
for
my
$option
( @{
$render
->{options} } ) {
if
(
exists
$option
->{group} ) {
$html
.=
"<optgroup"
;
if
(
defined
$option
->{label} ) {
$html
.=
sprintf
qq{ label="%s"}
,
$option
->{label};
}
$html
.=
sprintf
"%s>\n"
, process_attrs(
$option
->{attributes} );
for
my
$item
( @{
$option
->{group} } ) {
$html
.=
sprintf
qq{<option value="%s"%s>%s</option>\n}
,
$item
->{value},
process_attrs(
$item
->{attributes} ),
$item
->{label},
;
}
$html
.=
"</optgroup>\n"
;
}
else
{
$html
.=
sprintf
qq{<option value="%s"%s>%s</option>\n}
,
$option
->{value},
process_attrs(
$option
->{attributes} ),
$option
->{label},
;
}
}
$html
.=
"</select>"
;
return
$html
;
}
__PACKAGE__->meta->make_immutable;
1;