Last active
April 19, 2020 17:34
-
-
Save jjn1056/75bfb1cd1186419087780ead40a82186 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package DBIx::Class::Result::Valiant; | |
use DBIx::Class::MooResultClass; | |
extends 'DBIx::Class'; | |
with 'Valiant::Validates'; | |
sub register_column { | |
my $self = shift; | |
my ($column, $info) = @_; | |
$self->next::method(@_); | |
use Devel::Dwarn; | |
Dwarn \@_; | |
} | |
1; | |
package Example::Schema::Result::Person; | |
use base 'Example::Schema::Result'; | |
__PACKAGE__->table("person"); | |
__PACKAGE__->load_components(qw/EncodedColumn Result::Valiant/); | |
__PACKAGE__->add_columns( | |
id => { data_type => 'bigint', is_nullable => 0, is_auto_increment => 1 }, | |
username => { data_type => 'varchar', is_nullable => 0, size => 48 }, | |
first_name => { data_type => 'varchar', is_nullable => 0, size => 24 }, | |
last_name => { data_type => 'varchar', is_nullable => 0, size => 48 }, | |
address => { data_type => 'varchar', is_nullable => 0, size => 48 }, | |
city => { data_type => 'varchar', is_nullable => 0, size => 32 }, | |
zip => { data_type => 'varchar', is_nullable => 0, size => 5 }, | |
state_id => { data_type => 'integer', is_nullable => 0, is_foreign_key => 1 }, | |
password => { | |
data_type => 'varchar', | |
is_nullable => 0, | |
size => 64, | |
encode_column => 1, | |
encode_class => 'Digest', | |
encode_args => { algorithm => 'MD5', format => 'base64' }, | |
encode_check_method => 'check_password', | |
}, | |
); | |
__PACKAGE__->validates(username => presence=>1, length=>[3,24], format=>'alpha_numeric'); | |
__PACKAGE__->validates(password => presence=>1, length=>[6,24], confirmation=>1); | |
__PACKAGE__->validates(first_name => (presence=>1, length=>[2,24])); | |
__PACKAGE__->validates(last_name => (presence=>1, length=>[2,48])); | |
__PACKAGE__->validates(address => (presence=>1, length=>[2,48])); | |
__PACKAGE__->validates(city => (presence=>1, length=>[2,32])); | |
__PACKAGE__->validates(state => (presence=>1, length=>[2,18])); | |
__PACKAGE__->validates(zip => (presence=>1, format=>'zip')); | |
__PACKAGE__->set_primary_key("id"); | |
__PACKAGE__->add_unique_constraint(['username']); | |
__PACKAGE__->belongs_to( | |
state => | |
'Example::Schema::Result::State', | |
{ 'foreign.id' => 'self.state_id' } | |
); | |
sub registered { | |
my $self = shift; | |
return $self->validated && $self->valid; | |
} | |
1; | |
package Example::Model::Register; | |
use Moo; | |
sub ACCEPT_CONTEXT { | |
my ($class, $c) = @_; | |
if($c->req->method eq 'POST') { | |
my %params = %{$c->req->body_data}{qw/ | |
username | |
password | |
password_confirmation | |
first_name | |
last_name | |
address | |
city | |
state | |
zip | |
/}; | |
use Devel::Dwarn; | |
Dwarn \%params; | |
my $model = $c->model('Schema::Person')->create(\%params); | |
$model->validate; | |
return $model; | |
} else { | |
return $c->model('Schema::Person')->new_result(+{}); | |
} | |
} | |
1; | |
package Example::Controller::Users; | |
use Moose; | |
use MooseX::MethodAttributes; | |
extends 'Catalyst::Controller'; | |
sub root :Chained(../root) PathPart('') CaptureArgs(0) {} | |
sub register :Chained(root) Args(0) { | |
my ($self, $c) = @_; | |
my $model = $c->model('Register'); | |
$c->redirect_to_action('/session/login') if $model->registered; | |
$c->stash(model=>$model); | |
} | |
__PACKAGE__->meta->make_immutable; |
Author
jjn1056
commented
Apr 19, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment