Skip to content

Instantly share code, notes, and snippets.

@jjn1056
Last active April 19, 2020 17:34
Show Gist options
  • Save jjn1056/75bfb1cd1186419087780ead40a82186 to your computer and use it in GitHub Desktop.
Save jjn1056/75bfb1cd1186419087780ead40a82186 to your computer and use it in GitHub Desktop.
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;
@jjn1056
Copy link
Author

jjn1056 commented Apr 19, 2020

[info] *** Request 1 (0.009/s) [48339] [Sun Apr 19 12:29:01 2020] ***
[debug] Path is "/users/register"
[debug] "POST" request for "register" from "127.0.0.1"
[debug] Body Parameters are:
.-------------------------------------+--------------------------------------------------------------------------.
| Parameter                           | Value                                                                    |
+-------------------------------------+--------------------------------------------------------------------------+
| address                             | 15604 Harry Lind Road                                                    |
| city                                | Elgin                                                                    |
| first_name                          | john                                                                     |
| last_name                           | nap                                                                      |
| password                            | johnjohn                                                                 |
| password_confirmation               | johnjohn                                                                 |
| state.name                          | Texas                                                                    |
| username                            | john                                                                     |
| zip                                 | 78621                                                                    |
'-------------------------------------+--------------------------------------------------------------------------'
{
  address => "15604 Harry Lind Road",
  city => "Elgin",
  first_name => "john",
  last_name => "nap",
  password => "johnjohn",
  password_confirmation => "johnjohn",
  state => {
    name => "Texas",
  },
  username => "john",
  zip => 78621,
}
[error] Caught exception in Example::Controller::Users->register "Can't use an undefined value as a HASH reference at /Users/jnapiorkowski/.perlbrew/libs/perl-5.28.1@default/lib/perl5/DBIx/Class/Row.pm line 756."
127.0.0.1 - - [19/Apr/2020:12:29:01 -0500] "POST /register HTTP/1.1" 500 20669 "http://localhost:5000/register" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment