Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unique accessor bug #19

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Bug: unable to make fields unique when the accessor differs from column name.

0.29
Fix tests broken because item is now cleared

Expand Down Expand Up @@ -78,4 +80,3 @@
Fix dependency errors
0.04 Sun July 26, 2009
Split from HTML::FormHandler distribution

1 change: 1 addition & 0 deletions dist.ini
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ HTML::FormHandler = 0.40016
Moose = 2.0007
DBIx::Class = 0.08250
DBIx::Class::ResultSet::RecursiveUpdate = 0.25
List::Util = 1.29
namespace::autoclean = 0.09

[Prereqs / TestRequires]
Expand Down
18 changes: 17 additions & 1 deletion lib/HTML/FormHandler/TraitFor/Model/DBIC.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use Moose::Role;
use Carp;
use DBIx::Class::ResultClass::HashRefInflator;
use DBIx::Class::ResultSet::RecursiveUpdate;
use List::Util 'pairmap';
use Scalar::Util ('blessed');

our $VERSION = '0.26';
Expand Down Expand Up @@ -259,6 +260,20 @@ sub _build_ru_flags {
{ unknown_params_ok => 1 };
}

has _accessor_aliases => (
is => 'ro',
isa => 'HashRef',
lazy_build => 1,
);

sub _build__accessor_aliases {
my $self = shift;
return {
pairmap { $b->{accessor} || $a => $a, $a => $a }
%{ $self->resultset->result_source->columns_info }
};
}

sub validate_model {
my ($self) = @_;
return unless $self->validate_unique;
Expand Down Expand Up @@ -468,8 +483,9 @@ sub validate_unique {
my $value = $field->value;
next unless defined $value;
my $accessor = $field->accessor;
my $column = $self->_accessor_aliases->{$accessor} || $accessor;

my $count = $rs->search( { $accessor => $value, @id_clause } )->count;
my $count = $rs->search( { $column => $value, @id_clause } )->count;
next if $count < 1;
my $field_error = $field->get_message('unique') || $field->unique_message || 'Duplicate value for [_1]';
$field->add_error( $field_error, $field->loc_label );
Expand Down
2 changes: 1 addition & 1 deletion t/db/bookdb.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
BEGIN TRANSACTION;
CREATE TABLE user (
user_id INTEGER PRIMARY KEY,
user_name VARCHAR(32),
un VARCHAR(32),
fav_cat VARCHAR(32),
fav_book VARCHAR(32),
occupation VARCHAR(32),
Expand Down
2 changes: 1 addition & 1 deletion t/lib/BookDB/Form/User.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use DateTime;

has '+item_class' => ( default => 'User');

has_field 'user_name';
has_field 'user_name' => ( unique => 1 );
has_field 'fav_cat' => ( label => 'Category' );
has_field 'fav_book' => ( label => 'Favorite Book' );
has_field 'occupation';
Expand Down
4 changes: 2 additions & 2 deletions t/lib/BookDB/Schema/Result/User.pm
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ __PACKAGE__->table("user");
__PACKAGE__->add_columns(
"user_id",
{ data_type => "INTEGER", is_nullable => 0, size => 8 },
"user_name",
{ data_type => "VARCHAR", is_nullable => 0, size => 32 },
"un",
{ accessor => "user_name", data_type => "VARCHAR", is_nullable => 0, size => 32 },
"fav_cat",
{ data_type => "VARCHAR", is_nullable => 0, size => 32 },
"fav_book",
Expand Down