Skip to content

dim0xff/p5-Form-Data-Processor-Field-Subforms

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NAME

Form::Data::Processor::Field::Subforms - use forms like subfields

SYNOPSIS

package Shipping::Free {
    use Form::Data::Processor::Moose;
    extends 'Form::Data::Processor::Form';

    has_field available_hours => (...);
};

package Shipping::ByOurStore {
    use Form::Data::Processor::Moose;
    extends 'Form::Data::Processor::Form';

    has_field cost_per_order   => (...);
    has_field cost_per_product => (...);
    has_field handling_fee     => (...);
};

package Shipping::USPS {
    use Form::Data::Processor::Moose;
    extends 'Form::Data::Processor::Form';

    has_field package_size            => (...);
    has_field domestic_mail_type      => (...);
    has_field international_mail_type => (...);
};


#
# Multi-subform mode
# Dynamicaly load and validate data basing on `method` field value

package Shipping {
    use Form::Data::Processor::Moose;
    extends 'Form::Data::Processor::Form';

    has_field method => (
        type    => 'List::Single',
        options => [ 'Free', 'ByOurStore', 'USPS' ]
    );

    has_field method_data => (
        type           => 'Subforms',
        form_namespace => 'Shipping',
        name_field     => 'method',
    );
}


# ... and later

my $form = Shipping->new;
$form->process(
    {
        method      => 'ByOurStore',
        method_data => {
            cost_per_order   => 10.00,
            cost_per_product => 0.50,
            handling_fee     => 3.00,
        },
    }
) or do {
    if ( $form->field('method_data.handling_fee')->has_errors ) {
        die "Incorrect handling fee:", $form->field('method_data.handling_fee')->all_errors;
    }

    ...
};


#
# Single-subform mode
#

package Shipping::ByOurStore {
    use Form::Data::Processor::Moose;
    extends 'Form::Data::Processor::Form';

    has_field method_data => (
        type           => 'Subforms',
        form_namespace => 'Shipping',
        single_subform => 1,
        subform_name   => 'ByOurStore',
    );
}


# ... and later

my $form = Shipping::ByOurStore->new;
$form->process(
    {
        method_data => {
            cost_per_order   => 10.00,
            cost_per_product => 0.50,
            handling_fee     => 3.00,
        },
    }
) or do {
    if ( $form->field('method_data.handling_fee')->has_errors ) {
        die "Incorrect handling fee:", $form->field('method_data.handling_fee')->all_errors;
    }

    ...
};

DESCRIPTION

This add ability to use some forms like subfields in current form. To determine which "subform" will be used, it uses result from Form::Data::Processor::Field::List::Single field.

Before field is "ready" in Form::Data::Processor::Field it tries to load all classes via "form_namespace" and field ("name_field"). If some field option fires error, while class for this option is being tried to load, then this option is marked "disabled". Loading uses next notation: form_namespace::name_field option value.

On validating, input params will be passed to "subform" to validate.

Implement methods, which screen the same methods on child form:

all_error_fields
all_fields
error_fields
field
subfield

ATTRIBUTES

form_namespace

Type: Str
Required

Name space for subform loading.

name_field

Type: Str
Required (when "single_subform" is false)

Field name for Form::Data::Processor::Field::List::Single field from parent.

Notice: field in parent must be defined before current field.

single_subform

Type: Bool

When true then use single-subform mode (see "SYNOPSIS" for info).

subform_name

Type: Str
Required (when "single_subform" is true)

Subform name which will be used to create validation subform.

subform

Current validating subform.

Notice: normally is being set by Form::Data::Processor internals.

AUTHOR

Dmitry "dim0xff" Latin <dim0xff@gmail.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2017 by Dmitry Latin.

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

About

Use forms like subfields in your FDP

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages