[html-formfu] Inflator for NetAddr::IP object

Bill Uhl bill at greenlightnet.com
Sat Jun 16 21:44:15 GMT 2018


So after more searching and reading of source, I think I have this working.
It looks like the inflator has to die upon failure to trigger the error
within the eval that inflates the input into an object. At least that seems
to be working now. The final code for the module looks like this:

package HTML::FormFu::Inflator::NetAddr::IP;

use strict;
use warnings;

use Moose;
use NetAddr::IP;
use namespace::autoclean;

extends 'HTML::FormFu::Inflator';

sub inflator {
    my ( $self, $value ) = @_;

    return if !defined $value || $value eq '';

    my $ip = NetAddr::IP->new($value);

    die if ref $ip ne 'NetAddr::IP';

    return $ip;
}


__PACKAGE__->meta->make_immutable;

1;

As this seems to be working, I am moving on to the next steps with the
project. If it seems to fail later, I will be back. In the meantime, I
would still like confirmation that this is the correct way to implement an
inflator.

Thanks,
Bill



On Thu, Jun 14, 2018 at 9:04 AM, Bill Uhl <bill at greenlightnet.com> wrote:

> Hi,
>
> I am trying to write an inflator that will produce a NetAddr::IP object
> from my form input. I have searched for but been unable to find much
> information about writing custom inflators. If there are examples or
> additional documentation available, links would be greatly appreciated.
>
> I am working on my first Catalyst web site with DBIC and FormFu. I am
> using the built-in Catalyst server to display the web pages. The whole
> thing is running on a CentOS 7.5 VM on one of my servers.
>
> I took a look at the source for the core inflators and from what I
> (mis)understand of the code, the inflator provides an inflator method that
> returns on failure or returns the inflated object upon success. I have
> written the following but it does not work as I expected.
>
> package HTML::FormFu::Inflator::NetAddr::IP;
> use Moose;
> use HTML::FormFu::Constants qw( $EMPTY_STR );
> use NetAddr::IP;
> use namespace::autoclean;
>
> extends 'HTML::FormFu::Inflator';
>
> sub inflator {
>     my ( $self, $value ) = @_;
>
>     return if !defined $value || $value eq $EMPTY_STR;
>
>     my $ip = NetAddr::IP->new($value);
>     return if !defined $ip;
>
>     return $ip;
> }
>
> __PACKAGE__->meta->make_immutable;
>
> 1;
>
> With the following form config...
>
>   # Text box for the network subnet address
>   - type: Text
>     name: subnet
>     label: Subnet
>     attributes:
>       title: Enter the Subnet Address for this network in CIDR format.
>     inflators:
>       - type: NetAddr::IP
>         message: Not a valid subnet address.
>
> Even when I enter a valid address that should be inflatable, such as
> 10.255.255.0/24,  I get the 'Not a valid subnet address' error message.
>
> Any help or redirection would be appreciated.
>
> Bill
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.scsys.co.uk/pipermail/html-formfu/attachments/20180616/60fd367b/attachment.htm>


More information about the HTML-FormFu mailing list