$HTML::FormHandler::Field::VERSION
=
'0.40068'
;
has
'name'
=> (
isa
=>
'Str'
,
is
=>
'rw'
,
required
=> 1 );
has
'type'
=> (
isa
=>
'Str'
,
is
=>
'rw'
,
default
=>
sub
{
ref
shift
} );
has
'parent'
=> (
is
=>
'rw'
,
predicate
=>
'has_parent'
,
weak_ref
=> 1 );
sub
has_fields { }
has
'input_without_param'
=> (
is
=>
'rw'
,
predicate
=>
'has_input_without_param'
);
has
'not_nullable'
=> (
is
=>
'rw'
,
isa
=>
'Bool'
);
has
'no_value_if_empty'
=> (
is
=>
'rw'
,
isa
=>
'Bool'
);
has
'validate_when_empty'
=> (
is
=>
'rw'
,
isa
=>
'Bool'
);
has
'init_value'
=> (
is
=>
'rw'
,
clearer
=>
'clear_init_value'
,
predicate
=>
'has_init_value'
);
has
'default'
=> (
is
=>
'rw'
);
has
'default_over_obj'
=> (
is
=>
'rw'
,
builder
=>
'build_default_over_obj'
);
sub
build_default_over_obj { }
has
'result'
=> (
isa
=>
'HTML::FormHandler::Field::Result'
,
is
=>
'ro'
,
weak_ref
=> 1,
clearer
=>
'clear_result'
,
predicate
=>
'has_result'
,
writer
=>
'_set_result'
,
handles
=> [
'_set_input'
,
'_clear_input'
,
'_set_value'
,
'_clear_value'
,
'errors'
,
'all_errors'
,
'_push_errors'
,
'num_errors'
,
'has_errors'
,
'clear_errors'
,
'validated'
,
'add_warning'
,
'all_warnings'
,
'num_warnings'
,
'has_warnings'
,
'warnings'
,
'missing'
,
],
);
has
'_pin_result'
=> (
is
=>
'ro'
,
reader
=>
'_get_pin_result'
,
writer
=>
'_set_pin_result'
);
sub
has_input {
my
$self
=
shift
;
return
unless
$self
->has_result;
return
$self
->result->has_input;
}
sub
has_value {
my
$self
=
shift
;
return
unless
$self
->has_result;
return
$self
->result->has_value;
}
sub
reset_result {
my
$self
=
shift
;
$self
->clear_result;
$self
->build_result;
}
sub
build_result {
my
$self
=
shift
;
my
@parent
= (
'parent'
=>
$self
->parent->result )
if
(
$self
->parent &&
$self
->parent->result );
my
$result
= HTML::FormHandler::Field::Result->new(
name
=>
$self
->name,
field_def
=>
$self
,
@parent
);
$self
->_set_pin_result(
$result
);
$self
->_set_result(
$result
);
}
sub
input {
my
$self
=
shift
;
return
undef
unless
$self
->has_result || !
$self
->form;
my
$result
=
$self
->result;
return
$result
->_set_input(
@_
)
if
@_
;
return
$result
->input;
}
sub
value {
my
$self
=
shift
;
return
undef
unless
$self
->has_result || !
$self
->form;
my
$result
=
$self
->result;
return
undef
unless
$result
;
return
$result
->_set_value(
@_
)
if
@_
;
return
$result
->value;
}
sub
clear_input {
shift
->_clear_input }
sub
clear_value {
shift
->_clear_value }
sub
clear_data {
my
$self
=
shift
;
$self
->clear_result;
$self
->clear_active;
}
sub
_deflate_and_set_value {
my
(
$self
,
$value
) =
@_
;
if
(
$self
->_can_deflate ) {
$value
=
$self
->_apply_deflation(
$value
);
}
$self
->_set_value(
$value
);
}
sub
is_repeatable { }
has
'fif_from_value'
=> (
isa
=>
'Str'
,
is
=>
'ro'
);
sub
fif {
my
(
$self
,
$result
) =
@_
;
return
if
(
$self
->inactive && !
$self
->_active );
return
''
if
$self
->password;
return
unless
$result
||
$self
->has_result;
my
$lresult
=
$result
||
$self
->result;
if
( (
$self
->has_result &&
$self
->has_input && !
$self
->fif_from_value ) ||
(
$self
->fif_from_value && !
defined
$lresult
->value ) )
{
return
defined
$lresult
->input ?
$lresult
->input :
''
;
}
if
(
$lresult
->has_value ) {
my
$value
;
if
(
$self
->_can_deflate ) {
$value
=
$self
->_apply_deflation(
$lresult
->value);
}
else
{
$value
=
$lresult
->value;
}
return
(
defined
$value
?
$value
:
''
);
}
elsif
(
defined
$self
->value ) {
return
$self
->value;
}
return
''
;
}
has
'accessor'
=> (
isa
=>
'Str'
,
is
=>
'rw'
,
lazy
=> 1,
default
=>
sub
{
my
$self
=
shift
;
my
$accessor
=
$self
->name;
$accessor
=~ s/^(.*)\.//g
if
(
$accessor
=~ /\./ );
return
$accessor
;
}
);
has
'is_contains'
=> (
is
=>
'rw'
,
isa
=>
'Bool'
);
has
'temp'
=> (
is
=>
'rw'
);
sub
has_flag {
my
(
$self
,
$flag_name
) =
@_
;
return
unless
$self
->can(
$flag_name
);
return
$self
->
$flag_name
;
}
has
'label'
=> (
isa
=>
'Maybe[Str]'
,
is
=>
'rw'
,
lazy
=> 1,
builder
=>
'build_label'
,
);
has
'do_label'
=> (
isa
=>
'Bool'
,
is
=>
'rw'
,
default
=> 1 );
has
'build_label_method'
=> (
is
=>
'rw'
,
isa
=>
'CodeRef'
,
traits
=> [
'Code'
],
handles
=> {
'build_label'
=>
'execute_method'
},
default
=>
sub
{ \
&default_build_label
},
);
sub
default_build_label {
my
$self
=
shift
;
my
$label
=
$self
->name;
$label
=~ s/_/ /g;
$label
=
ucfirst
(
$label
);
return
$label
;
}
sub
loc_label {
my
$self
=
shift
;
return
$self
->_localize(
$self
->label);
}
has
'wrap_label_method'
=> (
traits
=> [
'Code'
],
is
=>
'ro'
,
isa
=>
'CodeRef'
,
predicate
=>
'does_wrap_label'
,
handles
=> {
'wrap_label'
=>
'execute_method'
},
);
has
'title'
=> (
isa
=>
'Str'
,
is
=>
'rw'
);
has
'style'
=> (
isa
=>
'Str'
,
is
=>
'rw'
);
has
'form'
=> (
isa
=>
'HTML::FormHandler'
,
is
=>
'rw'
,
weak_ref
=> 1,
predicate
=>
'has_form'
,
);
sub
is_form { 0 }
has
'html_name'
=> (
isa
=>
'Str'
,
is
=>
'rw'
,
lazy
=> 1,
builder
=>
'build_html_name'
);
sub
build_html_name {
my
$self
=
shift
;
my
$prefix
= (
$self
->form &&
$self
->form->html_prefix ) ?
$self
->form->name .
"."
:
''
;
return
$prefix
.
$self
->full_name;
}
has
'widget'
=> (
isa
=>
'Str'
,
is
=>
'rw'
);
has
'widget_wrapper'
=> (
isa
=>
'Str'
,
is
=>
'rw'
);
has
'do_wrapper'
=> (
is
=>
'rw'
,
default
=> 1 );
sub
wrapper {
shift
->widget_wrapper ||
''
}
sub
uwrapper { ucc_widget(
shift
->widget_wrapper ||
''
) ||
'simple'
}
sub
twrapper {
shift
->uwrapper .
".tt"
}
sub
uwidget { ucc_widget(
shift
->widget ||
''
) ||
'simple'
}
sub
twidget {
shift
->uwidget .
".tt"
}
has
'wrapper_tags'
=> (
isa
=>
'HashRef'
,
traits
=> [
'Hash'
],
is
=>
'rw'
,
builder
=>
'build_wrapper_tags'
,
handles
=> {
has_wrapper_tags
=>
'count'
}
);
sub
build_wrapper_tags { {} }
has
'tags'
=> (
traits
=> [
'Hash'
],
isa
=>
'HashRef'
,
is
=>
'rw'
,
builder
=>
'build_tags'
,
handles
=> {
_get_tag
=>
'get'
,
set_tag
=>
'set'
,
has_tag
=>
'exists'
,
tag_exists
=>
'exists'
,
delete_tag
=>
'delete'
,
},
);
sub
build_tags {{}}
sub
merge_tags {
my
(
$self
,
$new
) =
@_
;
my
$old
=
$self
->tags;
$self
->tags( merge(
$new
,
$old
) );
}
sub
get_tag {
my
(
$self
,
$name
) =
@_
;
return
''
unless
$self
->tag_exists(
$name
);
my
$tag
=
$self
->_get_tag(
$name
);
return
$self
->
$tag
if
ref
$tag
eq
'CODE'
;
return
$tag
unless
$tag
=~ /^%/;
(
my
$block_name
=
$tag
) =~ s/^%//;
return
$self
->form->block(
$block_name
)->render
if
(
$self
->form &&
$self
->form->block_exists(
$block_name
) );
return
''
;
}
has
'widget_name_space'
=> (
isa
=>
'HFH::ArrayRefStr'
,
is
=>
'rw'
,
traits
=> [
'Array'
],
default
=>
sub
{[]},
coerce
=> 1,
handles
=> {
push_widget_name_space
=>
'push'
,
},
);
sub
add_widget_name_space {
my
(
$self
,
@ns
) =
@_
;
@ns
= @{
$ns
[0]}
if
(
scalar
@ns
&&
ref
$ns
[0] eq
'ARRAY'
);
$self
->push_widget_name_space(
@ns
);
}
has
'order'
=> (
isa
=>
'Int'
,
is
=>
'rw'
,
default
=> 0 );
has
'inactive'
=> (
isa
=>
'Bool'
,
is
=>
'rw'
,
clearer
=>
'clear_inactive'
);
has
'_active'
=> (
isa
=>
'Bool'
,
is
=>
'rw'
,
clearer
=>
'clear_active'
,
predicate
=>
'has__active'
);
sub
is_active {
my
$self
=
shift
;
return
!
$self
->is_inactive;
}
sub
is_inactive {
my
$self
=
shift
;
return
((
$self
->inactive && !
$self
->_active) || (!
$self
->inactive &&
$self
->has__active &&
$self
->_active == 0 ) );
}
has
'id'
=> (
isa
=>
'Str'
,
is
=>
'rw'
,
lazy
=> 1,
builder
=>
'build_id'
);
has
'build_id_method'
=> (
is
=>
'rw'
,
isa
=>
'CodeRef'
,
traits
=> [
'Code'
],
default
=>
sub
{
sub
{
shift
->html_name } },
handles
=> {
build_id
=>
'execute_method'
},
);
has
'password'
=> (
isa
=>
'Bool'
,
is
=>
'rw'
);
has
'disabled'
=> (
isa
=>
'Bool'
,
is
=>
'rw'
);
has
'readonly'
=> (
isa
=>
'Bool'
,
is
=>
'rw'
);
has
'tabindex'
=> (
is
=>
'rw'
,
isa
=>
'Int'
);
sub
html_element {
'input'
}
has
'type_attr'
=> (
is
=>
'rw'
,
isa
=>
'Str'
,
default
=>
'text'
);
has
'html5_type_attr'
=> (
isa
=>
'Str'
,
is
=>
'ro'
,
default
=>
'text'
);
sub
input_type {
my
$self
=
shift
;
return
$self
->html5_type_attr
if
(
$self
->form &&
$self
->form->has_flag(
'is_html5'
) );
return
$self
->type_attr;
}
sub
html_attr {
shift
->element_attr(
@_
) }
sub
has_html_attr {
shift
->has_element_attr(
@_
) }
sub
get_html_attr {
shift
->get_element_attr(
@_
) }
sub
set_html_attr {
shift
->set_element_attr(
@_
) }
{
no
strict
'refs'
;
foreach
my
$attr
(
'wrapper'
,
'element'
,
'label'
) {
my
$add_meth
=
"add_${attr}_class"
;
my
$trigger_sub
=
sub
{
my
(
$self
,
$value
) =
@_
;
if
(
my
$class
=
delete
$self
->{
"${attr}_attr"
}->{class} ) {
$self
->
$add_meth
(
$class
);
}
};
has
"${attr}_attr"
=> (
is
=>
'rw'
,
traits
=> [
'Hash'
],
builder
=>
"build_${attr}_attr"
,
handles
=> {
"has_${attr}_attr"
=>
'count'
,
"get_${attr}_attr"
=>
'get'
,
"set_${attr}_attr"
=>
'set'
,
"delete_${attr}_attr"
=>
'delete'
,
"exists_${attr}_attr"
=>
'exists'
,
},
trigger
=>
$trigger_sub
,
);
my
$attr_builder
= __PACKAGE__ .
"::build_${attr}_attr"
;
*$attr_builder
= subname
$attr_builder
,
sub
{{}};
has
"${attr}_class"
=> (
is
=>
'rw'
,
isa
=>
'HFH::ArrayRefStr'
,
traits
=> [
'Array'
],
coerce
=> 1,
builder
=>
"build_${attr}_class"
,
handles
=> {
"has_${attr}_class"
=>
'count'
,
"_add_${attr}_class"
=>
'push'
,
},
);
my
$class_builder
= __PACKAGE__ .
"::build_${attr}_class"
;
*$class_builder
= subname
$class_builder
,
sub
{[]};
my
$add_to_class
= __PACKAGE__ .
"::add_${attr}_class"
;
my
$_add_meth
= __PACKAGE__ .
"::_add_${attr}_class"
;
*$add_to_class
= subname
$add_to_class
,
sub
{
shift
->
$_add_meth
((
ref
$_
[0] eq
'ARRAY'
? @{
$_
[0]} :
@_
)); }
}
}
has
'element_wrapper_class'
=> (
is
=>
'rw'
,
isa
=>
'HFH::ArrayRefStr'
,
traits
=> [
'Array'
],
coerce
=> 1,
builder
=>
"build_element_wrapper_class"
,
handles
=> {
has_element_wrapper_class
=>
'count'
,
_add_element_wrapper_class
=>
'push'
,
},
);
sub
add_element_wrapper_class {
shift
->_add_element_wrapper_class((
ref
$_
[0] eq
'ARRAY'
? @{
$_
[0]} :
@_
)); }
sub
build_element_wrapper_class { [] }
sub
element_wrapper_attributes {
my
(
$self
,
$result
) =
@_
;
$result
||=
$self
->result;
my
$attr
= {};
my
$class
= [@{
$self
->element_wrapper_class}];
$self
->add_standard_element_wrapper_classes(
$result
,
$class
);
$attr
->{class} =
$class
if
@$class
;
my
$mod_attr
=
$self
->form->html_attributes(
$self
,
'element_wrapper'
,
$attr
,
$result
)
if
$self
->form;
return
ref
(
$mod_attr
) eq
'HASH'
?
$mod_attr
:
$attr
;
}
sub
add_standard_element_wrapper_classes {
my
(
$self
,
$result
,
$class
) =
@_
;
}
sub
attributes {
shift
->element_attributes(
@_
) }
sub
element_attributes {
my
(
$self
,
$result
) =
@_
;
$result
||=
$self
->result;
my
$attr
= {};
if
(
$self
->form &&
$self
->form->has_flag(
'is_html5'
)) {
$attr
->{required} =
'required'
if
$self
->required;
$attr
->{min} =
$self
->range_start
if
defined
$self
->range_start;
$attr
->{max} =
$self
->range_end
if
defined
$self
->range_end;
}
for
my
$dep_attr
(
'readonly'
,
'disabled'
) {
$attr
->{
$dep_attr
} =
$dep_attr
if
$self
->
$dep_attr
;
}
for
my
$dep_attr
(
'style'
,
'title'
,
'tabindex'
) {
$attr
->{
$dep_attr
} =
$self
->
$dep_attr
if
defined
$self
->
$dep_attr
;
}
$attr
= {
%$attr
, %{
$self
->element_attr}};
my
$class
= [@{
$self
->element_class}];
$self
->add_standard_element_classes(
$result
,
$class
);
$attr
->{class} =
$class
if
@$class
;
my
$mod_attr
=
$self
->form->html_attributes(
$self
,
'element'
,
$attr
,
$result
)
if
$self
->form;
return
ref
(
$mod_attr
) eq
'HASH'
?
$mod_attr
:
$attr
;
}
sub
add_standard_element_classes {
my
(
$self
,
$result
,
$class
) =
@_
;
push
@$class
,
'error'
if
$result
->has_errors;
push
@$class
,
'warning'
if
$result
->has_warnings;
push
@$class
,
'disabled'
if
$self
->disabled;
}
sub
label_attributes {
my
(
$self
,
$result
) =
@_
;
$result
||=
$self
->result;
my
$attr
= {%{
$self
->label_attr}};
my
$class
= [@{
$self
->label_class}];
$self
->add_standard_label_classes(
$result
,
$class
);
$attr
->{class} =
$class
if
@$class
;
my
$mod_attr
=
$self
->form->html_attributes(
$self
,
'label'
,
$attr
,
$result
)
if
$self
->form;
return
ref
(
$mod_attr
) eq
'HASH'
?
$mod_attr
:
$attr
;
}
sub
add_standard_label_classes {
my
(
$self
,
$result
,
$class
) =
@_
;
}
sub
wrapper_attributes {
my
(
$self
,
$result
) =
@_
;
$result
||=
$self
->result;
my
$attr
= {%{
$self
->wrapper_attr}};
my
$class
= [@{
$self
->wrapper_class}];
$self
->add_standard_wrapper_classes(
$result
,
$class
);
$attr
->{class} =
$class
if
@$class
;
$attr
->{id} =
$self
->id
if
(
$self
->has_flag(
'is_compound'
) && not
exists
$attr
->{id} && !
$self
->get_tag(
'no_wrapper_id'
) );
my
$mod_attr
=
$self
->form->html_attributes(
$self
,
'wrapper'
,
$attr
,
$result
)
if
$self
->form;
return
ref
(
$mod_attr
) eq
'HASH'
?
$mod_attr
:
$attr
;
}
sub
add_standard_wrapper_classes {
my
(
$self
,
$result
,
$class
) =
@_
;
push
@$class
,
'error'
if
(
$result
->has_error_results ||
$result
->has_errors );
push
@$class
,
'warning'
if
$result
->has_warnings;
}
sub
wrapper_tag {
my
$self
=
shift
;
return
$self
->get_tag(
'wrapper_tag'
) ||
'div'
;
}
sub
field_filename {
my
$self
=
shift
;
return
'checkbox_tag.tt'
if
$self
->input_type eq
'checkbox'
;
return
'input_tag.tt'
;
}
sub
label_tag {
my
$self
=
shift
;
return
$self
->get_tag(
'label_tag'
) ||
'label'
;
}
has
'writeonly'
=> (
isa
=>
'Bool'
,
is
=>
'rw'
);
has
'noupdate'
=> (
isa
=>
'Bool'
,
is
=>
'rw'
);
sub
convert_full_name {
my
$full_name
=
shift
;
$full_name
=~ s/\.\d+\./_/g;
$full_name
=~ s/\./_/g;
return
$full_name
;
}
has
'validate_method'
=> (
traits
=> [
'Code'
],
is
=>
'ro'
,
isa
=>
'CodeRef'
,
lazy
=> 1,
builder
=>
'build_validate_method'
,
handles
=> {
'_validate'
=>
'execute_method'
},
);
has
'set_validate'
=> (
isa
=>
'Str'
,
is
=>
'ro'
,);
sub
build_validate_method {
my
$self
=
shift
;
my
$set_validate
=
$self
->set_validate;
$set_validate
||=
"validate_"
. convert_full_name(
$self
->full_name);
return
sub
{
my
$self
=
shift
;
$self
->form->
$set_validate
(
$self
); }
if
(
$self
->form &&
$self
->form->can(
$set_validate
) );
return
sub
{ };
}
has
'default_method'
=> (
traits
=> [
'Code'
],
is
=>
'ro'
,
isa
=>
'CodeRef'
,
writer
=>
'_set_default_method'
,
predicate
=>
'has_default_method'
,
handles
=> {
'_default'
=>
'execute_method'
},
);
has
'set_default'
=> (
isa
=>
'Str'
,
is
=>
'ro'
,
writer
=>
'_set_default'
);
sub
build_default_method {
my
$self
=
shift
;
my
$set_default
=
$self
->set_default;
$set_default
||=
"default_"
. convert_full_name(
$self
->full_name);
if
(
$self
->form &&
$self
->form->can(
$set_default
) ) {
$self
->_set_default_method(
sub
{
my
$self
=
shift
;
return
$self
->form->
$set_default
(
$self
,
$self
->form->item); }
);
}
}
sub
get_default_value {
my
$self
=
shift
;
if
(
$self
->has_default_method ) {
return
$self
->_default;
}
elsif
(
defined
$self
->
default
) {
return
$self
->
default
;
}
return
;
}
{
foreach
my
$type
(
'inflate_default'
,
'deflate_value'
,
'inflate'
,
'deflate'
) {
has
"${type}_method"
=> (
is
=>
'ro'
,
traits
=> [
'Code'
],
isa
=>
'CodeRef'
,
writer
=>
"_set_${type}_method"
,
predicate
=>
"has_${type}_method"
,
handles
=> {
$type
=>
'execute_method'
,
},
);
}
}
has
'deflation'
=> (
is
=>
'rw'
,
predicate
=>
'has_deflation'
,
);
has
'trim'
=> (
is
=>
'rw'
,
default
=>
sub
{ {
transform
=> \
&default_trim
} }
);
sub
default_trim {
my
$value
=
shift
;
return
unless
defined
$value
;
my
@values
=
ref
$value
eq
'ARRAY'
?
@$value
: (
$value
);
for
(
@values
) {
next
if
ref
$_
or !
defined
;
s/^\s+//;
s/\s+$//;
}
return
ref
$value
eq
'ARRAY'
? \
@values
:
$values
[0];
}
has
'render_filter'
=> (
traits
=> [
'Code'
],
is
=>
'ro'
,
isa
=>
'CodeRef'
,
lazy
=> 1,
builder
=>
'build_render_filter'
,
handles
=> {
html_filter
=>
'execute'
},
);
sub
build_render_filter {
my
$self
=
shift
;
if
(
$self
->form &&
$self
->form->can(
'render_filter'
) ) {
my
$coderef
=
$self
->form->can(
'render_filter'
);
return
$coderef
;
}
else
{
return
\
&default_render_filter
;
}
}
sub
default_render_filter {
my
$string
=
shift
;
return
''
if
(!
defined
$string
);
$string
=~ s/&/
&
;/g;
$string
=~ s/</
<
;/g;
$string
=~ s/>/
>
;/g;
$string
=~ s/"/
"
;/g;
return
$string
;
}
has
'input_param'
=> (
is
=>
'rw'
,
isa
=>
'Str'
);
has
'language_handle'
=> (
isa
=> duck_type( [
qw(maketext)
] ),
is
=>
'rw'
,
reader
=>
'get_language_handle'
,
writer
=>
'set_language_handle'
,
predicate
=>
'has_language_handle'
);
sub
language_handle {
my
(
$self
,
$value
) =
@_
;
if
(
$value
) {
$self
->set_language_handle(
$value
);
return
;
}
return
$self
->get_language_handle
if
(
$self
->has_language_handle );
return
$self
->form->language_handle
if
(
$self
->has_form );
my
$lh
;
if
(
$ENV
{LANGUAGE_HANDLE} ) {
if
( blessed
$ENV
{LANGUAGE_HANDLE} ) {
$lh
=
$ENV
{LANGUAGE_HANDLE};
}
else
{
$lh
= HTML::FormHandler::I18N->get_handle(
$ENV
{LANGUAGE_HANDLE} );
}
}
else
{
$lh
= HTML::FormHandler::I18N->get_handle;
}
$self
->set_language_handle(
$lh
);
return
$lh
;
}
has
'localize_meth'
=> (
traits
=> [
'Code'
],
is
=>
'ro'
,
isa
=>
'CodeRef'
,
lazy
=> 1,
builder
=>
'build_localize_meth'
,
handles
=> {
'_localize'
=>
'execute_method'
},
);
sub
build_localize_meth {
my
$self
=
shift
;
if
(
$self
->form &&
$self
->form->can(
'localize_meth'
) ) {
my
$coderef
=
$self
->form->can(
'localize_meth'
);
return
$coderef
;
}
else
{
return
\
&default_localize
;
}
}
sub
default_localize {
my
(
$self
,
@message
) =
@_
;
my
$message
=
$self
->language_handle->maketext(
@message
);
return
$message
;
}
has
'messages'
=> (
is
=>
'rw'
,
isa
=>
'HashRef'
,
traits
=> [
'Hash'
],
default
=>
sub
{{}},
handles
=> {
'_get_field_message'
=>
'get'
,
'_has_field_message'
=>
'exists'
,
'set_message'
=>
'set'
,
},
);
our
$class_messages
= {
'field_invalid'
=>
'field is invalid'
,
'range_too_low'
=>
'Value must be greater than or equal to [_1]'
,
'range_too_high'
=>
'Value must be less than or equal to [_1]'
,
'range_incorrect'
=>
'Value must be between [_1] and [_2]'
,
'wrong_value'
=>
'Wrong value'
,
'no_match'
=>
'[_1] does not match'
,
'not_allowed'
=>
'[_1] not allowed'
,
'error_occurred'
=>
'error occurred'
,
'required'
=>
'[_1] field is required'
,
'unique'
=>
'Duplicate value for [_1]'
,
};
sub
get_class_messages {
my
$self
=
shift
;
my
$messages
= {
%$class_messages
};
$messages
->{required} =
$self
->required_message
if
$self
->required_message;
$messages
->{unique} =
$self
->unique_message
if
$self
->unique_message;
return
$messages
;
}
sub
get_message {
my
(
$self
,
$msg
) =
@_
;
return
$self
->_get_field_message(
$msg
)
if
$self
->_has_field_message(
$msg
);
return
$self
->form->_get_form_message(
$msg
)
if
$self
->has_form &&
$self
->form->_has_form_message(
$msg
);
return
$self
->get_class_messages->{
$msg
};
}
sub
all_messages {
my
$self
=
shift
;
my
$form_messages
=
$self
->has_form ?
$self
->form->messages : {};
my
$field_messages
=
$self
->messages || {};
my
$lclass_messages
=
$self
->my_class_messages || {};
return
{%{
$lclass_messages
}, %{
$form_messages
}, %{
$field_messages
}};
}
sub
BUILDARGS {
my
$class
=
shift
;
my
@new
;
push
@new
, (
'element_attr'
, {
@_
}->{html_attr} )
if
(
exists
{
@_
}->{html_attr} );
push
@new
, (
'do_label'
, !{
@_
}->{no_render_label} )
if
(
exists
{
@_
}->{no_render_label} );
return
$class
->SUPER::BUILDARGS(
@_
,
@new
);
}
sub
BUILD {
my
(
$self
,
$params
) =
@_
;
$self
->merge_tags(
$self
->wrapper_tags)
if
$self
->has_wrapper_tags;
$self
->build_default_method;
$self
->validate_method;
$self
->add_widget_name_space(
$self
->form->widget_name_space )
if
$self
->form;
$self
->add_action(
$self
->trim )
if
$self
->trim;
$self
->_build_apply_list;
$self
->add_action( @{
$params
->{apply} } )
if
$params
->{apply};
}
sub
_result_from_fields {
my
(
$self
,
$result
) =
@_
;
if
(
$self
->disabled &&
$self
->has_init_value ) {
$result
->_set_value(
$self
->init_value);
}
elsif
(
my
@values
=
$self
->get_default_value ) {
if
(
$self
->has_inflate_default_method ) {
@values
=
$self
->inflate_default(
@values
);
}
my
$value
=
@values
> 1 ? \
@values
:
shift
@values
;
$self
->init_value(
$value
)
if
defined
$value
;
$result
->_set_value(
$value
)
if
defined
$value
;
}
$self
->_set_result(
$result
);
$result
->_set_field_def(
$self
);
return
$result
;
}
sub
_result_from_input {
my
(
$self
,
$result
,
$input
,
$exists
) =
@_
;
if
(
$exists
) {
$result
->_set_input(
$input
);
}
elsif
(
$self
->disabled ) {
return
$self
->_result_from_fields(
$result
);
}
elsif
(
$self
->form &&
$self
->form->use_fields_for_input_without_param ) {
return
$self
->_result_from_fields(
$result
);
}
elsif
(
$self
->has_input_without_param ) {
$result
->_set_input(
$self
->input_without_param );
}
$self
->_set_result(
$result
);
$result
->_set_field_def(
$self
);
return
$result
;
}
sub
_result_from_object {
my
(
$self
,
$result
,
$value
) =
@_
;
$self
->_set_result(
$result
);
if
(
$self
->form ) {
$self
->form->init_value(
$self
,
$value
);
}
else
{
$self
->init_value(
$value
);
$result
->_set_value(
$value
);
}
$result
->_set_value(
undef
)
if
$self
->writeonly;
$result
->_set_field_def(
$self
);
return
$result
;
}
sub
full_name {
my
$field
=
shift
;
my
$name
=
$field
->name;
my
$parent_name
;
if
(
$field
->parent ) {
$parent_name
=
$field
->parent->full_name;
}
return
$name
unless
defined
$parent_name
&&
length
$parent_name
;
return
$parent_name
.
'.'
.
$name
;
}
sub
full_accessor {
my
$field
=
shift
;
my
$parent
=
$field
->parent;
if
(
$field
->is_contains ) {
return
''
unless
$parent
;
return
$parent
->full_accessor;
}
my
$accessor
=
$field
->accessor;
my
$parent_accessor
;
if
(
$parent
) {
$parent_accessor
=
$parent
->full_accessor;
}
return
$accessor
unless
defined
$parent_accessor
&&
length
$parent_accessor
;
return
$parent_accessor
.
'.'
.
$accessor
;
}
sub
add_error {
my
(
$self
,
@message
) =
@_
;
unless
(
defined
$message
[0] ) {
@message
= (
$class_messages
->{field_invalid});
}
@message
= @{
$message
[0]}
if
ref
$message
[0] eq
'ARRAY'
;
my
$out
;
try
{
$out
=
$self
->_localize(
@message
);
}
catch
{
die
"Error occurred localizing error message for "
.
$self
->label .
". Check brackets. $_"
;
};
return
$self
->push_errors(
$out
);;
}
sub
push_errors {
my
$self
=
shift
;
$self
->_push_errors(
@_
);
if
(
$self
->parent ) {
$self
->parent->propagate_error(
$self
->result);
}
return
;
}
sub
_apply_deflation {
my
(
$self
,
$value
) =
@_
;
if
(
$self
->has_deflation ) {
$value
=
$self
->deflation->(
$value
);
}
elsif
(
$self
->has_deflate_method ) {
$value
=
$self
->deflate(
$value
);
}
return
$value
;
}
sub
_can_deflate {
my
$self
=
shift
;
return
$self
->has_deflation ||
$self
->has_deflate_method;
}
sub
clone {
my
(
$self
,
%params
) =
@_
;
$self
->meta->clone_object(
$self
,
%params
);
}
sub
value_changed {
my
(
$self
) =
@_
;
my
@cmp
;
for
(
'init_value'
,
'value'
) {
my
$val
=
$self
->
$_
;
$val
=
''
unless
defined
$val
;
push
@cmp
,
join
'|'
,
sort
map
{
ref
(
$_
) &&
$_
->isa(
'DateTime'
) ?
$_
->iso8601 :
"$_"
}
ref
(
$val
) eq
'ARRAY'
?
@$val
:
$val
;
}
return
$cmp
[0] ne
$cmp
[1];
}
sub
required_text {
shift
->required ?
'required'
:
'optional'
}
sub
input_defined {
my
(
$self
) =
@_
;
return
unless
$self
->has_input;
return
has_some_value(
$self
->input );
}
sub
dump
{
my
$self
=
shift
;
warn
"HFH: ----- "
,
$self
->name,
" -----\n"
;
warn
"HFH: type: "
,
$self
->type,
"\n"
;
warn
"HFH: required: "
, (
$self
->required ||
'0'
),
"\n"
;
warn
"HFH: label: "
,
$self
->label,
"\n"
;
warn
"HFH: widget: "
,
$self
->widget ||
''
,
"\n"
;
my
$v
=
$self
->value;
warn
"HFH: value: "
, Data::Dumper::Dumper(
$v
)
if
$v
;
my
$iv
=
$self
->init_value;
warn
"HFH: init_value: "
, Data::Dumper::Dumper(
$iv
)
if
$iv
;
my
$i
=
$self
->input;
warn
"HFH: input: "
, Data::Dumper::Dumper(
$i
)
if
$i
;
my
$fif
=
$self
->fif;
warn
"HFH: fif: "
, Data::Dumper::Dumper(
$fif
)
if
$fif
;
if
(
$self
->can(
'options'
) ) {
my
$o
=
$self
->options;
warn
"HFH: options: "
. Data::Dumper::Dumper(
$o
);
}
}
sub
apply_rendering_widgets {
my
$self
=
shift
;
if
(
$self
->widget ) {
warn
"in apply_rendering_widgets "
.
$self
->widget .
" Field\n"
;
$self
->apply_widget_role(
$self
,
$self
->widget,
'Field'
);
}
my
$widget_wrapper
=
$self
->widget_wrapper;
$widget_wrapper
||=
$self
->form->widget_wrapper
if
$self
->form;
$widget_wrapper
||=
'Simple'
;
unless
(
$widget_wrapper
eq
'none'
) {
$self
->apply_widget_role(
$self
,
$widget_wrapper
,
'Wrapper'
);
}
return
;
}
sub
peek {
my
(
$self
,
$indent
) =
@_
;
$indent
||=
''
;
my
$string
=
$indent
.
'field: "'
.
$self
->name .
'" type: '
.
$self
->type .
"\n"
;
if
(
$self
->has_flag(
'has_contains'
) ) {
$string
.=
$indent
.
"contains: \n"
;
my
$lindent
=
$indent
.
' '
;
foreach
my
$field
(
$self
->contains->sorted_fields ) {
$string
.=
$field
->peek(
$lindent
);
}
}
if
(
$self
->has_fields ) {
$string
.=
$indent
.
'subfields of "'
.
$self
->name .
'": '
.
$self
->num_fields .
"\n"
;
my
$lindent
=
$indent
.
' '
;
foreach
my
$field
(
$self
->sorted_fields ) {
$string
.=
$field
->peek(
$lindent
);
}
}
return
$string
;
}
sub
has_some_value {
my
$x
=
shift
;
return
unless
defined
$x
;
return
$x
=~ /\S/
if
!
ref
$x
;
if
(
ref
$x
eq
'ARRAY'
) {
for
my
$elem
(
@$x
) {
return
1
if
has_some_value(
$elem
);
}
return
0;
}
if
(
ref
$x
eq
'HASH'
) {
for
my
$key
(
keys
%$x
) {
return
1
if
has_some_value(
$x
->{
$key
} );
}
return
0;
}
return
1
if
blessed(
$x
);
return
1
if
ref
(
$x
);
return
;
}
sub
apply_traits {
my
(
$class
,
@traits
) =
@_
;
$class
->meta->make_mutable;
Moose::Util::apply_all_roles(
$class
->meta,
@traits
);
$class
->meta->make_immutable;
}
__PACKAGE__->meta->make_immutable;
1;