- 追加された行はこの色です。
- 削除された行はこの色です。
* ヴァリデーションの仕方 [#x62f2a7f]
* FormValidator::Simpleでヴァリデーション [#b6af0ec7]
[[FormValidator-Simple>http://search.cpan.org/perldoc?FormValidator::Simple]]を使う。
** 設定 [#z2faef52]
myapp.ymlは以下のようにする。
validator:
plugins:
- Japanese
options:
charset: euc
messages: conf/messages.yml
messages.ymlに「名前は10字以内です」のようなメッセージを書く。
member:
e_mail:
DEFAULT: メールアドレスが正しくありません。
login_id:
DEFAULT: ログインIDが正しくありません。
LENGTH: ログインIDは10-12文字にする必要があります。
REGEX: ログインIDは半角英数字、ハイフン、アンダースコアで入力してください。
password:
DEFAULT: パスワードが正しくありません。
LENGTH: パスワードは10-12文字にする必要があります。
REGEX: パスワードは半角英数字、ハイフン、アンダースコアで入力してください。
** アクションクラス [#ra48249b]
$c->form(
login_id => [qw/NOT_BLANK/, ['REGEX', qr/[?w?d-]+/], [qw/LENGTH 10 12/]],
password => [qw/NOT_BLANK/, ['REGEX', qr/[?w?d-]+/], [qw/LENGTH 10 12/]],
login_id => [qw/NOT_BLANK/, ['REGEX', qr/[\w\d-]+/], [qw/LENGTH 10 12/]],
password => [qw/NOT_BLANK/, ['REGEX', qr/[\w\d-]+/], [qw/LENGTH 10 12/]],
name => [qw/NOT_BLANK/],
namekana => [qw/NOT_BLANK HIRAGANA/],
zip => [qw/NOT_BLANK ZIP_JP/],
prefec => [qw/NOT_BLANK/],
tel => [qw/NOT_BLANK/, ['REGEX', qr/[?d-]+/]],
e_mail => [qw/NOT_BLANK EMAIL_LOOSE/],
sex => [qw/NOT_BLANK INT/],
birth => [qw/NOT_BLANK/, ['DATETIME_STRPTIME','%Y-%m-%d']],
);
if ($prefec) {
if (! grep {$prefec eq $_ } @{$c->config->{prefectures}}) {
$c->set_invalid_form(prefec => 'PREFECTURES');
}
}
$c->stash->{template} = 'member/regist.tt2' if $c->form->has_error;
** テンプレート [#n2edb34d]
[% IF Catalyst.form.has_error -%]
<h3>エラー!!</h3>
<ul>
[% FOREACH mesg IN Catalyst.form.messages('member') -%]
<li>[% mesg %]</li>
[% END -%]
</ul>
[% END -%]
** ヴァリデーションプラグインの作成 [#f9805b18]
- ↓ここを見る
- http://blog.nomadscafe.jp/archives/000510.html
- アプリケーションルート直下に適当なディレクトリ(commonとか)を作って、myapp.pmの中でuse lib 'common'などとするとよい。
** 参考 [#y71fe044]
- ↓ここ見ると分かる。
- http://search.cpan.org/~lyokato/Catalyst-Plugin-FormValidator-Simple-0.12/lib/Catalyst/Plugin/FormValidator/Simple.pm
- http://search.cpan.org/perldoc?Catalyst::Plugin::FormValidator::Simple
- ↓それ以外の参考。
- http://search.cpan.org/~lyokato/FormValidator-Simple-0.22/lib/FormValidator/Simple.pm
- http://search.cpan.org/~lyokato/FormValidator-Simple-Plugin-Japanese-0.04/lib/FormValidator/Simple/Plugin/Japanese.pm
- http://search.cpan.org/~markstos/Data-FormValidator-4.50/lib/Data/FormValidator/Results.pm
- http://search.cpan.org/perldoc?FormValidator::Simple
- http://search.cpan.org/perldoc?FormValidator::Simple::Plugin::Japanese
- http://search.cpan.org/perldoc?Data::FormValidator