Cataly...FormFu(3pm)

πŸ“¦
version 2.04 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

okay bestie, gather round πŸ’– this is Catalyst::Controller::HTML::FormFu, straight out of the User Contributed Perl Documentation corner (you know, the 3pm zone). It's the little matchmaker that gets your Catalyst controllers and your HTML::FormFu forms to be besties. No cap.


πŸŽ€ NAME πŸŽ€

Catalyst::Controller::HTML::FormFu β€” Catalyst integration for HTML::FormFu 🫢


✨ VERSION ✨

version 2.04


πŸ¦‹ SYNOPSIS πŸ¦‹

okay so first you set up the package, and you gotta use Moose and use namespace::autoclean, and then you extends the controller. the BEGIN block is doing the most here:

package MyApp::Controller::My::Controller;

use Moose;
use namespace::autoclean;

BEGIN { extends 'Catalyst::Controller::HTML::FormFu'; }

next up, an action that does not use an Attribute to make a form β€” and ngl, you can still just grab an empty form from $self->form(), so it's giving low-effort-but-cute:

sub index : Local {
    my ( $self, $c ) = @_;

    # doesn't use an Attribute to make a form
    # can get an empty form from $self->form()

    my $form = $self->form();
}

now here's the Form attribute, and the way it just... does the whole routine for you? iconic:

sub foo : Local : Form {
    my ( $self, $c ) = @_;

    # using the Form attribute is equivalent to:
    #
    # my $form = $self->form;
    #
    # $form->process;
    #
    # $c->stash->{form} = $form;
}

then we got FormConfig, which reads your config filestem and processes everything for you, so all you're left doing is the fun part:

sub bar : Local : FormConfig {
    my ( $self, $c ) = @_;

    # using the FormConfig attribute is equivalent to:
    #
    # my $form = $self->form;
    #
    # $form->load_config_filestem('root/forms/my/controller/bar');
    #
    # $form->process;
    #
    # $c->stash->{form} = $form;
    #
    # so you only need to do the following...

    my $form = $c->stash->{form};

    if ( $form->submitted_and_valid ) {
        do_something();
    }
}

and FormConfig can take an argument too, if you wanna point at a specific config β€” same energy, different filestem:

sub baz : Local : FormConfig('my_config') {
    my ( $self, $c ) = @_;

    # using the FormConfig attribute with an argument is equivalent to:
    #
    # my $form = $self->form;
    #
    # $form->load_config_filestem('root/forms/my_config');
    #
    # $form->process;
    #
    # $c->stash->{form} = $form;
    #
    # so you only need to do the following...

    my $form = $c->stash->{form};

    if ( $form->submitted_and_valid ) {
        do_something();
    }
}

then there's FormMethod, which calls one of your own methods and populates the form with whatever it hands back. so obsessed with the separation of concerns here:

sub quux : Local : FormMethod('load_form') {
    my ( $self, $c ) = @_;

    # using the FormMethod attribute with an argument is equivalent to:
    #
    # my $form = $self->form;
    #
    # $form->populate( $c->load_form );
    #
    # $form->process;
    #
    # $c->stash->{form} = $form;
    #
    # so you only need to do the following...

    my $form = $c->stash->{form};

    if ( $form->submitted_and_valid ) {
        do_something();
    }
}

sub load_form {
    my ( $self, $c ) = @_;

    # Automatically called by the above FormMethod('load_form') action.
    # Called as a method on the controller object, with the context
    # object as an argument.

    # Must return a hash-ref suitable to be fed to $form->populate()
}

πŸ’‘ and plot twist: you can also use specially-named actions that only get called under certain circumstances. like, they know when to show up. main character energy fr:

sub edit : Chained('group') : PathPart : Args(0) : FormConfig { }

sub edit_FORM_VALID {
    my ( $self, $c ) = @_;

    my $form  = $c->stash->{form};
    my $group = $c->stash->{group};

    $form->model->update( $group );

    $c->response->redirect( $c->uri_for( '/group', $group->id ) );
}

sub edit_FORM_NOT_SUBMITTED {
    my ( $self, $c ) = @_;

    my $form  = $c->stash->{form};
    my $group = $c->stash->{group};

    $form->model->default_values( $group );
}

