<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package HTML::FormHandler::Field;
# ABSTRACT: base class for fields
$HTML::FormHandler::Field::VERSION = '0.40068';
use HTML::FormHandler::Moose;
use HTML::FormHandler::Field::Result;
use Try::Tiny;
use Moose::Util::TypeConstraints;
use HTML::FormHandler::Merge ('merge');
use HTML::FormHandler::Render::Util('cc_widget', 'ucc_widget');
use Sub::Name;

with 'HTML::FormHandler::Traits';
with 'HTML::FormHandler::Validate';
with 'HTML::FormHandler::Widget::ApplyRole';
with 'HTML::FormHandler::TraitFor::Types';


has 'name' =&gt; ( isa =&gt; 'Str', is =&gt; 'rw', required =&gt; 1 );
has 'type' =&gt; ( isa =&gt; 'Str', is =&gt; 'rw', default =&gt; sub { ref shift } );
has 'parent' =&gt; ( is  =&gt; 'rw',   predicate =&gt; 'has_parent', weak_ref =&gt; 1 );
sub has_fields { }
has 'input_without_param' =&gt; (
    is        =&gt; 'rw',
    predicate =&gt; 'has_input_without_param'
);
has 'not_nullable' =&gt; ( is =&gt; 'rw', isa =&gt; 'Bool' );
has 'no_value_if_empty' =&gt; ( is =&gt; 'rw', isa =&gt; 'Bool' );
has 'validate_when_empty' =&gt; ( is =&gt; 'rw', isa =&gt; 'Bool' );
has 'init_value' =&gt; ( is =&gt; 'rw', clearer =&gt; 'clear_init_value', predicate =&gt; 'has_init_value' );
has 'default' =&gt; ( is =&gt; 'rw' );
has 'default_over_obj' =&gt; ( is =&gt; 'rw', builder =&gt; 'build_default_over_obj' );
sub build_default_over_obj { }
has 'result' =&gt; (
    isa       =&gt; 'HTML::FormHandler::Field::Result',
    is        =&gt; 'ro',
    weak_ref  =&gt; 1,
    clearer   =&gt; 'clear_result',
    predicate =&gt; 'has_result',
    writer    =&gt; '_set_result',
    handles   =&gt; [
        '_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' =&gt; ( is =&gt; 'ro', reader =&gt; '_get_pin_result', writer =&gt; '_set_pin_result' );

sub has_input {
    my $self = shift;
    return unless $self-&gt;has_result;
    return $self-&gt;result-&gt;has_input;
}

sub has_value {
    my $self = shift;
    return unless $self-&gt;has_result;
    return $self-&gt;result-&gt;has_value;
}

# these should normally only be called for field tests
sub reset_result {
    my $self = shift;
    $self-&gt;clear_result;
    $self-&gt;build_result;
}
sub build_result {
    my $self = shift;
    my @parent = ( 'parent' =&gt; $self-&gt;parent-&gt;result )
        if ( $self-&gt;parent &amp;&amp; $self-&gt;parent-&gt;result );
    my $result = HTML::FormHandler::Field::Result-&gt;new(
        name      =&gt; $self-&gt;name,
        field_def =&gt; $self,
        @parent
    );
    $self-&gt;_set_pin_result($result);    # to prevent garbage collection of result
    $self-&gt;_set_result($result);
}

sub input {
    my $self = shift;

    # allow testing fields individually by creating result if no form
    return undef unless $self-&gt;has_result || !$self-&gt;form;
    my $result = $self-&gt;result;
    return $result-&gt;_set_input(@_) if @_;
    return $result-&gt;input;
}

sub value {
    my $self = shift;

    # allow testing fields individually by creating result if no form
    return undef unless $self-&gt;has_result || !$self-&gt;form;
    my $result = $self-&gt;result;
    return undef unless $result;
    return $result-&gt;_set_value(@_) if @_;
    return $result-&gt;value;
}
# for compatibility. deprecate and remove at some point
sub clear_input { shift-&gt;_clear_input }
sub clear_value { shift-&gt;_clear_value }
sub clear_data  {
    my $self = shift;
    $self-&gt;clear_result;
    $self-&gt;clear_active;
}
# this is a kludge to allow testing field deflation
sub _deflate_and_set_value {
    my ( $self, $value ) = @_;
    if( $self-&gt;_can_deflate ) {
        $value = $self-&gt;_apply_deflation($value);
    }
    $self-&gt;_set_value($value);
}

sub is_repeatable { }

has 'fif_from_value' =&gt; ( isa =&gt; 'Str', is =&gt; 'ro' );

sub fif {
    my ( $self, $result ) = @_;

    return if ( $self-&gt;inactive &amp;&amp; !$self-&gt;_active );
    return '' if $self-&gt;password;
    return unless $result || $self-&gt;has_result;
    my $lresult = $result || $self-&gt;result;
    if ( ( $self-&gt;has_result &amp;&amp; $self-&gt;has_input &amp;&amp; !$self-&gt;fif_from_value ) ||
        ( $self-&gt;fif_from_value &amp;&amp; !defined $lresult-&gt;value ) )
    {
        return defined $lresult-&gt;input ? $lresult-&gt;input : '';
    }
    if ( $lresult-&gt;has_value ) {
        my $value;
        if( $self-&gt;_can_deflate ) {
            $value = $self-&gt;_apply_deflation($lresult-&gt;value);
        }
        else {
            $value = $lresult-&gt;value;
        }
        return ( defined $value ? $value : '' );
    }
    elsif ( defined $self-&gt;value ) {
        # this is because checkboxes and submit buttons have their own 'value'
        # needs to be fixed in some better way
        return $self-&gt;value;
    }
    return '';
}

has 'accessor' =&gt; (
    isa     =&gt; 'Str',
    is      =&gt; 'rw',
    lazy    =&gt; 1,
    default =&gt; sub {
        my $self     = shift;
        my $accessor = $self-&gt;name;
        $accessor =~ s/^(.*)\.//g if ( $accessor =~ /\./ );
        return $accessor;
    }
);
has 'is_contains' =&gt; ( is =&gt; 'rw', isa =&gt; 'Bool' );
has 'temp' =&gt; ( is =&gt; 'rw' );

sub has_flag {
    my ( $self, $flag_name ) = @_;
    return unless $self-&gt;can($flag_name);
    return $self-&gt;$flag_name;
}

has 'label' =&gt; (
    isa     =&gt; 'Maybe[Str]',
    is      =&gt; 'rw',
    lazy    =&gt; 1,
    builder =&gt; 'build_label',
);
has 'do_label' =&gt; ( isa =&gt; 'Bool', is =&gt; 'rw', default =&gt; 1 );
has 'build_label_method' =&gt; ( is =&gt; 'rw', isa =&gt; 'CodeRef',
    traits =&gt; ['Code'], handles =&gt; { 'build_label' =&gt; 'execute_method' },
    default =&gt; sub { \&amp;default_build_label },
);
sub default_build_label {
    my $self = shift;
    my $label = $self-&gt;name;
    $label =~ s/_/ /g;
    $label = ucfirst($label);
    return $label;
}
sub loc_label {
    my $self = shift;
    return $self-&gt;_localize($self-&gt;label);
}
has 'wrap_label_method' =&gt; (
   traits =&gt; ['Code'],
   is     =&gt; 'ro',
   isa    =&gt; 'CodeRef',
   predicate =&gt; 'does_wrap_label',
   handles =&gt; { 'wrap_label' =&gt; 'execute_method' },
);
has 'title'     =&gt; ( isa =&gt; 'Str', is =&gt; 'rw' );
has 'style'     =&gt; ( isa =&gt; 'Str', is =&gt; 'rw' );
has 'form'      =&gt; (
    isa =&gt; 'HTML::FormHandler',
    is =&gt; 'rw',
    weak_ref =&gt; 1,
    predicate =&gt; 'has_form',
);
sub is_form { 0 }
has 'html_name' =&gt; (
    isa     =&gt; 'Str',
    is      =&gt; 'rw',
    lazy    =&gt; 1,
    builder =&gt; 'build_html_name'
);

sub build_html_name {
    my $self = shift;
    my $prefix = ( $self-&gt;form &amp;&amp; $self-&gt;form-&gt;html_prefix ) ? $self-&gt;form-&gt;name . "." : '';
    return $prefix . $self-&gt;full_name;
}
has 'widget'            =&gt; ( isa =&gt; 'Str',  is =&gt; 'rw' );
has 'widget_wrapper'    =&gt; ( isa =&gt; 'Str',  is =&gt; 'rw' );
has 'do_wrapper'    =&gt; ( is =&gt; 'rw', default =&gt; 1 );
sub wrapper { shift-&gt;widget_wrapper || '' }
sub uwrapper { ucc_widget( shift-&gt;widget_wrapper || '' ) || 'simple' }
sub twrapper { shift-&gt;uwrapper . ".tt" }
sub uwidget { ucc_widget( shift-&gt;widget || '' ) || 'simple' }
sub twidget { shift-&gt;uwidget . ".tt" }
# for use of wrapper classes
has 'wrapper_tags' =&gt; (
    isa =&gt; 'HashRef',
    traits =&gt; ['Hash'],
    is =&gt; 'rw',
    builder =&gt; 'build_wrapper_tags',
    handles =&gt; {
        has_wrapper_tags =&gt; 'count'
    }
);
sub build_wrapper_tags { {} }
has 'tags'         =&gt; (
    traits =&gt; ['Hash'],
    isa =&gt; 'HashRef',
    is =&gt; 'rw',
    builder =&gt; 'build_tags',
    handles =&gt; {
      _get_tag =&gt; 'get',
      set_tag =&gt; 'set',
      has_tag =&gt; 'exists',
      tag_exists =&gt; 'exists',
      delete_tag =&gt; 'delete',
    },
);
sub build_tags {{}}
sub merge_tags {
    my ( $self, $new ) = @_;
    my $old = $self-&gt;tags;
    $self-&gt;tags( merge($new, $old) );
}
sub get_tag {
    my ( $self, $name ) = @_;
    return '' unless $self-&gt;tag_exists($name);
    my $tag = $self-&gt;_get_tag($name);
    return $self-&gt;$tag if ref $tag eq 'CODE';
    return $tag unless $tag =~ /^%/;
    ( my $block_name = $tag ) =~ s/^%//;
    return $self-&gt;form-&gt;block($block_name)-&gt;render
        if ( $self-&gt;form &amp;&amp; $self-&gt;form-&gt;block_exists($block_name) );
    return '';
}

has 'widget_name_space' =&gt; (
    isa =&gt; 'HFH::ArrayRefStr',
    is =&gt; 'rw',
    traits =&gt; ['Array'],
    default =&gt; sub {[]},
    coerce =&gt; 1,
    handles =&gt; {
        push_widget_name_space =&gt; 'push',
    },
);

sub add_widget_name_space {
    my ( $self, @ns ) = @_;
    @ns = @{$ns[0]}if( scalar @ns &amp;&amp; ref $ns[0] eq 'ARRAY' );
    $self-&gt;push_widget_name_space(@ns);
}

has 'order'             =&gt; ( isa =&gt; 'Int',  is =&gt; 'rw', default =&gt; 0 );
# 'inactive' is set in the field declaration, and is static. Default status.
has 'inactive'          =&gt; ( isa =&gt; 'Bool', is =&gt; 'rw', clearer =&gt; 'clear_inactive' );
# 'active' is cleared whenever the form is cleared. Ephemeral activation.
has '_active'         =&gt; ( isa =&gt; 'Bool', is =&gt; 'rw', clearer =&gt; 'clear_active', predicate =&gt; 'has__active' );
sub is_active {
    my $self = shift;
    return ! $self-&gt;is_inactive;
}
sub is_inactive {
    my $self = shift;
    return (($self-&gt;inactive &amp;&amp; !$self-&gt;_active) || (!$self-&gt;inactive &amp;&amp; $self-&gt;has__active &amp;&amp; $self-&gt;_active == 0 ) );
}
has 'id'                =&gt; ( isa =&gt; 'Str',  is =&gt; 'rw', lazy =&gt; 1, builder =&gt; 'build_id' );
has 'build_id_method' =&gt; ( is =&gt; 'rw', isa =&gt; 'CodeRef', traits =&gt; ['Code'],
    default =&gt; sub { sub { shift-&gt;html_name } },
    handles =&gt; { build_id =&gt; 'execute_method' },
);

# html attributes
has 'password'   =&gt; ( isa =&gt; 'Bool', is =&gt; 'rw' );
has 'disabled'   =&gt; ( isa =&gt; 'Bool', is =&gt; 'rw' );
has 'readonly'   =&gt; ( isa =&gt; 'Bool', is =&gt; 'rw' );
has 'tabindex' =&gt; ( is =&gt; 'rw', isa =&gt; 'Int' );

sub html_element { 'input' }
has 'type_attr' =&gt; ( is =&gt; 'rw', isa =&gt; 'Str', default =&gt; 'text' );
has 'html5_type_attr' =&gt; ( isa =&gt; 'Str', is =&gt; 'ro', default =&gt; 'text' );
sub input_type {
    my $self = shift;
    return $self-&gt;html5_type_attr if ( $self-&gt;form &amp;&amp; $self-&gt;form-&gt;has_flag('is_html5') );
    return $self-&gt;type_attr;
}
# temporary methods for compatibility after name change
sub html_attr { shift-&gt;element_attr(@_) }
sub has_html_attr { shift-&gt;has_element_attr(@_) }
sub get_html_attr { shift-&gt;get_element_attr(@_) }
sub set_html_attr { shift-&gt;set_element_attr(@_) }

{
    # create the attributes and methods for
    # element_attr, build_element_attr, element_class,
    # label_attr, build_label_attr, label_class,
    # wrapper_attr, build_wrapper_atrr, wrapper_class
    no strict 'refs';
    foreach my $attr ('wrapper', 'element', 'label' ) {
        # trigger to move 'class' set via _attr to the class slot
        my $add_meth = "add_${attr}_class";
        my $trigger_sub = sub {
            my ( $self, $value ) = @_;
            if( my $class = delete $self-&gt;{"${attr}_attr"}-&gt;{class} ) {
                $self-&gt;$add_meth($class);
            }
        };
        has "${attr}_attr" =&gt; ( is =&gt; 'rw', traits =&gt; ['Hash'],
            builder =&gt; "build_${attr}_attr",
            handles =&gt; {
                "has_${attr}_attr" =&gt; 'count',
                "get_${attr}_attr" =&gt; 'get',
                "set_${attr}_attr" =&gt; 'set',
                "delete_${attr}_attr" =&gt; 'delete',
                "exists_${attr}_attr" =&gt; 'exists',
            },
            trigger =&gt; $trigger_sub,
        );
        # create builders fo _attrs
        my $attr_builder = __PACKAGE__ . "::build_${attr}_attr";
        *$attr_builder = subname $attr_builder, sub {{}};
        # create the 'class' slots
        has "${attr}_class" =&gt; ( is =&gt; 'rw', isa =&gt; 'HFH::ArrayRefStr',
            traits =&gt; ['Array'],
            coerce =&gt; 1,
            builder =&gt; "build_${attr}_class",
            handles =&gt; {
                "has_${attr}_class" =&gt; 'count',
                "_add_${attr}_class" =&gt; 'push',
           },
        );
        # create builders for classes
        my $class_builder = __PACKAGE__ . "::build_${attr}_class";
        *$class_builder = subname $class_builder, sub {[]};
        # create wrapper for add_to_ to accept arrayref
        my $add_to_class = __PACKAGE__ . "::add_${attr}_class";
        my $_add_meth = __PACKAGE__ . "::_add_${attr}_class";
        *$add_to_class = subname $add_to_class, sub { shift-&gt;$_add_meth((ref $_[0] eq 'ARRAY' ? @{$_[0]} : @_)); }
    }
}

# we're assuming that the only attribute we want in an element wrapper is a class
has 'element_wrapper_class' =&gt; (
    is =&gt; 'rw', isa =&gt; 'HFH::ArrayRefStr',
    traits =&gt; ['Array'],
    coerce =&gt; 1,
    builder =&gt; "build_element_wrapper_class",
    handles =&gt; {
        has_element_wrapper_class =&gt; 'count',
        _add_element_wrapper_class =&gt; 'push',
    },
);
sub add_element_wrapper_class { shift-&gt;_add_element_wrapper_class((ref $_[0] eq 'ARRAY' ? @{$_[0]} : @_)); }
sub build_element_wrapper_class { [] }
sub element_wrapper_attributes {
    my ( $self, $result ) = @_;
    $result ||= $self-&gt;result;
    # local copy of label_attr
    my $attr = {};
    my $class = [@{$self-&gt;element_wrapper_class}];
    $self-&gt;add_standard_element_wrapper_classes( $result, $class );
    $attr-&gt;{class} = $class if @$class;
    # call form hook
    my $mod_attr = $self-&gt;form-&gt;html_attributes($self, 'element_wrapper', $attr, $result) if $self-&gt;form;
    return ref($mod_attr) eq 'HASH' ? $mod_attr : $attr;
}
sub add_standard_element_wrapper_classes {
    my ( $self, $result, $class ) = @_;
}

sub attributes { shift-&gt;element_attributes(@_) }
sub element_attributes {
    my ( $self, $result ) = @_;
    $result ||= $self-&gt;result;
    my $attr = {};
    # handle html5 attributes
    if ($self-&gt;form &amp;&amp; $self-&gt;form-&gt;has_flag('is_html5')) {
        $attr-&gt;{required} = 'required' if $self-&gt;required;
        $attr-&gt;{min} = $self-&gt;range_start if defined $self-&gt;range_start;
        $attr-&gt;{max} = $self-&gt;range_end if defined $self-&gt;range_end;
    }
    # pull in deprecated attributes for backward compatibility
    for my $dep_attr ( 'readonly', 'disabled' ) {
        $attr-&gt;{$dep_attr} = $dep_attr if $self-&gt;$dep_attr;
    }
    for my $dep_attr ( 'style', 'title', 'tabindex' ) {
        $attr-&gt;{$dep_attr} = $self-&gt;$dep_attr if defined $self-&gt;$dep_attr;
    }
    $attr = {%$attr, %{$self-&gt;element_attr}};
    my $class = [@{$self-&gt;element_class}];
    $self-&gt;add_standard_element_classes($result, $class);
    $attr-&gt;{class} = $class if @$class;
    # call form hook
    my $mod_attr = $self-&gt;form-&gt;html_attributes($self, 'element', $attr, $result) if $self-&gt;form;
    return ref($mod_attr) eq 'HASH' ? $mod_attr : $attr;
}

sub add_standard_element_classes {
    my ( $self, $result, $class ) = @_;
    push @$class, 'error' if $result-&gt;has_errors;
    push @$class, 'warning' if $result-&gt;has_warnings;
    push @$class, 'disabled' if $self-&gt;disabled;
}

sub label_attributes {
    my ( $self, $result ) = @_;
    $result ||= $self-&gt;result;
    # local copy of label_attr
    my $attr = {%{$self-&gt;label_attr}};
    my $class = [@{$self-&gt;label_class}];
    $self-&gt;add_standard_label_classes($result, $class);
    $attr-&gt;{class} = $class if @$class;
    # call form hook
    my $mod_attr = $self-&gt;form-&gt;html_attributes($self, 'label', $attr, $result) if $self-&gt;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-&gt;result;
    # copy wrapper
    my $attr = {%{$self-&gt;wrapper_attr}};
    my $class = [@{$self-&gt;wrapper_class}];
    # add 'error' to class
    $self-&gt;add_standard_wrapper_classes($result, $class);
    $attr-&gt;{class} = $class if @$class;
    # add id if compound field and id doesn't exist unless 'no_wrapper_id' tag
    $attr-&gt;{id} = $self-&gt;id
        if ( $self-&gt;has_flag('is_compound') &amp;&amp; not exists $attr-&gt;{id} &amp;&amp; ! $self-&gt;get_tag('no_wrapper_id') );
    # call form hook
    my $mod_attr = $self-&gt;form-&gt;html_attributes($self, 'wrapper', $attr, $result) if $self-&gt;form;
    return ref($mod_attr) eq 'HASH' ? $mod_attr : $attr;
}

sub add_standard_wrapper_classes {
    my ( $self, $result, $class ) = @_;
    push @$class, 'error' if ( $result-&gt;has_error_results || $result-&gt;has_errors );
    push @$class, 'warning' if $result-&gt;has_warnings;
}


sub wrapper_tag {
    my $self = shift;
    return $self-&gt;get_tag('wrapper_tag') || 'div';
}

#=====================
# these may be temporary
sub field_filename {
    my $self = shift;
    return 'checkbox_tag.tt' if $self-&gt;input_type eq 'checkbox';
    return 'input_tag.tt';
}
sub label_tag {
    my $self = shift;
    return $self-&gt;get_tag('label_tag') || 'label';
}
#===================

has 'writeonly'  =&gt; ( isa =&gt; 'Bool', is =&gt; 'rw' );
has 'noupdate'   =&gt; ( isa =&gt; 'Bool', is =&gt; 'rw' );

#==============
sub convert_full_name {
    my $full_name = shift;
    $full_name =~ s/\.\d+\./_/g;
    $full_name =~ s/\./_/g;
    return $full_name;
}
has 'validate_method' =&gt; (
     traits =&gt; ['Code'],
     is     =&gt; 'ro',
     isa    =&gt; 'CodeRef',
     lazy   =&gt; 1,
     builder =&gt; 'build_validate_method',
     handles =&gt; { '_validate' =&gt; 'execute_method' },
);
has 'set_validate' =&gt; ( isa =&gt; 'Str', is =&gt; 'ro',);
sub build_validate_method {
    my $self = shift;
    my $set_validate = $self-&gt;set_validate;
    $set_validate ||= "validate_" . convert_full_name($self-&gt;full_name);
    return sub { my $self = shift; $self-&gt;form-&gt;$set_validate($self); }
        if ( $self-&gt;form &amp;&amp; $self-&gt;form-&gt;can($set_validate) );
    return sub { };
}

has 'default_method' =&gt; (
     traits =&gt; ['Code'],
     is     =&gt; 'ro',
     isa    =&gt; 'CodeRef',
     writer =&gt; '_set_default_method',
     predicate =&gt; 'has_default_method',
     handles =&gt; { '_default' =&gt; 'execute_method' },
);
has 'set_default' =&gt; ( isa =&gt; 'Str', is =&gt; 'ro', writer =&gt; '_set_default');
# this is not a "true" builder, because sometimes 'default_method' is not set
sub build_default_method {
    my $self = shift;
    my $set_default = $self-&gt;set_default;
    $set_default ||= "default_" . convert_full_name($self-&gt;full_name);
    if ( $self-&gt;form &amp;&amp; $self-&gt;form-&gt;can($set_default) ) {
        $self-&gt;_set_default_method(
            sub { my $self = shift; return $self-&gt;form-&gt;$set_default($self, $self-&gt;form-&gt;item); }
        );
    }
}

sub get_default_value {
    my $self = shift;
    if ( $self-&gt;has_default_method ) {
        return $self-&gt;_default;
    }
    elsif ( defined $self-&gt;default ) {
        return $self-&gt;default;
    }
    return;
}
{
    # create inflation/deflation methods
    foreach my $type ( 'inflate_default', 'deflate_value', 'inflate', 'deflate' ) {
        has "${type}_method" =&gt; ( is =&gt; 'ro', traits =&gt; ['Code'],
            isa =&gt; 'CodeRef',
            writer =&gt; "_set_${type}_method",
            predicate =&gt; "has_${type}_method",
            handles =&gt; {
                $type =&gt; 'execute_method',
            },
        );
    }
}

has 'deflation' =&gt; (
    is        =&gt; 'rw',
    predicate =&gt; 'has_deflation',
);
has 'trim' =&gt; (
    is      =&gt; 'rw',
    default =&gt; sub { { transform =&gt; \&amp;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' =&gt; (
     traits =&gt; ['Code'],
     is     =&gt; 'ro',
     isa    =&gt; 'CodeRef',
     lazy   =&gt; 1,
     builder =&gt; 'build_render_filter',
     handles =&gt; { html_filter =&gt; 'execute' },
);

sub build_render_filter {
    my $self = shift;

    if( $self-&gt;form &amp;&amp; $self-&gt;form-&gt;can('render_filter') ) {
        my $coderef = $self-&gt;form-&gt;can('render_filter');
        return $coderef;
    }
    else {
        return \&amp;default_render_filter;
    }
}

sub default_render_filter {
    my $string = shift;
    return '' if (!defined $string);
    $string =~ s/&amp;/&amp;amp;/g;
    $string =~ s/&lt;/&amp;lt;/g;
    $string =~ s/&gt;/&amp;gt;/g;
    $string =~ s/"/&amp;quot;/g;
    return $string;
}

has 'input_param' =&gt; ( is =&gt; 'rw', isa =&gt; 'Str' );

has 'language_handle' =&gt; (
    isa =&gt; duck_type( [ qw(maketext) ] ),
    is =&gt; 'rw',
    reader =&gt; 'get_language_handle',
    writer =&gt; 'set_language_handle',
    predicate =&gt; 'has_language_handle'
);

sub language_handle {
    my ( $self, $value ) = @_;
    if( $value ) {
        $self-&gt;set_language_handle($value);
        return;
    }
    return $self-&gt;get_language_handle if( $self-&gt;has_language_handle );
    # if language handle isn't set use form language handle if possible
    return $self-&gt;form-&gt;language_handle if ( $self-&gt;has_form );
    # no form, no language handle. This should only happen when
    # testing fields.
    my $lh;
    if ( $ENV{LANGUAGE_HANDLE} ) {
        if ( blessed $ENV{LANGUAGE_HANDLE} ) {
            $lh = $ENV{LANGUAGE_HANDLE};
        }
        else {
            $lh = HTML::FormHandler::I18N-&gt;get_handle( $ENV{LANGUAGE_HANDLE} );
        }
    }
    else {
       require HTML::FormHandler::I18N;
       $lh =  HTML::FormHandler::I18N-&gt;get_handle;
    }
    $self-&gt;set_language_handle($lh);
    return $lh;
}

has 'localize_meth' =&gt; (
     traits =&gt; ['Code'],
     is     =&gt; 'ro',
     isa    =&gt; 'CodeRef',
     lazy   =&gt; 1,
     builder =&gt; 'build_localize_meth',
     handles =&gt; { '_localize' =&gt; 'execute_method' },
);

sub build_localize_meth {
    my $self = shift;

    if( $self-&gt;form &amp;&amp; $self-&gt;form-&gt;can('localize_meth') ) {
        my $coderef = $self-&gt;form-&gt;can('localize_meth');
        return $coderef;
    }
    else {
        return \&amp;default_localize;
    }
}

sub default_localize {
    my ($self, @message) = @_;
    my $message = $self-&gt;language_handle-&gt;maketext(@message);
    return $message;
}

has 'messages' =&gt; ( is =&gt; 'rw',
    isa =&gt; 'HashRef',
    traits =&gt; ['Hash'],
    default =&gt; sub {{}},
    handles =&gt; {
        '_get_field_message' =&gt; 'get',
        '_has_field_message' =&gt; 'exists',
        'set_message' =&gt; 'set',
    },
);

our $class_messages = {
    'field_invalid'   =&gt; 'field is invalid',
    'range_too_low'   =&gt; 'Value must be greater than or equal to [_1]',
    'range_too_high'  =&gt; 'Value must be less than or equal to [_1]',
    'range_incorrect' =&gt; 'Value must be between [_1] and [_2]',
    'wrong_value'     =&gt; 'Wrong value',
    'no_match'        =&gt; '[_1] does not match',
    'not_allowed'     =&gt; '[_1] not allowed',
    'error_occurred'  =&gt; 'error occurred',
    'required'        =&gt; '[_1] field is required',
    'unique'          =&gt; 'Duplicate value for [_1]',
};

sub get_class_messages  {
    my $self = shift;
    my $messages = { %$class_messages };
    $messages-&gt;{required} = $self-&gt;required_message
        if $self-&gt;required_message;
    $messages-&gt;{unique} = $self-&gt;unique_message
        if $self-&gt;unique_message;
    return $messages;
}

sub get_message {
    my ( $self, $msg ) = @_;

    # first look in messages set on individual field
    return $self-&gt;_get_field_message($msg)
       if $self-&gt;_has_field_message($msg);
    # then look at form messages
    return $self-&gt;form-&gt;_get_form_message($msg)
       if $self-&gt;has_form &amp;&amp; $self-&gt;form-&gt;_has_form_message($msg);
    # then look for messages up through inherited field classes
    return $self-&gt;get_class_messages-&gt;{$msg};
}
sub all_messages {
    my $self = shift;
    my $form_messages = $self-&gt;has_form ? $self-&gt;form-&gt;messages : {};
    my $field_messages = $self-&gt;messages || {};
    my $lclass_messages = $self-&gt;my_class_messages || {};
    return {%{$lclass_messages}, %{$form_messages}, %{$field_messages}};
}

sub BUILDARGS {
    my $class = shift;

    # for backwards compatibility; these will be removed eventually
    my @new;
    push @new, ('element_attr', {@_}-&gt;{html_attr} )
        if( exists {@_}-&gt;{html_attr} );
    push @new, ('do_label', !{@_}-&gt;{no_render_label} )
        if( exists {@_}-&gt;{no_render_label} );
    return $class-&gt;SUPER::BUILDARGS(@_, @new);
}

sub BUILD {
    my ( $self, $params ) = @_;

    # temporary, for compatibility. move widget_tags to tags
    $self-&gt;merge_tags($self-&gt;wrapper_tags) if $self-&gt;has_wrapper_tags;
    # run default method builder
    $self-&gt;build_default_method;
    # build validate_method; needs to happen before validation
    # in order to have the "real" repeatable field names, not the instances
    $self-&gt;validate_method;
    # merge form widget_name_space
    $self-&gt;add_widget_name_space( $self-&gt;form-&gt;widget_name_space ) if $self-&gt;form;
    # handle apply actions
    $self-&gt;add_action( $self-&gt;trim ) if $self-&gt;trim;
    $self-&gt;_build_apply_list;
    $self-&gt;add_action( @{ $params-&gt;{apply} } ) if $params-&gt;{apply};
}

# this is the recursive routine that is used
# to initialize field results if there is no initial object and no params
sub _result_from_fields {
    my ( $self, $result ) = @_;

    if ( $self-&gt;disabled &amp;&amp; $self-&gt;has_init_value ) {
        $result-&gt;_set_value($self-&gt;init_value);
    }
    elsif ( my @values = $self-&gt;get_default_value ) {
        if ( $self-&gt;has_inflate_default_method ) {
            @values = $self-&gt;inflate_default(@values);
        }
        my $value = @values &gt; 1 ? \@values : shift @values;
        $self-&gt;init_value($value)   if defined $value;
        $result-&gt;_set_value($value) if defined $value;
    }
    $self-&gt;_set_result($result);
    $result-&gt;_set_field_def($self);
    return $result;
}

sub _result_from_input {
    my ( $self, $result, $input, $exists ) = @_;

    if ($exists) {
        $result-&gt;_set_input($input);
    }
    elsif ( $self-&gt;disabled ) {
        # This maybe should come from _result_from_object, but there's
        # not a reliable way to get there from here. Field can handle...
        return $self-&gt;_result_from_fields( $result );
    }
    elsif ( $self-&gt;form &amp;&amp; $self-&gt;form-&gt;use_fields_for_input_without_param ) {
        return $self-&gt;_result_from_fields( $result );
    }
    elsif ( $self-&gt;has_input_without_param ) {
        $result-&gt;_set_input( $self-&gt;input_without_param );
    }
    $self-&gt;_set_result($result);
    $result-&gt;_set_field_def($self);
    return $result;
}

sub _result_from_object {
    my ( $self, $result, $value ) = @_;

    $self-&gt;_set_result($result);

    if ( $self-&gt;form ) {
        $self-&gt;form-&gt;init_value( $self, $value );
    }
    else {
        $self-&gt;init_value($value);
        $result-&gt;_set_value($value);
    }
    $result-&gt;_set_value(undef) if $self-&gt;writeonly;
    $result-&gt;_set_field_def($self);
    return $result;
}

sub full_name {
    my $field = shift;

    my $name = $field-&gt;name;
    my $parent_name;
    # field should always have a parent unless it's a standalone field test
    if ( $field-&gt;parent ) {
        $parent_name = $field-&gt;parent-&gt;full_name;
    }
    return $name unless defined $parent_name &amp;&amp; length $parent_name;
    return $parent_name . '.' . $name;
}

sub full_accessor {
    my $field = shift;

    my $parent = $field-&gt;parent;
    if( $field-&gt;is_contains ) {
        return '' unless $parent;
        return $parent-&gt;full_accessor;
    }
    my $accessor = $field-&gt;accessor;
    my $parent_accessor;
    if ( $parent ) {
        $parent_accessor = $parent-&gt;full_accessor;
    }
    return $accessor unless defined $parent_accessor &amp;&amp; length $parent_accessor;
    return $parent_accessor . '.' . $accessor;
}

sub add_error {
    my ( $self, @message ) = @_;

    unless ( defined $message[0] ) {
        @message = ( $class_messages-&gt;{field_invalid});
    }
    @message = @{$message[0]} if ref $message[0] eq 'ARRAY';
    my $out;
    try {
        $out = $self-&gt;_localize(@message);
    }
    catch {
        die "Error occurred localizing error message for " . $self-&gt;label . ". Check brackets. $_";
    };
    return $self-&gt;push_errors($out);;
}

sub push_errors {
    my $self = shift;
    $self-&gt;_push_errors(@_);
    if ( $self-&gt;parent ) {
        $self-&gt;parent-&gt;propagate_error($self-&gt;result);
    }
    return;
}

sub _apply_deflation {
    my ( $self, $value ) = @_;

    if ( $self-&gt;has_deflation ) {
        $value = $self-&gt;deflation-&gt;($value);
    }
    elsif ( $self-&gt;has_deflate_method ) {
        $value = $self-&gt;deflate($value);
    }
    return $value;
}
sub _can_deflate {
    my $self = shift;
    return $self-&gt;has_deflation || $self-&gt;has_deflate_method;
}

# use Class::MOP to clone
sub clone {
    my ( $self, %params ) = @_;
    $self-&gt;meta-&gt;clone_object( $self, %params );
}

sub value_changed {
    my ($self) = @_;

    my @cmp;
    for ( 'init_value', 'value' ) {
        my $val = $self-&gt;$_;
        $val = '' unless defined $val;
        push @cmp, join '|', sort
            map { ref($_) &amp;&amp; $_-&gt;isa('DateTime') ? $_-&gt;iso8601 : "$_" }
            ref($val) eq 'ARRAY' ? @$val : $val;
    }
    return $cmp[0] ne $cmp[1];
}

sub required_text { shift-&gt;required ? 'required' : 'optional' }

sub input_defined {
    my ($self) = @_;
    return unless $self-&gt;has_input;
    return has_some_value( $self-&gt;input );
}

sub dump {
    my $self = shift;

    require Data::Dumper;
    warn "HFH: -----  ", $self-&gt;name, " -----\n";
    warn "HFH: type: ",  $self-&gt;type, "\n";
    warn "HFH: required: ", ( $self-&gt;required || '0' ), "\n";
    warn "HFH: label: ",  $self-&gt;label,  "\n";
    warn "HFH: widget: ", $self-&gt;widget || '', "\n";
    my $v = $self-&gt;value;
    warn "HFH: value: ", Data::Dumper::Dumper($v) if $v;
    my $iv = $self-&gt;init_value;
    warn "HFH: init_value: ", Data::Dumper::Dumper($iv) if $iv;
    my $i = $self-&gt;input;
    warn "HFH: input: ", Data::Dumper::Dumper($i) if $i;
    my $fif = $self-&gt;fif;
    warn "HFH: fif: ", Data::Dumper::Dumper($fif) if $fif;

    if ( $self-&gt;can('options') ) {
        my $o = $self-&gt;options;
        warn "HFH: options: " . Data::Dumper::Dumper($o);
    }
}

sub apply_rendering_widgets {
    my $self = shift;

    if ( $self-&gt;widget ) {
        warn "in apply_rendering_widgets " . $self-&gt;widget . " Field\n";
        $self-&gt;apply_widget_role( $self, $self-&gt;widget, 'Field' );
    }
    my $widget_wrapper = $self-&gt;widget_wrapper;
    $widget_wrapper ||= $self-&gt;form-&gt;widget_wrapper if $self-&gt;form;
    $widget_wrapper ||= 'Simple';
    unless ( $widget_wrapper eq 'none' ) {
        $self-&gt;apply_widget_role( $self, $widget_wrapper, 'Wrapper' );
    }
    return;

}

sub peek {
    my ( $self, $indent ) = @_;

    $indent ||= '';
    my $string = $indent . 'field: "' . $self-&gt;name . '"  type: ' . $self-&gt;type . "\n";
    if( $self-&gt;has_flag('has_contains') ) {
        $string .= $indent . "contains: \n";
        my $lindent = $indent . '  ';
        foreach my $field ( $self-&gt;contains-&gt;sorted_fields ) {
            $string .= $field-&gt;peek( $lindent );
        }
    }
    if( $self-&gt;has_fields ) {
        $string .= $indent . 'subfields of "' . $self-&gt;name . '": ' . $self-&gt;num_fields . "\n";
        my $lindent = $indent . '  ';
        foreach my $field ( $self-&gt;sorted_fields ) {
            $string .= $field-&gt;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-&gt;{$key} );
        }
        return 0;
    }
    return 1 if blessed($x);    # true if blessed, otherwise false
    return 1 if ref( $x );
    return;
}

sub apply_traits {
    my ($class, @traits) = @_;

    $class-&gt;meta-&gt;make_mutable;
    Moose::Util::apply_all_roles($class-&gt;meta, @traits);
    $class-&gt;meta-&gt;make_immutable;
}

__PACKAGE__-&gt;meta-&gt;make_immutable;
use namespace::autoclean;
1;

__END__

=pod

=encoding UTF-8

=head1 NAME

HTML::FormHandler::Field - base class for fields

=head1 VERSION

version 0.40068

=head1 SYNOPSIS

Instances of Field subclasses are generally built by L&lt;HTML::FormHandler&gt;
from 'has_field' declarations or the field_list, but they can also be constructed
using new for test purposes (since there's no standard way to add a field to a form
after construction).

    use HTML::FormHandler::Field::Text;
    my $field = HTML::FormHandler::Field::Text-&gt;new( name =&gt; $name, ... );

In your custom field class:

    package MyApp::Field::MyText;
    use HTML::FormHandler::Moose;
    extends 'HTML::FormHandler::Field::Text';

    has 'my_attribute' =&gt; ( isa =&gt; 'Str', is =&gt; 'rw' );

    apply [ { transform =&gt; sub { ... } },
            { check =&gt; ['fighter', 'bard', 'mage' ], message =&gt; '....' }
          ];
    1;

=head1 DESCRIPTION

This is the base class for form fields. The 'type' of a field class
is used in the FormHandler field_list or has_field to identify which field class to
load from the 'field_name_space' (or directly, when prefixed with '+').
If the type is not specified, it defaults to Text.

See L&lt;HTML::FormHandler::Manual::Fields&gt; for a list of the fields and brief
descriptions of their structure.

=head1 ATTRIBUTES

=head2 Names, types, accessor

=over

=item name

The name of the field. Used in the HTML form. Often a db accessor.
The only required attribute.

=item type

The class or type of the field. The 'type' of L&lt;HTML::FormHandler::Field::Money&gt;
is 'Money'. Classes that you define yourself are prefixed with '+'.

=item accessor

If the name of your field is different than your database accessor, use
this attribute to provide the accessor.

=item full_name

The name of the field with all parents:

   'event.start_date.month'

=item full_accessor

The field accessor with all parents.

=item html_name

The full_name plus the form name if 'html_prefix' is set.

=item input_param

By default we expect an input parameter based on the field name.  This allows
you to look for a different input parameter.

=back

=head2 Field data

=over

=item inactive, is_inactive, is_active

Set the 'inactive' attribute to 1 if this field is inactive. The 'inactive' attribute
that isn't set or is set to 0 will make a field 'active'.
This provides a way to define fields in the form and selectively set them to inactive.
There is also an '_active' attribute, for internal use to indicate that the field has
been activated/inactivated on 'process' by the form's 'active'/'inactive' attributes.

You can use the is_inactive and is_active methods to check whether this particular
field is active.

   if( $form-&gt;field('foo')-&gt;is_active ) { ... }

=item input

The input string from the parameters passed in.

=item value

The value as it would come from or go into the database, after being
acted on by inflations/deflations and transforms. Used to construct the
C&lt;&lt; $form-&gt;values &gt;&gt; hash. Validation and constraints act on 'value'.

See also L&lt;HTML::FormHandler::Manual::InflationDeflation&gt;.

=item fif

Values used to fill in the form. Read only. Use a deflation to get
from 'value' to 'fif' if an inflator was used. Use 'fif_from_value'
attribute if you want to use the field 'value' to fill in the form.

   [% form.field('title').fif %]

=item init_value

Initial value populated by init_from_object. You can tell if a field
has changed by comparing 'init_value' and 'value'. Read only.

=item input_without_param

Input for this field if there is no param. Set by default for Checkbox,
and Select, since an unchecked checkbox or unselected pulldown
does not return a parameter.

=back

=head2 Form, parent

=over

=item form

A reference to the containing form.

=item parent

A reference to the parent of this field. Compound fields are the
parents for the fields they contain.

=back

=head2 Errors

=over

=item errors

Returns the error list for the field. Also provides 'num_errors',
'has_errors', 'push_errors' and 'clear_errors' from Array
trait. Use 'add_error' to add an error to the array if you
want to use a MakeText language handle. Default is an empty list.

=item add_error

Add an error to the list of errors. Error message will be localized
using '_localize' method.
See also L&lt;HTML::FormHandler::TraitFor::I18N&gt;.

    return $field-&gt;add_error( 'bad data' ) if $bad;

=item error_fields

Compound fields will have an array of errors from the subfields.

=item localize_meth

Set the method used to localize.

=back

=head2 Attributes for creating HTML

The 'element_attr' hashref attribute can be used to set
arbitrary HTML attributes on a field's input tag.

   has_field 'foo' =&gt; ( element_attr =&gt; { readonly =&gt; 1, my_attr =&gt; 'abc' } );

Note that the 'id' and 'type' attributes are not set using element_attr. Use
the field's 'id' attribute (or 'build_id_method') to set the id.

The 'label_attr' hashref is for label attributes, and the 'wrapper_attr'
is for attributes on the wrapping element (a 'div' for the standard 'simple'
wrapper).

A 'javascript' key in one of the '_attr' hashes will be inserted into the
element as-is.

The following are used in rendering HTML, but are handled specially.

   label       - Text label for this field. Defaults to ucfirst field name.
   build_label_method - coderef for constructing the label
   wrap_label_method - coderef for constructing a wrapped label
   id          - Useful for javascript (default is html_name. to prefix with
                 form name, use 'html_prefix' in your form)
   build_id_method - coderef for constructing the id
   render_filter - Coderef for filtering fields before rendering. By default
                 changes &gt;, &lt;, &amp;, " to the html entities
   disabled    - Boolean to set field disabled

The order attribute may be used to set the order in which fields are rendered.

   order       - Used for sorting errors and fields. Built automatically,
                 but may also be explicitly set

The following are discouraged. Use 'element_attr', 'label_attr', and 'wrapper_attr'
instead.

   title       - instead use element_attr =&gt; { title =&gt; '...' }
   style       - instead use element_attr =&gt; { style =&gt; '...' }
   tabindex    - instead use element_attr =&gt; { tabindex =&gt; 1 }
   readonly    - instead use element_attr =&gt; { readonly =&gt; 'readonly' }

Rendering of the various HTML attributes is done by calling the 'process_attrs'
function (from HTML::FormHandler::Render::Util) and passing in a method that
adds in error classes, provides backward compatibility with the deprecated
attributes, etc.

    attribute hashref  class attribute        wrapping method
    =================  =================      ================
    element_attr       element_class          element_attributes
    label_attr         label_class            label_attributes
    wrapper_attr       wrapper_class          wrapper_attributes
                       element_wrapper_class  element_wrapper_attributes

('element_wrapper' is for an inner div around the input element, not
including the label. Used for Bootstrap3 rendering, but also available
in the Simple wrapper.)
The slots for the class attributes are arrayrefs; they will coerce a
string into an arrayref.
In addition, these 'wrapping methods' call a hook method in the form class,
'html_attributes', which you can use to customize and localize the various
attributes. (Field types: 'element', 'wrapper', 'label')

   sub html_attributes {
       my ( $self, $field, $type, $attr ) = @_;
       $attr-&gt;{class} = 'label' if $type eq 'label';
       return $attr;
   }

The 'process_attrs' function will also handle an array of strings, such as for the
'class' attribute.

=head2 tags

A hashref containing flags and strings for use in the rendering code.
The value of a tag can be a string, a coderef (accessed as a method on the
field) or a block specified with a percent followed by the blockname
('%blockname').

Retrieve a tag with 'get_tag'. It returns a '' if the tag doesn't exist.

This attribute used to be named 'widget_tags', which is deprecated.

=head2 html5_type_attr [string]

This string is used when rendering an input element as the value for the type attribute.
It is used when the form has the is_html5 flag on.

=head2 widget

The 'widget' attribute is used in rendering, so if you are
not using FormHandler's rendering facility, you don't need this
attribute.  It is used in generating HTML, in templates and the
rendering roles. Fields of different type can use the same widget.

This attribute is set in the field classes, or in the fields
defined in the form. If you want a new widget type, create a
widget role, such as MyApp::Form::Widget::Field::MyWidget. Provide
the name space in the 'widget_name_space' attribute, and set
the 'widget' of your field to the package name after the
Field/Form/Wrapper:

   has_field 'my_field' =&gt; ( widget =&gt; 'MyWidget' );

If you are using a template based rendering system you will want
to create a widget template.
(see L&lt;HTML::FormHandler::Manual::Templates&gt;)

Widget types for some of the provided field classes:

    Widget                 : Field classes
    -----------------------:---------------------------------
    Text                   : Text, Integer
    Checkbox               : Checkbox, Boolean
    RadioGroup             : Select, Multiple, IntRange (etc)
    Select                 : Select, Multiple, IntRange (etc)
    CheckboxGroup          : Multiple select
    TextArea               : TextArea
    Compound               : Compound, Repeatable, DateTime
    Password               : Password
    Hidden                 : Hidden
    Submit                 : Submit
    Reset                  : Reset
    NoRender               :
    Upload                 : Upload

Widget roles are automatically applied to field classes
unless they already have a 'render' method, and if the
'no_widgets' flag in the form is not set.

You can create your own widget roles and specify the namespace
in 'widget_name_space'. In the form:

    has '+widget_name_space' =&gt; ( default =&gt; sub { ['MyApp::Widget'] } );

If you want to use a fully specified role name for a widget, you
can prefix it with a '+':

   widget =&gt; '+MyApp::Widget::SomeWidget'

For more about widgets, see L&lt;HTML::FormHandler::Manual::Rendering&gt;.

=head2 Flags

   password  - prevents the entered value from being displayed in the form
   writeonly - The initial value is not taken from the database
   noupdate  - Do not update this field in the database (does not appear in $form-&gt;value)

=head2 Defaults

See also the documentation on L&lt;HTML::FormHandler::Manual::Intro/Defaults&gt;.

=over

=item default_method, set_default

Supply a coderef (which will be a method on the field) with 'default_method'
or the name of a form method with 'set_default' (which will be a method on
the form). If not specified and a form method with a name of
C&lt;&lt; default_&lt;field_name&gt; &gt;&gt; exists, it will be used.

=item default

Provide an initial value just like the 'set_default' method, except in the field
declaration:

  has_field 'bax' =&gt; ( default =&gt; 'Default bax' );

FormHandler has flipped back and forth a couple of times about whether a default
specified in the has_field definition should override values provided in an
initial item or init_object. Sometimes people want one behavior, and sometimes
the other. Now 'default' does *not* override.

If you pass in a model object with C&lt;&lt; item =&gt; $row &gt;&gt; or an initial object
with C&lt;&lt; init_object =&gt; {....} &gt;&gt; the values in that object will be used instead
of values provided in the field definition with 'default' or 'default_fieldname'.
If you want defaults that override or supplement the item/init_object, you can use the form
flags 'use_defaults_over_obj', 'use_init_obj_over_item', and
'use_init_obj_when_no_accessor_in_item'.

You could also put your defaults into your row or init_object instead.

=item default_over_obj

This is deprecated; look into using 'use_defaults_over_obj' or 'use_init_obj_over_item'
flags instead. They allow using the standard 'default' attribute.

Allows setting defaults which will override values provided with an item/init_object.
(And only those. Will not be used for defaults without an item/init_object.)

   has_field 'quux' =&gt; ( default_over_obj =&gt; 'default quux' );

At this time there is no equivalent of 'set_default', but the type of the attribute
is not defined so you can provide default values in a variety of other ways,
including providing a trait which does 'build_default_over_obj'. For examples,
see tests in the distribution.

=back

=head1 Constraints and Validations

See also L&lt;HTML::FormHandler::Manual::Validation&gt;.

=head2 Constraints set in attributes

=over

=item required

Flag indicating whether this field must have a value

=item unique

For DB field - check for uniqueness. Action is performed by
the DB model.

=item messages

    messages =&gt; { required =&gt; '...', unique =&gt; '...' }

Set messages created by FormHandler by setting in the 'messages'
hashref. Some field subclasses have additional settable messages.

required:  Error message text added to errors if required field is not present.
The default is "Field &lt;field label&gt; is required".

=item range_start

=item range_end

Field values are validated against the specified range if one
or both of range_start and range_end are set and the field
does not have 'options'.

The IntRange field uses this range to create a select list
with a range of integers.

In a FormHandler field_list:

    age =&gt; {
        type            =&gt; 'Integer',
        range_start     =&gt; 18,
        range_end       =&gt; 120,
    }

=item not_nullable

Fields that contain 'empty' values such as '' are changed to undef in the validation process.
If this flag is set, the value is not changed to undef. If your database column requires
an empty string instead of a null value (such as a NOT NULL column), set this attribute.

    has_field 'description' =&gt; (
        type =&gt; 'TextArea',
        not_nullable =&gt; 1,
    );

This attribute is also used when you want an empty array to stay an empty array and not
be set to undef.

It's also used when you have a compound field and you want the 'value' returned
to contain subfields with undef, instead of the whole field to be undef.

=back

=head2 apply

Use the 'apply' keyword to specify an ArrayRef of constraints and coercions to
be executed on the field at validate_field time.

   has_field 'test' =&gt; (
      apply =&gt; [ 'MooseType',
                 { check =&gt; sub {...}, message =&gt; { } },
                 { transform =&gt; sub { ... lc(shift) ... } }
               ],
   );

See more documentation in L&lt;HTML::FormHandler::Manual::Validation&gt;.

=head2 trim

An action to trim the field. By default
this contains a transform to strip beginning and trailing spaces.
Set this attribute to null to skip trimming, or supply a different
transform.

  trim =&gt; { transform =&gt; sub {
      my $string = shift;
      $string =~ s/^\s+//;
      $string =~ s/\s+$//;
      return $string;
  } }
  trim =&gt; { type =&gt; MyTypeConstraint }

Trimming is performed before any other defined actions.

=head2 Inflation/deflation

There are a number of methods to provide finely tuned inflation and deflation:

=over 4

=item inflate_method

Inflate to a data format desired for validation.

=item deflate_method

Deflate to a string format for presenting in HTML.

=item inflate_default_method

Modify the 'default' provided by an 'item' or 'init_object'.

=item deflate_value_method

Modify the value returned by C&lt;&lt; $form-&gt;value &gt;&gt;.

=item deflation

Another way of providing a deflation method.

=item transform

Another way of providing an inflation method.

=back

Normally if you have a deflation, you will need a matching inflation.
There are two different flavors of inflation/deflation: one for inflating values
to a format needed for validation and deflating for output, the other for
inflating the initial provided values (usually from a database row) and deflating
them for the 'values' returned.

See L&lt;HTML::FormHandler::Manual::InflationDeflation&gt;.

=head1 Processing and validating the field

=head2 validate_field

This is the base class validation routine. Most users will not
do anything with this. It might be useful for method modifiers,
if you want code that executed before or after the validation
process.

=head2 validate

This field method can be used in addition to or instead of 'apply' actions
in custom field classes.
It should validate the field data and set error messages on
errors with C&lt;&lt; $field-&gt;add_error &gt;&gt;.

    sub validate {
        my $field = shift;
        my $value = $field-&gt;value;
        return $field-&gt;add_error( ... ) if ( ... );
    }

=head2 validate_method, set_validate

Supply a coderef (which will be a method on the field) with 'validate_method'
or the name of a form method with 'set_validate' (which will be a method on
the form). If not specified and a form method with a name of
C&lt;&lt; validate_&lt;field_name&gt; &gt;&gt; exists, it will be used.

Periods in field names will be replaced by underscores, so that the field
'addresses.city' will use the 'validate_addresses_city' method for validation.

   has_field 'my_foo' =&gt; ( validate_method =&gt; \&amp;my_foo_validation );
   sub my_foo_validation { ... }
   has_field 'title' =&gt; ( isa =&gt; 'Str', set_validate =&gt; 'check_title' );

=head1 AUTHOR

FormHandler Contributors - see HTML::FormHandler

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2017 by Gerda Shank.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
</pre></body></html>