package HTML::FormFu; use strict; our $VERSION = '2.05'; # VERSION use Moose; use MooseX::Attribute::FormFuChained; with 'HTML::FormFu::Role::Render', 'HTML::FormFu::Role::CreateChildren', 'HTML::FormFu::Role::GetProcessors', 'HTML::FormFu::Role::ContainsElements', 'HTML::FormFu::Role::ContainsElementsSharedWithField', 'HTML::FormFu::Role::FormAndBlockMethods', 'HTML::FormFu::Role::FormAndElementMethods', 'HTML::FormFu::Role::FormBlockAndFieldMethods', 'HTML::FormFu::Role::NestedHashUtils', 'HTML::FormFu::Role::Populate', 'HTML::FormFu::Role::CustomRoles'; use HTML::FormFu::Attribute qw( mk_attrs mk_attr_accessors mk_output_accessors mk_inherited_accessors mk_inherited_merging_accessors ); use HTML::FormFu::Constants qw( $EMPTY_STR ); use HTML::FormFu::Constraint; use HTML::FormFu::Exception; use HTML::FormFu::FakeQuery; use HTML::FormFu::Filter; use HTML::FormFu::Inflator; use HTML::FormFu::Localize; use HTML::FormFu::ObjectUtil qw( form load_config_file load_config_filestem clone stash constraints_from_dbic parent _load_file ); use HTML::FormFu::Util qw( DEBUG DEBUG_PROCESS DEBUG_CONSTRAINTS debug require_class _get_elements xml_escape split_name _parse_args process_attrs _filter_components ); use Clone (); use List::Util 1.45 qw( first any none uniq ); use Scalar::Util qw( blessed weaken reftype ); use Carp qw( croak ); use overload ( 'eq' => '_string_equals', '==' => '_object_equals', '""' => sub { return shift->render }, 'bool' => sub {1}, 'fallback' => 1, ); __PACKAGE__->mk_attr_accessors(qw( id action enctype method )); for my $name ( qw( _elements _output_processors _valid_names _plugins _models ) ) { has $name => ( is => 'rw', default => sub { [] }, lazy => 1, isa => 'ArrayRef', ); } has languages => ( is => 'rw', default => sub { ['en'] }, lazy => 1, isa => 'ArrayRef', traits => ['FormFuChained'], ); has input => ( is => 'rw', default => sub { {} }, lazy => 1, isa => 'HashRef', traits => ['FormFuChained'], ); has _processed_params => ( is => 'rw', default => sub { {} }, lazy => 1, isa => 'HashRef', ); has form_error_message_class => ( is => 'rw', default => 'form_error_message', lazy => 1, ); our @MULTIFORM_SHARED = (qw( javascript javascript_src indicator filename query_type force_error_message localize_class tt_module nested_name nested_subscript default_model tmp_upload_dir params_ignore_underscore )); for (@MULTIFORM_SHARED) { has $_ => ( is => 'rw', traits => ['FormFuChained'], ); } has submitted => ( is => 'rw', traits => ['FormFuChained'] ); has query => ( is => 'rw', traits => ['FormFuChained'] ); has _auto_fieldset => ( is => 'rw' ); __PACKAGE__->mk_output_accessors(qw( form_error_message )); *elements = \&element; *constraints = \&constraint; *filters = \&filter; *deflators = \&deflator; *inflators = \&inflator; *validators = \&validator; *transformers = \&transformer; *output_processors = \&output_processor; *loc = \&localize; *plugins = \&plugin; *add_plugins = \&add_plugin; our $build_defaults = { action => '', method => 'post', filename => 'form', render_method => 'string', tt_args => {}, tt_module => 'Template', query_type => 'CGI', default_model => 'DBIC', localize_class => 'HTML::FormFu::I18N', auto_error_message => 'form_%s_%t', error_tag => 'span', }; sub BUILD { my ( $self, $args ) = @_; $self->populate($build_defaults); return; } sub auto_fieldset { my ( $self, $element_ref ) = @_; # if there's no arg, just return whether there's an auto_fieldset already return $self->_auto_fieldset if !$element_ref; # if the argument isn't a reference, assume it's just a "1" meaning true, # and use an empty hashref if ( !ref $element_ref ) { $element_ref = {}; } $element_ref->{type} = 'Fieldset'; $self->element($element_ref); $self->_auto_fieldset(1); return $self; } sub default_values { my ( $self, $default_ref ) = @_; for my $field ( @{ $self->get_fields } ) { my $name = $field->nested_name; next if !defined $name; next if !exists $default_ref->{$name}; $field->default( $default_ref->{$name} ); } return $self; } sub model { my ( $self, $model_name ) = @_; $model_name ||= $self->default_model; # search models already loaded for my $model ( @{ $self->_models } ) { return $model if $model->type =~ /\Q$model_name\E$/; } # class not found, try require-ing it my $class = $model_name =~ s/^\+// ? $model_name : "HTML::FormFu::Model::$model_name"; require_class($class); my $model = $class->new( { type => $model_name, parent => $self, } ); push @{ $self->_models }, $model; return $model; } sub process { my ( $self, $query ) = @_; $self->input( {} ); $self->_processed_params( {} ); $self->_valid_names( [] ); $self->clear_errors; $query ||= $self->query; if ( defined $query && !blessed($query) ) { $query = HTML::FormFu::FakeQuery->new( $self, $query ); } # save it for further calls to process() if ($query) { DEBUG && debug( QUERY => $query ); $self->query($query); } # run all elements pre_process() methods for my $elem ( @{ $self->get_elements } ) { $elem->pre_process; } # run all plugins pre_process() methods for my $plugin ( @{ $self->get_plugins } ) { $plugin->pre_process; } # run all elements process() methods for my $elem ( @{ $self->get_elements } ) { $elem->process; } # run all plugins process() methods for my $plugin ( @{ $self->get_plugins } ) { $plugin->process; } my $submitted; if ( defined $query ) { eval { my @params = $query->param }; croak "Invalid query object: $@" if $@; $submitted = $self->_submitted($query); } DEBUG_PROCESS && debug( SUBMITTED => $submitted ); $self->submitted($submitted); if ($submitted) { my %input; my @params = $query->param; for my $field ( @{ $self->get_fields } ) { my $name = $field->nested_name; next if !defined $name; next if none { $name eq $_ } @params; ## CGI wants you to use $query->multi_param($foo). ## doing so breaks CGI::Simple. So shoosh it up for now. local $CGI::LIST_CONTEXT_WARN = 0; if ( $field->nested ) { # call in list context so we know if there's more than 1 value my @values = $query->param($name); my $value = @values > 1 ? \@values : $values[0]; $self->set_nested_hash_value( \%input, $name, $value ); } else { my @values = $query->param($name); $input{$name} = @values > 1 ? \@values : $values[0]; } } DEBUG && debug( INPUT => \%input ); # run all field process_input methods for my $field ( @{ $self->get_fields } ) { $field->process_input( \%input ); } $self->input( \%input ); $self->_process_input; } # run all plugins post_process methods for my $elem ( @{ $self->get_elements } ) { $elem->post_process; } for my $plugin ( @{ $self->get_plugins } ) { $plugin->post_process; } return; } sub _submitted { my ( $self, $query ) = @_; my $indicator = $self->indicator; my $code; if ( defined($indicator) && ref $indicator ne 'CODE' ) { DEBUG_PROCESS && debug( INDICATOR => $indicator ); $code = sub { return defined $query->param($indicator) }; } elsif ( !defined $indicator ) { my @names = uniq grep {defined} map { $_->nested_name } @{ $self->get_fields }; DEBUG_PROCESS && debug( 'no indicator, checking fields...' => \@names ); $code = sub { grep { defined $query->param($_) } @names; }; } else { $code = $indicator; } return $code->( $self, $query ); } sub _process_input { my ($self) = @_; $self->_build_params; $self->_process_file_uploads; $self->_filter_input; $self->_constrain_input; $self->_inflate_input if !@{ $self->get_errors }; $self->_validate_input if !@{ $self->get_errors }; $self->_transform_input if !@{ $self->get_errors }; $self->_build_valid_names; return; } sub _build_params { my ($self) = @_; my $input = $self->input; my %params; for my $field ( @{ $self->get_fields } ) { my $name = $field->nested_name; next if !defined $name; next if exists $params{$name}; next if !$self->nested_hash_key_exists( $self->input, $name ) && !$field->default_empty_value; my $input = $self->get_nested_hash_value( $self->input, $name ); if ( ref $input eq 'ARRAY' ) { # can't clone upload filehandles # so create new arrayref of values $input = [@$input]; } elsif ( !defined $input && $field->default_empty_value ) { $input = ''; } $self->set_nested_hash_value( \%params, $name, $input, $name ); } $self->_processed_params( \%params ); DEBUG_PROCESS && debug( 'PROCESSED PARAMS' => \%params ); return; } sub _process_file_uploads { my ($self) = @_; my @names = uniq grep {defined} map { $_->nested_name } grep { $_->isa('HTML::FormFu::Element::File') } @{ $self->get_fields }; if (@names) { my $query_class = $self->query_type; if ( $query_class !~ /^\+/ ) { $query_class = "HTML::FormFu::QueryType::$query_class"; } require_class($query_class); my $params = $self->_processed_params; my $input = $self->input; for my $name (@names) { next if !$self->nested_hash_key_exists( $input, $name ); my $values = $query_class->parse_uploads( $self, $name ); $self->set_nested_hash_value( $params, $name, $values ); } } return; } sub _filter_input { my ($self) = @_; my $params = $self->_processed_params; for my $filter ( @{ $self->get_filters } ) { my $name = $filter->nested_name; next if !defined $name; next if !$self->nested_hash_key_exists( $params, $name ); $filter->process( $self, $params ); } return; } sub _constrain_input { my ($self) = @_; my $params = $self->_processed_params; for my $constraint ( @{ $self->get_constraints } ) { DEBUG_CONSTRAINTS && debug( 'FIELD NAME' => $constraint->field->nested_name, 'CONSTRAINT TYPE' => $constraint->type, ); $constraint->pre_process; my @errors = eval { $constraint->process($params) }; DEBUG_CONSTRAINTS && debug( ERRORS => \@errors ); DEBUG_CONSTRAINTS && debug( '$@' => $@ ); if ( blessed $@ && $@->isa('HTML::FormFu::Exception::Constraint') ) { push @errors, $@; } elsif ($@) { push @errors, HTML::FormFu::Exception::Constraint->new; } for my $error (@errors) { if ( !$error->parent ) { $error->parent( $constraint->parent ); } if ( !$error->constraint ) { $error->constraint($constraint); } $error->parent->add_error($error); } } return; } sub _inflate_input { my ($self) = @_; my $params = $self->_processed_params; for my $inflator ( @{ $self->get_inflators } ) { my $name = $inflator->nested_name; next if !defined $name; next if !$self->nested_hash_key_exists( $params, $name ); next if any {defined} @{ $inflator->parent->get_errors }; my $value = $self->get_nested_hash_value( $params, $name ); my @errors; ( $value, @errors ) = eval { $inflator->process($value) }; if ( blessed $@ && $@->isa('HTML::FormFu::Exception::Inflator') ) { push @errors, $@; } elsif ($@) { push @errors, HTML::FormFu::Exception::Inflator->new; } for my $error (@errors) { $error->parent( $inflator->parent ) if !$error->parent; $error->inflator($inflator) if !$error->inflator; $error->parent->add_error($error); } $self->set_nested_hash_value( $params, $name, $value ); } return; } sub _validate_input { my ($self) = @_; my $params = $self->_processed_params; for my $validator ( @{ $self->get_validators } ) { my $name = $validator->nested_name; next if !defined $name; next if !$self->nested_hash_key_exists( $params, $name ); next if any {defined} @{ $validator->parent->get_errors }; my @errors = eval { $validator->process($params) }; if ( blessed $@ && $@->isa('HTML::FormFu::Exception::Validator') ) { push @errors, $@; } elsif ($@) { push @errors, HTML::FormFu::Exception::Validator->new; } for my $error (@errors) { $error->parent( $validator->parent ) if !$error->parent; $error->validator($validator) if !$error->validator; $error->parent->add_error($error); } } return; } sub _transform_input { my ($self) = @_; my $params = $self->_processed_params; for my $transformer ( @{ $self->get_transformers } ) { my $name = $transformer->nested_name; next if !defined $name; next if !$self->nested_hash_key_exists( $params, $name ); next if any {defined} @{ $transformer->parent->get_errors }; my $value = $self->get_nested_hash_value( $params, $name ); my (@errors) = eval { $transformer->process( $value, $params ) }; if ( blessed $@ && $@->isa('HTML::FormFu::Exception::Transformer') ) { push @errors, $@; } elsif ($@) { push @errors, HTML::FormFu::Exception::Transformer->new; } for my $error (@errors) { $error->parent( $transformer->parent ) if !$error->parent; $error->transformer($transformer) if !$error->transformer; $error->parent->add_error($error); } } return; } sub _build_valid_names { my ($self) = @_; my $params = $self->_processed_params; my $skip_private = $self->params_ignore_underscore; my @errors = $self->has_errors; my @names; my %non_param; for my $field ( @{ $self->get_fields } ) { my $name = $field->nested_name; next if !defined $name; next if $skip_private && $field->name =~ /^_/; if ( $field->non_param ) { $non_param{$name} = 1; } elsif ( $self->nested_hash_key_exists( $params, $name ) ) { push @names, $name; } } push @names, uniq grep { ref $params->{$_} ne 'HASH' } grep { !( $skip_private && /^_/ ) } grep { !exists $non_param{$_} } keys %$params; my %valid; CHECK: for my $name (@names) { for my $error (@errors) { next CHECK if $name eq $error; } $valid{$name}++; } my @valid = keys %valid; $self->_valid_names( \@valid ); return; } sub _hash_keys { my ( $hash, $subscript ) = @_; my @names; for my $key ( keys %$hash ) { if ( ref $hash->{$key} eq 'HASH' ) { push @names, map { $subscript ? "${key}[${_}]" : "$key.$_" } _hash_keys( $hash->{$key}, $subscript ); } elsif ( ref $hash->{$key} eq 'ARRAY' ) { push @names, map { $subscript ? "${key}[${_}]" : "$key.$_" } _array_indices( $hash->{$key}, $subscript ); } else { push @names, $key; } } return @names; } sub _array_indices { my ( $array, $subscript ) = @_; my @names; for my $i ( 0 .. $#{$array} ) { if ( ref $array->[$i] eq 'HASH' ) { push @names, map { $subscript ? "${i}[${_}]" : "$i.$_" } _hash_keys( $array->[$i], $subscript ); } elsif ( ref $array->[$i] eq 'ARRAY' ) { push @names, map { $subscript ? "${i}[${_}]" : "$i.$_" } _array_indices( $array->[$i], $subscript ); } else { push @names, $i; } } return @names; } sub submitted_and_valid { my ($self) = @_; return $self->submitted && !$self->has_errors; } sub params { my ($self) = @_; return {} if !$self->submitted; my @names = $self->valid; my %params; for my $name (@names) { my @values = $self->param($name); if ( @values > 1 ) { $self->set_nested_hash_value( \%params, $name, \@values ); } else { $self->set_nested_hash_value( \%params, $name, $values[0] ); } } return \%params; } sub param { my ( $self, $name ) = @_; croak 'param method is readonly' if @_ > 2; return if !$self->submitted; if ( @_ == 2 ) { return if !$self->valid($name); my $value = $self->get_nested_hash_value( $self->_processed_params, $name ); return if !defined $value; if ( ref $value eq 'ARRAY' ) { return wantarray ? @$value : $value->[0]; } else { return $value; } } # return a list of valid names, if no $name arg return $self->valid; } sub param_value { my ( $self, $name ) = @_; croak 'name parameter required' if @_ != 2; return undef ## no critic (ProhibitExplicitReturnUndef); if !$self->valid($name); # this is guaranteed to always return a single value my $value = $self->get_nested_hash_value( $self->_processed_params, $name ); return ref $value eq 'ARRAY' ? $value->[0] : $value; } sub param_array { my ( $self, $name ) = @_; croak 'name parameter required' if @_ != 2; # guaranteed to always return an arrayref return [] if !$self->valid($name); my $value = $self->get_nested_hash_value( $self->_processed_params, $name ); return [] if !defined $value; return ref $value eq 'ARRAY' ? $value : [$value]; } sub param_list { my ( $self, $name ) = @_; croak 'name parameter required' if @_ != 2; # guaranteed to always return an arrayref return if !$self->valid($name); my $value = $self->get_nested_hash_value( $self->_processed_params, $name ); return if !defined $value; return ref $value eq 'ARRAY' ? @$value : $value; } sub valid { my $self = shift; return if !$self->submitted; my @valid = @{ $self->_valid_names }; if (@_) { my $name = shift; return 1 if any { $name eq $_ } @valid; # not found - see if it's the name of a nested block my $parent; if ( defined $self->nested_name && $self->nested_name eq $name ) { $parent = $self; } else { ($parent) = first { $_->isa('HTML::FormFu::Element::Block') } @{ $self->get_all_elements( { nested_name => $name, } ) }; } if ( defined $parent ) { my $fail = any {defined} map { @{ $_->get_errors } } @{ $parent->get_fields }; return 1 if !$fail; } return; } # return a list of valid names, if no $name arg return @valid; } sub has_errors { my $self = shift; return if !$self->submitted; my @names = map { $_->nested_name } grep { @{ $_->get_errors } } grep { defined $_->nested_name } @{ $self->get_fields }; if (@_) { my $name = shift; return 1 if any {/\Q$name/} @names; return; } # return list of names with errors, if no $name arg return @names; } sub add_valid { my ( $self, $key, $value ) = @_; croak 'add_valid requires arguments ($key, $value)' if @_ != 3; $self->set_nested_hash_value( $self->input, $key, $value ); $self->set_nested_hash_value( $self->_processed_params, $key, $value ); if ( none { $_ eq $key } @{ $self->_valid_names } ) { push @{ $self->_valid_names }, $key; } return $value; } sub _single_plugin { my ( $self, $arg_ref ) = @_; if ( !ref $arg_ref ) { $arg_ref = { type => $arg_ref }; } elsif ( ref $arg_ref eq 'HASH' ) { # shallow clone $arg_ref = {%$arg_ref}; } else { croak 'invalid args'; } my $type = delete $arg_ref->{type}; my @return; my @names = map { ref $_ ? @$_ : $_ } grep {defined} ( delete $arg_ref->{name}, delete $arg_ref->{names} ); if (@names) { # add plugins to appropriate fields for my $x (@names) { for my $field ( @{ $self->get_fields( { nested_name => $x } ) } ) { my $new = $field->_require_plugin( $type, $arg_ref ); push @{ $field->_plugins }, $new; push @return, $new; } } } else { # add plugin directly to form my $new = $self->_require_plugin( $type, $arg_ref ); push @{ $self->_plugins }, $new; push @return, $new; } return @return; } around render => sub { my $orig = shift; my $self = shift; my $plugins = $self->get_plugins; for my $plugin (@$plugins) { $plugin->render; } my $output = $self->$orig; for my $plugin (@$plugins) { $plugin->post_render( \$output ); } return $output; }; sub render_data { my ( $self, $args ) = @_; my $render = $self->render_data_non_recursive( { elements => [ map { $_->render_data } @{ $self->_elements } ], $args ? %$args : (), } ); return $render; } sub render_data_non_recursive { my ( $self, $args ) = @_; my %render = ( filename => $self->filename, javascript => $self->javascript, javascript_src => $self->javascript_src, attributes => xml_escape( $self->attributes ), stash => $self->stash, $args ? %$args : (), ); $render{form} = \%render; weaken( $render{form} ); $render{object} = $self; if ($self->force_error_message || ( $self->has_errors && defined $self->form_error_message ) ) { $render{form_error_message} = xml_escape( $self->form_error_message ); $render{form_error_message_class} = $self->form_error_message_class; } return \%render; } sub string { my ( $self, $args_ref ) = @_; $args_ref ||= {}; my $html = $self->_string_form_start( $args_ref ); # form template $html .= "\n"; for my $element ( @{ $self->get_elements } ) { # call render, so that child elements can use a different renderer my $element_html = $element->render; # skip Blank fields if ( length $element_html ) { $html .= $element_html . "\n"; } } $html .= $self->_string_form_end( $args_ref ); $html .= "\n"; return $html; } sub _string_form_start { my ( $self, $args_ref ) = @_; # start_form template my $render_ref = exists $args_ref->{render_data} ? $args_ref->{render_data} : $self->render_data_non_recursive; my $html = sprintf "", process_attrs( $render_ref->{attributes} ); if ( defined $render_ref->{form_error_message} ) { $html .= sprintf qq{\n
%s
}, $render_ref->{form_error_message_class}, $render_ref->{form_error_message}, ; } if ( defined $render_ref->{javascript_src} ) { my $uri = $render_ref->{javascript_src}; my @uris = ref $uri eq 'ARRAY' ? @$uri : ($uri); for my $uri (@uris) { $html .= sprintf qq{\n}, $uri, ; } } if ( defined $render_ref->{javascript} ) { $html .= sprintf qq{\n}, $render_ref->{javascript}, ; } return $html; } sub _string_form_end { my ( $self ) = @_; # end_form template return ""; } sub start { my $self = shift; if ( 'tt' eq $self->render_method ) { return $self->tt( { filename => 'start_form', render_data => $self->render_data_non_recursive, } ); } else { return $self->_string_form_start( @_ ); } } sub end { my $self = shift; if ( 'tt' eq $self->render_method ) { return $self->tt( { filename => 'end_form', render_data => $self->render_data_non_recursive, } ); } else { return $self->_string_form_end( @_ ); } } sub hidden_fields { my ($self) = @_; return join $EMPTY_STR, map { $_->render } @{ $self->get_fields( { type => 'Hidden' } ) }; } sub output_processor { my ( $self, $arg ) = @_; my @return; if ( ref $arg eq 'ARRAY' ) { push @return, map { $self->_single_output_processor($_) } @$arg; } else { push @return, $self->_single_output_processor($arg); } return @return == 1 ? $return[0] : @return; } sub _single_output_processor { my ( $self, $arg ) = @_; if ( !ref $arg ) { $arg = { type => $arg }; } elsif ( ref $arg eq 'HASH' ) { $arg = Clone::clone($arg); } else { croak 'invalid args'; } my $type = delete $arg->{type}; my $new = $self->_require_output_processor( $type, $arg ); push @{ $self->_output_processors }, $new; return $new; } sub _require_output_processor { my ( $self, $type, $opt ) = @_; croak 'required arguments: $self, $type, \%options' if @_ != 3; croak "options argument must be hash-ref" if reftype($opt) ne 'HASH'; my $class = $type; if ( not $class =~ s/^\+// ) { $class = "HTML::FormFu::OutputProcessor::$class"; } $type =~ s/^\+//; require_class($class); my $object = $class->new( { type => $type, parent => $self, } ); # handle default_args my $parent = $self->parent; if ( $parent && exists $parent->default_args->{output_processor}{$type} ) { %$opt = ( %{ $parent->default_args->{output_processer}{$type} }, %$opt ); } $object->populate($opt); return $object; } sub get_output_processors { my $self = shift; my %args = _parse_args(@_); my @x = @{ $self->_output_processors }; if ( exists $args{type} ) { @x = grep { $_->type eq $args{type} } @x; } return \@x; } sub get_output_processor { my $self = shift; my $x = $self->get_output_processors(@_); return @$x ? $x->[0] : (); } __PACKAGE__->meta->make_immutable; 1; __END__ =head1 NAME HTML::FormFu - HTML Form Creation, Rendering and Validation Framework =head1 VERSION version 2.05 =head1 SYNOPSIS Note: These examples make use of L. As of C v02.005, the L module is not bundled with C and is available in a stand-alone distribution. use HTML::FormFu; my $form = HTML::FormFu->new; $form->load_config_file('form.yml'); $form->process( $cgi_query ); if ( $form->submitted_and_valid ) { # do something with $form->params } else { # display the form $template->param( form => $form ); } If you're using L, a more suitable example might be: package MyApp::Controller::User; use Moose; extends 'Catalyst::Controller::HTML::FormFu'; sub user : FormFuChained CaptureArgs(1) { my ( $self, $c, $id ) = @_; my $rs = $c->model('Schema')->resultset('User'); $c->stash->{user} = $rs->find( $id ); return; } sub edit : FormFuChained('user') Args(0) FormConfig { my ( $self, $c ) = @_; my $form = $c->stash->{form}; my $user = $c->stash->{user}; if ( $form->submitted_and_valid ) { $form->model->update( $user ); $c->res->redirect( $c->uri_for( "/user/$id" ) ); return; } $form->model->default_values( $user ) if ! $form->submitted; } Note: Because L is automatically called for you by the Catalyst controller; if you make any modifications to the form within your action method, such as adding or changing elements, adding constraints, etc; you must call L again yourself before using L, any of the methods listed under L or L, or rendering the form. Here's an example of a config file to create a basic login form (all examples here are L, but you can use any format supported by L), you can also create forms directly in your perl code, rather than using an external config file. --- action: /login indicator: submit auto_fieldset: 1 elements: - type: Text name: user constraints: - Required - type: Password name: pass constraints: - Required - type: Submit name: submit constraints: - SingleValue =head1 DESCRIPTION L is a HTML form framework which aims to be as easy as possible to use for basic web forms, but with the power and flexibility to do anything else you might want to do (as long as it involves forms). You can configure almost any part of formfu's behaviour and output. By default formfu renders "XHTML 1.0 Strict" compliant markup, with as little extra markup as possible, but with sufficient CSS class names to allow for a wide-range of output styles to be generated by changing only the CSS. All methods listed below (except L) can either be called as a normal method on your C<$form> object, or as an option in your config file. Examples will mainly be shown in L config syntax. This documentation follows the convention that method arguments surrounded by square brackets C<[]> are I, and all other arguments are required. =head1 BUILDING A FORM =head2 new Arguments: [\%options] Return Value: $form Create a new L object. Any method which can be called on the L object may instead be passed as an argument to L. my $form = HTML::FormFu->new({ action => '/search', method => 'GET', auto_fieldset => 1, }); =head2 load_config_file Arguments: $filename Arguments: \@filenames Return Value: $form Accepts a filename or list of file names, whose filetypes should be of any format recognized by L. The content of each config file is passed to L, and so are added to the form. L may be called in a config file itself, so as to allow common settings to be kept in a single config file which may be loaded by any form. --- load_config_file: - file1 - file2 YAML multiple documents within a single file. The document start marker is a line containing 3 dashes. Multiple documents will be applied in order, just as if multiple filenames had been given. In the following example, multiple documents are taken advantage of to load another config file after the elements are added. (If this were a single document, the C would be called before C, regardless of its position in the file). --- elements: - name: one - name: two --- load_config_file: ext.yml Relative paths are resolved from the L directory if it is set, otherwise from the current working directory. See L for advice on organising config files. =head2 config_callback Arguments: \%options If defined, the arguments are used to create a L object during L which may be used to pre-process the config before it is sent to L. For example, the code below adds a callback to a form that will dynamically alter any config value ending in ".yml" to end in ".yaml" when you call L: $form->config_callback({ plain_value => sub { my( $visitor, $data ) = @_; s/\.yml/.yaml/; } }); Default Value: not defined This method is a special 'inherited accessor', which means it can be set on the form, a block element or a single element. When the value is read, if no value is defined it automatically traverses the element's hierarchy of parents, through any block elements and up to the form, searching for a defined value. =head2 populate Arguments: \%options Return Value: $form Each option key/value passed may be any L method-name and arguments. Provides a simple way to set multiple values, or add multiple elements to a form with a single method-call. Attempts to call the method-names in a semi-intelligent order (see the source of populate() in C for details). =head2 default_values Arguments: \%defaults Return Value: $form Set multiple field's default values from a single hash-ref. The hash-ref's keys correspond to a form field's name, and the value is passed to the field's L. This should be called after all fields have been added to the form, and before L is called (otherwise, call L again before rendering the form). =head2 config_file_path Arguments: $directory_name L defines where configuration files will be searched for, if an absolute path is not given to L. Default Value: not defined This method is a special 'inherited accessor', which means it can be set on the form, a block element or a single element. When the value is read, if no value is defined it automatically traverses the element's hierarchy of parents, through any block elements and up to the form, searching for a defined value. Is an L. =head2 indicator Arguments: $field_name Arguments: \&coderef If L is set to a fieldname, L will return true if a value for that fieldname was submitted. If L is set to a code-ref, it will be called as a subroutine with the two arguments C<$form> and C<$query>, and its return value will be used as the return value for L. If L is not set, L will return true if a value for any known fieldname was submitted. =head2 auto_fieldset Arguments: 1 Arguments: \%options Return Value: $fieldset This setting is suitable for most basic forms, and means you can generally ignore adding fieldsets yourself. Calling C<< $form->auto_fieldset(1) >> immediately adds a fieldset element to the form. Thereafter, C<< $form->elements() >> will add all elements (except fieldsets) to that fieldset, rather than directly to the form. To be specific, the elements are added to the I fieldset on the form, so if you add another fieldset, any further elements will be added to that fieldset. Also, you may pass a hashref to auto_fieldset(), and this will be used to set defaults for the first fieldset created. A few examples and their output, to demonstrate: 2 elements with no fieldset. --- elements: - type: Text name: foo - type: Text name: bar
2 elements with an L. --- auto_fieldset: 1 elements: - type: Text name: foo - type: Text name: bar
The 3rd element is within a new fieldset --- auto_fieldset: { id: fs } elements: - type: Text name: foo - type: Text name: bar - type: Fieldset - type: Text name: baz
Because of this behaviour, if you want nested fieldsets you will have to add each nested fieldset directly to its intended parent. my $parent = $form->get_element({ type => 'Fieldset' }); $parent->element('fieldset'); =head2 form_error_message Arguments: $string Normally, input errors cause an error message to be displayed alongside the appropriate form field. If you'd also like a general error message to be displayed at the top of the form, you can set the message with L. To set the CSS class for the message, see L. To change the markup used to display the message, edit the C template file. See L. Is an L. =head2 force_error_message If true, forces the L to be displayed even if there are no field errors. =head2 default_args Arguments: \%defaults Set defaults which will be added to every element, constraint, etc. of the given type which is subsequently added to the form. For example, to make every C element automatically have a size of C<10>, and make every C deflator automatically get its strftime set to C<%d/%m/%Y>: default_args: elements: Text: size: 10 deflators: Strftime: strftime: '%d/%m/%Y' An example to make all DateTime elements automatically get an appropriate Strftime deflator and a DateTime inflator: default_args: elements: DateTime: deflators: type: Strftime strftime: '%d-%m-%Y' inflators: type: DateTime parser: strptime: '%d-%m-%Y' =head3 Pseudo types As a special case, you can also use the C keys C, C and C to match any element which inherits from L or which C L or L. =head3 Alternatives Each C key can contain an C list using the C<|> divider: e.g. # apply the given class to any Element of type Password or Button default_args: elements: 'Password|Button': attrs: class: novalidate =head3 Match ancestor Each C key list can contain a type starting with C<+> to only match elements with an ancestor of the given type: e.g. # only apple the given class to an Input field within a Multi block default_args: elements: 'Input|+Multi': attrs: class: novalidate =head3 Don't match ancestor Each C key list can contain a type starting with C<-> to only match elements who do not have an ancestor of the given type: e.g. # apply the given class only to Input fields that are not in a Multi block default_args: elements: 'Input|-Multi': attrs: clasS: validate =head3 Order The arguments are applied in least- to most-specific order: C, C, C, C<$type>. Within each of these, arguments are applied in order of shortest-first to longest-last. The C key must match the value returned by C, e.g. L. If, for example, you have a custom element outside of the C namespace, which you load via C<< $form->element({ type => '+My::Custom::Element' }) >>, the key given to L should B include the leading C<+>, as that is stripped-out of the returned C value. Example: # don't include the leading '+' here default_args: elements: 'My::Custom::Element': attrs: class: whatever # do include the leading '+' here elements: - type: +My::Custom::Element =head3 Clashes L generates a single hashref to pass to L, merging arguments for each type in turn - meaning L is only called once in total - not once for each type. Because scalar values are B merged - this means later values will override earlier values: e.g. # Normally, calling $field->add_attrs({ class => 'input' }) # then calling $field->add_attrs({ class => 'not-in-multi' }) # would result in both values being retained: # class="input not-in-multi" # # However, default_args() creates a single data-structure to pass once # to populate(), so any scalar values will overwrite earlier ones # before they reach populate(). # # The below example would result in the longest-matching key # overwriting any others: # class="not-in-multi" # default_args: elements: Input: add_attrs: class: input 'Input:-Multi': add_attrs: class: not-in-multi =head3 Strictness Note: Unlike the proper methods which have aliases, for example L which is an alias for L - the keys given to C must be of the plural form, e.g.: default_args: elements: {} deflators: {} filters: {} constraints: {} inflators: {} validators: {} transformers: {} output_processors: {} =head2 javascript If set, the contents will be rendered within a C