package MyApp;

use strict;
use warnings;
use base qw(CGI::Application);
use CGI::Application::Plugin::FormValidator::Simple;
use CGI::Application::Plugin::TT;
use CGI::Application::Plugin::Redirect;

sub setup{
	my $self = shift;
	$self->start_mode('index');
	$self->run_modes(
		'index'=>'dispatch_index',
		'do_index'=>'post_dispatch_index'
	);
	$self->mode_param(path_info=>1);
	$self->header_props(-charset=>'utf8');
	
	#validatorのmessage設定
	$self->validator->set_messages({
		action1=>{
			mail=>{
				NOT_BLANK=>'メールアドレスを入力してください',
				EMAIL=>'メールアドレスが正しくありません',
			},
			password=>{
				NOT_BLANK=>'パスワードを入力してください',
				DEFAULT=>'パスワードは英数字5文字〜32文字で入力してください'
			},
			password_confirmation=>{
				DEFAULT=>'パスワードが一致しません'
			}
		}
	});
}

sub dispatch_index{
	my $self = shift;
	return $self->redirect($self->query->url . "/") if length $self->query->path_info < 1;
	return $self->tt_process('index.tt.html');
}

sub post_dispatch_index{
	my $self = shift;
	$self->form(
		mail=>['NOT_BLANK','EMAIL'],
		password=>['NOT_BLANK','ASCII',['LENGTH',5,32]],
		{password_confirmation=>[qw/password password_confirmation/]}=>['DUPLICATION']
	);
	return $self->tt_process('index.tt.html') if $self->form->has_error;
	return $self->tt_process('index2.tt.html');
}

1;