πŸ’… METHODS πŸ’…

🧚 form

this one creates a brand new HTML::FormFu object, and passes the contents of the "constructor" config value as its argument. slay.

it's genuinely useful when you're using the ConfigForm() or MethodForm() action attributes and you want a 2nd form that isn't populated from a config-file or a method return value. like a lil bonus form. πŸŽ€

sub foo : Local {
    my ( $self, $c ) = @_;

    my $form = $self->form;
}
⚠️ heads up bestie: when you use this method, the form's query method is not populated with the Catalyst request object. no cap, don't forget that part.

πŸ‘‘ SPECIAL ACTION NAMES πŸ‘‘

okay so here's an example of how a complicated action method can get broken down into smaller sections, which makes it way clearer which code runs, and when. it's giving organized.

this big messy vibe:

sub edit : Local : FormConfig {
    my ( $self, $c ) = @_;

    my $form  = $c->stash->{form};
    my $group = $c->stash->{group};

    $c->detach('/unauthorised') unless $c->user->can_edit( $group );

    if ( $form->submitted_and_valid ) {
        $form->model->update( $group );

        $c->response->redirect( $c->uri_for('/group', $group->id ) );
        return;
    }
    elsif ( !$form->submitted ) {
        $form->model->default_values( $group );
    }

    $self->_add_breadcrumbs_nav( $c, $group );
}

instead becomes... ✨ this ✨:

sub edit : Local : FormConfig {
    my ( $self, $c ) = @_;

    $c->detach('/unauthorised') unless $c->user->can_edit(
        $c->stash->{group}
    );
}

sub edit_FORM_VALID {
    my ( $self, $c ) = @_;

    my $group = $c->stash->{group};

    $c->stash->{form}->model->update( $group );

    $c->response->redirect( $c->uri_for('/group', $group->id ) );
}

sub edit_FORM_NOT_SUBMITTED {
    my ( $self, $c ) = @_;

    $c->stash->{form}->model->default_values(
        $c->stash->{group}
    );
}

sub edit_FORM_RENDER {
    my ( $self, $c ) = @_;

    $self->_add_breadcrumbs_nav( $c, $c->stash->{group} );
}

so the tea is: for any action method that uses a "Form", "FormConfig" or "FormMethod" attribute, you can add extra methods that follow the naming conventions below. 🫢

and these methods get called after the original, plainly named action method. periodt.

_FORM_VALID

Run when the form has been submitted and has no errors. βœ…

_FORM_SUBMITTED

