FormValidator::Assets

YAPC::Asia 2008 Tokyo

May 16th, 2008

Daisuke Murase (typester)

...

Daisuke Murase (typester)

...

Daisuke Murase (typester)

...

Daisuke Murase (typester)

FormValidator::Assets

同じことを何回も書きたくない人のためのFVモジュール

CPANにすでにいっぱいある

...

CPANにすでにいっぱいある

Data::FormValidator

...

Data::FormValidator

...

Data::FormValidator

FormValidator::Simple

...

FormValidator::Simple

...

FormValidator::Simple

...

FormValidator::Simple

HTML::FormFu

...

HTML::FormFu

...

HTML::FormFu

...

HTML::FormFu

...

HTML::FormFu

でも全部DRYじゃない

たとえば

ユーザー登録とログイン

login:
  username: ...
  password: ...

signup:
  username: ...
  password: ...
  ...

Don't repeat yourself!

FormValidator::Assets

...

FormValidator::Assets

...

FormValidator::Assets

...

FormValidator::Assets

...

FormValidator::Assets

...

FormValidator::Assets

SYNOPSIS

my $f = FormValidator::Assets(
    assets_dir => '/path/to/assets',
);

my $res = $f->process( $query );

if ($res->has_error) {

}
else {

}

assetファイル

...

assetファイル

...

assetファイル

assets_dir/
  fields/
    username.pl
    password.pl
...

assetファイル

assets_dir/
  fields/
    username.pl
    password.pl
...

assetファイル

assets_dir/
  fields/
    username.pl
    password.pl

field

field 'username';

use_filter 'decode';
use_filter 'chomp';

use_rule 'required';
use_rule 'length' => 10;

filter

filter 'decode';

use Encode;

process {
    decode('utf-8', $_[0]);
};

rule

rule 'required';

process {
    defined($_[0]) && length($_[0]);
};

ほかの要素と絡むrule

fields 'password_again';

use_rule 'depends' => ('password');

ほかの要素と絡むrule

rule 'depends';

process {
    my ($value, $args, $context) = @_;

    for $depend (@$args) {
       # check $context->params->{ $depend }
    }
};

bundle

bundle 'login';

fields qw(username password);

bundle

my $f = FormValidator::Assets->new(..);
my $bundled = $f->bundle('login');

my $res = $bundled->process( $query );

Development

ideas and paches are always welcome!

Thank you!

Any questions?