HTML-FormHandler

 view release on metacpan or  search on metacpan

lib/HTML/FormHandler.pm  view on Meta::CPAN

            last;
        }
    }
}

sub _clear_dependency {
    my $self = shift;

    $_->required(0) for @{$self->_required};
    $self->clear_required;
}

sub peek {
    my $self = shift;
    my $string = "Form " . $self->name . "\n";
    my $indent = '  ';
    foreach my $field ( $self->sorted_fields ) {
        $string .= $field->peek( $indent );
    }
    return $string;
}

sub _munge_params {
    my ( $self, $params, $attr ) = @_;
    my $_fix_params = $self->params_class->new( @{ $self->params_args || [] } );
    my $new_params = $_fix_params->expand_hash($params);
    if ( $self->html_prefix ) {
        $new_params = $new_params->{ $self->name };
    }
    $new_params = {} if !defined $new_params;
    $self->{params} = $new_params;
}

sub params_to_values {
    my ( $self, $params ) = @_;
    my $_fix_params = $self->params_class->new( @{ $self->params_args || [] } );
    my $new_params = $_fix_params->expand_hash($params);
    return $new_params;
}

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

    unless ( defined $message[0] ) {
        @message = ('form is invalid');
    }
    my $out;
    try {
        $out = $self->_localize(@message);
    }
    catch {
        die "Error occurred localizing error message for " . $self->name . ".  $_";
    };
    $self->push_form_errors($out);
    return;
}

sub get_default_value { }
sub _can_deflate { }

sub update_fields {
    my $self = shift;
    if( $self->has_update_field_list ) {
        my $updates = $self->update_field_list;
        foreach my $field_name ( keys %{$updates} ) {
            $self->update_field($field_name, $updates->{$field_name} );
        }
        $self->clear_update_field_list;
    }
    if( $self->has_defaults ) {
        my $defaults = $self->defaults;
        foreach my $field_name ( keys %{$defaults} ) {
            $self->update_field($field_name, { default => $defaults->{$field_name} } );
        }
        $self->clear_defaults;
    }
}

sub update_field {
    my ( $self, $field_name, $updates ) = @_;

    my $field = $self->field($field_name);
    unless( $field ) {
        die "Field $field_name is not found and cannot be updated by update_fields";
    }
    while ( my ( $attr_name, $attr_value ) = each %{$updates} ) {
        confess "invalid attribute '$attr_name' passed to update_field"
            unless $field->can($attr_name);
        if( $attr_name eq 'tags' ) {
            $field->set_tag(%$attr_value);
        }
        else {
            $field->$attr_name($attr_value);
        }
    }
}


__PACKAGE__->meta->make_immutable;
use namespace::autoclean;
1;

__END__

=pod

=encoding UTF-8

=head1 NAME

HTML::FormHandler - HTML forms using Moose

=head1 VERSION

version 0.40068

=head1 SYNOPSIS

See the manual at L<HTML::FormHandler::Manual>.

    use HTML::FormHandler; # or a custom form: use MyApp::Form::User;
    my $form = HTML::FormHandler->new( .... );
    $form->process( params => $params );
    my $rendered_form = $form->render;
    if( $form->validated ) {
        # perform validated form actions
    }
    else {
        # perform non-validated actions
    }

Or, if you want to use a form 'result' (which contains only the form
values and error messages) instead:

    use MyApp::Form; # or a generic form: use HTML::FormHandler;
    my $form = MyApp::Form->new( .... );
    my $result = $form->run( params => $params );
    if( $result->validated ) {
        # perform validated form actions

lib/HTML/FormHandler.pm  view on Meta::CPAN

define your own field classes (or apply a role to a field class - see
L<HTML::FormHandler::Manual::Cookbook>). The field 'type' (used in field
definitions) is the short class name of the field class, used when
searching the 'field_name_space' for the field class.

=head3 has_field

The most common way of declaring fields is the 'has_field' syntax.
Using the 'has_field' syntax sugar requires C< use HTML::FormHandler::Moose; >
or C< use HTML::FormHandler::Moose::Role; > in a role.
See L<HTML::FormHandler::Manual::Intro>

   use HTML::FormHandler::Moose;
   has_field 'field_name' => ( type => 'FieldClass', .... );

=head3 field_list

A 'field_list' is an array of field definitions which can be used as an
alternative to 'has_field' in small, dynamic forms to create fields.

    field_list => [
       field_one => {
          type => 'Text',
          required => 1
       },
       field_two => 'Text,
    ]

The field_list array takes elements which are either a field_name key
pointing to a 'type' string or a field_name key pointing to a
hashref of field attributes. You can also provide an array of
hashref elements with the name as an additional attribute.
The field list can be set inside a form class, when you want to
add fields to the form depending on some other state, although
you can also create all the fields and set some of them inactive.

   sub field_list {
      my $self = shift;
      my $fields = $self->schema->resultset('SomeTable')->
                          search({user_id => $self->user_id, .... });
      my @field_list;
      while ( my $field = $fields->next )
      {
         < create field list >
      }
      return \@field_list;
   }

=head3 update_field_list

Used to dynamically set particular field attributes on the 'process' (or
'run') call. (Will not create fields.)

    $form->process( update_field_list => {
       foo_date => { format => '%m/%e/%Y', date_start => '10-01-01' } },
       params => $params );

The 'update_field_list' is processed by the 'update_fields' form method,
which can also be used in a form to do specific field updates:

    sub update_fields {
        my $self = shift;
        $self->field('foo')->temp( 'foo_temp' );
        $self->field('bar')->default( 'foo_value' );
        $self->next::method();
    }

(Note that you although you can set a field's 'default', you can't set a
field's 'value' directly here, since it will
be overwritten by the validation process. Set the value in a field
validation method.)

=head3 update_subfields

Yet another way to provide settings for the field, except this one is intended for
use in roles and compound fields, and is only executed when the form is
initially built. It takes the same field name keys as 'update_field_list', plus
'all', 'by_flag', and 'by_type'.

    sub build_update_subfields {{
        all => { tags => { wrapper_tag => 'p' } },
        foo => { element_class => 'blue' },
    }}

The 'all' hash key will apply updates to all fields. (Conflicting attributes
in a field definition take precedence.)

The 'by_flag' hash key will apply updates to fields with a particular flag.
The currently supported subkeys are 'compound', 'contains', and 'repeatable'.
(For repeatable instances, in addition to 'contains' you can also use the
'repeatable' key and the 'init_contains' attribute.)
This is useful for turning on the rendering
wrappers for compounds and repeatables, which are off by default. (The
repeatable instances are wrapped by default.)

    sub build_update_subfields {{
        by_flag => { compound => { do_wrapper => 1 } },
        by_type => { Select => { element_class => ['sel_elem'] } },
    }}

The 'by_type' hash key will provide values to all fields of a particular
type.

=head3 defaults

This is a more specialized version of the 'update_field_list'. It can be
used to provide 'default' settings for fields, in a shorthand way (you don't
have to say 'default' for every field).

   $form->process( defaults => { foo => 'this_foo', bar => 'this_bar' }, ... );

=head3 active/inactive

A field can be marked 'inactive' and set to active at new or process time
by specifying the field name in the 'active' array:

   has_field 'foo' => ( type => 'Text', inactive => 1 );
   ...
   my $form = MyApp::Form->new( active => ['foo'] );
   ...
   $form->process( active => ['foo'] );



( run in 0.333 second using v1.01-cache-2.11-cpan-e93a5daba3e )