Run when the form has been submitted, regardless of whether or not there was errors. (so even if it's messy, it still shows up πŸ’€)

_FORM_COMPLETE

For MultiForms, is run if the MultiForm is completed. πŸŽ‰

_FORM_NOT_VALID

Run when the form has been submitted and there were errors. 😭

_FORM_NOT_SUBMITTED

Run when the form has not been submitted. πŸ™ˆ

_FORM_NOT_COMPLETE

For MultiForms, is run if the MultiForm is not completed.

_FORM_RENDER

For normal "Form" base classes, this subroutine is run after any of the other special methods, unless $form->submitted_and_valid is true.

For "MultiForm" base classes, this subroutine is run after any of the other special methods, unless $multi->complete is true.


πŸŽ€ CUSTOMIZATION πŸŽ€

okay bestie so you can totally set your own config settings β€” either through your controller config or your application config. pick your fighter πŸ’…

$c->config( 'Controller::HTML::FormFu' => \%my_values );

# or

MyApp->config( 'Controller::HTML::FormFu' => \%my_values );

# or, in myapp.conf

<Controller::HTML::FormFu>
    default_action_use_path 1
</Controller::HTML::FormFu>

✨ form_method ✨

Override the method-name used to create a new form object.

See "form" bestie.

Default value: "form".

✨ form_stash ✨

Sets the stash key name used to store the form object.

Default value: "form".

✨ form_attr ✨

Sets the attribute name used to load the Catalyst::Controller::HTML::FormFu::Action::Form action.

Default value: "Form".

✨ config_attr ✨

Sets the attribute name used to load the Catalyst::Controller::HTML::FormFu::Action::Config action.

Default value: "FormConfig".

✨ method_attr ✨

Sets the attribute name used to load the Catalyst::Controller::HTML::FormFu::Action::Method action.

Default value: "FormMethod".

✨ form_action ✨

Sets which package will be used by the Form() action.

Probably only useful if you want to create a sub-class which provides custom behaviour, iykyk.

Default value: "Catalyst::Controller::HTML::FormFu::Action::Form".

✨ config_action ✨

Sets which package will be used by the Config() action.

Probably only useful if you want to create a sub-class which provides custom behaviour.

Default value: "Catalyst::Controller::HTML::FormFu::Action::Config".

✨ method_action ✨

Sets which package will be used by the Method() action.

Probably only useful if you want to create a sub-class which provides custom behaviour.

Default value: "Catalyst::Controller::HTML::FormFu::Action::Method".

✨ constructor ✨

Pass common defaults to the HTML::FormFu constructor, slay.

These values are used by all of the action attributes, and by the $self->form method.

Default value: "{}".

✨ config_callback ✨

Arguments: bool

If true, a coderef is passed to $form->config_callback->{plain_value} which replaces any instance of __uri_for(URI)__ found in form config files with the result of passing the URI argument to uri_for in Catalyst. iconic behavior tbh πŸ’–

The form __uri_for(URI, PATH, PARTS)__ is also supported, which is equivalent to $c->uri_for( 'URI', \@ARGS ). At this time, there is no way to pass query values equivalent to $c->uri_for( 'URI', \@ARGS, \%QUERY_VALUES ).

The second codeword that is being replaced is __path_to( @DIRS )__. Any instance is replaced with the result of passing the DIRS arguments to path_to in Catalyst. Don't use qoutationmarks as they would become part of the path. ⚠️

Default value: 1

✨ default_action_use_name ✨

If set to a true value the action for the form will be set to the currently called action name.

Default value: "false".

✨ default_action_use_path ✨

If set to a true value the action for the form will be set to the currently called action path. The action path includes concurrent to action name additioal parameters which were code inside the path.

Default value: "false".

Example:

action: /foo/bar
called uri contains: /foo/bar/1

# default_action_use_name => 1 leads to:
$form->action = /foo/bar

# default_action_use_path => 1 leads to:
$form->action = /foo/bar/1

✨ model_stash ✨

Arguments: \%stash_keys_to_model_names

Used to place Catalyst models on the form stash. 🫢

If it's being used to make a DBIx::Class schema available for options_from_model in HTML::FormFu::Model::DBIC, for Select and other Group-type elements β€” then the hash-key must be schema. For example, if your schema model class is MyApp::Model::MySchema, you would set model_stash like so:

<Controller::HTML::FormFu>
    <model_stash>
        schema MySchema
    </model_stash>
</Controller::HTML::FormFu>

✨ context_stash ✨

To allow your form validation packages, etc, access to the catalyst context, a weakened reference of the context is copied into the form's stash.

$form->stash->{context};

This setting allows you to change the key name used in the form stash.

Default value: "context".

✨ languages_from_context ✨

If you're using a L10N / I18N plugin such as Catalyst::Plugin::I18N which provides a languages method that returns a list of valid languages to use for the currect request β€” and you want to use formfu's built-in I18N packages, then setting languages_from_context 🌈

✨ localize_from_context ✨

If you're using a L10N / I18N plugin such as Catalyst::Plugin::I18N which provides it's own localize method, you can set localize_from_context to use that method for formfu's localization. 🌸

✨ request_token_enable ✨

If true, adds an instance of HTML::FormFu::Plugin::RequestToken to every form, to stop accidental double-submissions of data and to prevent CSRF attacks. lowkey a security baddie πŸ’…

✨ request_token_field_name ✨

Defaults to "_token".

✨ request_token_session_key ✨

Defaults to "__token".

✨ request_token_expiration_time ✨

Defaults to 3600.


πŸ’€ DISCONTINUED CONFIG SETTINGS πŸ’€

config_file_ext

Support for this has now been removed. Config files are now searched for, with any file extension supported by Config::Any. it's giving "gone, no cap" 😭

config_file_path

Support for this has now been removed. Use {constructor}{config_file_path} instead.


⚠️ CAVEATS ⚠️

When using the Form action attribute to create an empty form, you must call $form->process after populating the form. However, you don't need to pass any arguments to process, as the Catalyst request object will have automatically been set in $form->query. bestie do NOT forget this step fr fr πŸ™ˆ

When using the FormConfig and FormMethod action attributes, if you make any modifications to the form, such as adding or changing it's elements, you must call $form->process before rendering the form.


πŸ‘‘ AUTHORS πŸ‘‘

  • Carl Franks <cpan@fireartist.com>
  • Nigel Metheringham <nigelm@cpan.org>
  • Dean Hamstead <dean@bytefoundry.com.au>

This software is copyright (c) 2007-2018 by Carl Franks / Nigel Metheringham / Dean Hamstead.

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


🫢 SUPPORT 🫢

Perldoc

You can find documentation for this module with the perldoc command.

perldoc Catalyst::Controller::HTML::FormFu

Websites

The following websites have more information about this module, and may be of help to you. As always, in addition to those websites please use your favorite search engine to discover more resources. πŸ”

  • MetaCPAN

A modern, open-source CPAN search engine, useful to view POD in HTML format.

<http://metacpan.org/release/Catalyst-Controller-HTML-FormFu>

  • Search CPAN

The default CPAN search engine, useful to view POD in HTML format.

<http://search.cpan.org/dist/Catalyst-Controller-HTML-FormFu>

  • RT: CPAN's Bug Tracker

The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.

<https://rt.cpan.org/Public/Dist/Display.html?Name=Catalyst-Controller-HTML-FormFu>

  • AnnoCPAN

The AnnoCPAN is a website that allows community annotations of Perl module documentation.

<http://annocpan.org/dist/Catalyst-Controller-HTML-FormFu>

  • CPAN Ratings

The CPAN Ratings is a website that allows community ratings and reviews of Perl modules.

<http://cpanratings.perl.org/d/Catalyst-Controller-HTML-FormFu>

  • CPAN Forum

The CPAN Forum is a web forum for discussing Perl modules.

<http://cpanforum.com/dist/Catalyst-Controller-HTML-FormFu>

  • CPANTS

The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.

<http://cpants.cpanauthors.org/dist/Catalyst-Controller-HTML-FormFu>

  • CPAN Testers

The CPAN Testers is a network of smokers who run automated tests on uploaded CPAN distributions.

<http://www.cpantesters.org/distro/C/Catalyst-Controller-HTML-FormFu>

  • CPAN Testers Matrix

The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.

<http://matrix.cpantesters.org/?dist=Catalyst-Controller-HTML-FormFu>

  • CPAN Testers Dependencies

The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.

<http://deps.cpantesters.org/?module=Catalyst::Controller::HTML::FormFu>

Bugs / Feature Requests

Please report any bugs or feature requests by email to bug-catalyst-controller-html-formfu at rt.cpan.org, or through the web interface at <https://rt.cpan.org/Public/Bug/Report.html?Queue=Catalyst-Controller-HTML-FormFu>. You will be automatically notified of any progress on the request by the system. ✨

Source Code

The code is open to the world, and available for you to hack on. Please feel free to browse it and play with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull from your repository :)

<https://github.com/FormFu/Catalyst-Controller-HTML-FormFu>

git clone https://github.com/FormFu/Catalyst-Controller-HTML-FormFu.git

🌟 CONTRIBUTORS 🌟

  • Aran Deltac <aran@ziprecruiter.com>
  • bricas <brian.cassidy@gmail.com>
  • dandv <ddascalescu@gmail.com>
  • fireartist <fireartist@gmail.com>
  • lestrrat <lestrrat+github@gmail.com>
  • marcusramberg <marcus.ramberg@gmail.com>
  • mariominati <mario.minati@googlemail.com>
  • Moritz Onken <1nd@gmx.de>
  • Moritz Onken <onken@netcubed.de>
  • Nigel Metheringham <nm9762github@muesli.org.uk>
  • omega <andreas.marienborg@gmail.com>
  • Petr PΓ­saΕ™ <ppisar@redhat.com>

and that's a wrap bestie!! πŸŽ€βœ¨ this page was built for perl v5.36.0, dated 2022-12-06 β€” go forth and slay your forms, no cap πŸ’…πŸ’–

πŸ“¦ where this page comes from