NAME

Data::MuFormX::Registry - Registry of Form classes

SYNOPSIS

Given some Data::MuForms in a common namespace:

package MyApp::Form::Login;

use Moo;
use Data::MuForm::Meta;

extends 'Data::MuForm';

has_field 'username' => (
  type => 'Text',
  required => 1 );

package MyApp::Form::Email;

use Moo;
use Data::MuForm::Meta;

extends 'Data::MuForm';

has_field 'username' => (
  type => 'Text',
  required => 1 );

Create a 'registry' object that will load a prepare all the forms:

my $registry = Data::MuFormX::Registry->new(form_namespace=>'MyApp::Form');
my $login = $registry->create('Login'); # $login ISA MyApp::Form::Login

You may also subclass for hardcoded defaults

package MyApp::MyRegistry;

use Moo;
extends 'Data::MuFormX::Registry';

1;

# 'form_namespace defaults to 'MyApp::Form'
my $registry = MyApp::MyRegistry->new;

DESCRIPTION

NOTE Early access; the docs do not describe all existing features (read the source :) ) and I reserve the right to break stuff if that's the only way to fix deep problems. On the other hand there's not a ton of stuff here so its probably ok...

This is a wrapper on top of Module::Pluggable::Object to make it easier to load up and create a namespace of Data::MuForm based form validation classes. At its heart it makes it so you don't have to say 'use Form;' for every form you need in a package. It also adds a way to centralize some form initialization work. This may or may not recommend itself to you. I think it makes it easier to reuse forms in different packages (for example in different Mojolicous controllers). On the other hand it injects a proxy layer such that '$registry->create("Login")' is not 100% transparent in that you are getting an instance of 'MyApp::Form::Login'. You may consider this a type of action at a distance that makes your code harder to maintain.

If you have a lot of Data::MuForm based form validation classes you may find it more useful. I also believe it helps you follow the 'code against an interface not an class' best practice. As you wish ;)

METHOD

This class exposes the follow methods for intended public use.

new

Create a new registry object. You can set the following initial arguments:

form_namespace

Either a scalar or array ref of the base namespaces used to find forms.

config

configuration values used when creating form objects.

create

Create a new form. Requires a form name. May accept a hash of additional initialization values (which are merged with any global configuration. Examples:

my $transaction = $registry->create('Transaction');
my $login = $registry->create('Login', user_rs=>$users);

If you do not need to pass any extra arguments we reuse a pre-initialized copy of the form rather than build a new one as a performance enhancement.

form_names

Returns an array of the form names, which can be used in "create". Do not rely on return order!

config

Global configuration information for all forms.

Intended to be overridden in a subclass to provide form defaults. For example:

package MyRegistry;

use Moo;
extends 'Data::MuFormX::Registry';

sub config {
  'Login' => + {
    min_username_length => 11,
  },
  'NewNodes' => sub {
    my ($self) = @_;
    return +{
      example1 => 1,
      example2 => 1,
    };
  },
}

This method should return a hash where the key is the form name and the value is either a hashref used as part of the instantiation of the form or a coderef which recieves the registry instance and should return a hashref. The second form is useful in cases where you have a form that itself has other forms as subforms or when you custom subclass contains additional information of value to the form (such as a database connection).

AUTHOR

John Napiorkowski email:jjnapiork@cpan.org

SEE ALSO

Data::MuForm, Module::Pluggable::Object

COPYRIGHT & LICENSE

Copyright 2018, John Napiorkowski email:jjnapiork@cpan.org

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