/usr/share/perl5/HTML/FormFu/Constraint/DBIC/Unique.pm is in libhtml-formfu-model-dbic-perl 1.00-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 | package HTML::FormFu::Constraint::DBIC::Unique;
{
$HTML::FormFu::Constraint::DBIC::Unique::VERSION = '1.00';
}
use Moose;
extends 'HTML::FormFu::Constraint';
use Carp qw( carp croak );
use HTML::FormFu::Util qw( DEBUG_CONSTRAINTS debug );
has model => ( is => 'rw', traits => ['Chained'] );
has resultset => ( is => 'rw', traits => ['Chained'] );
has column => ( is => 'rw', traits => ['Chained'] );
has method_name => ( is => 'rw', traits => ['Chained'] );
has self_stash_key => ( is => 'rw', traits => ['Chained'] );
has others => ( is => 'rw', traits => ['Chained'] );
has id_field => ( is => 'rw', traits => ['Chained'] );
sub constrain_value {
my ( $self, $value ) = @_;
return 1 if !defined $value || $value eq '';
for (qw/ resultset /) {
if ( !defined $self->$_ ) {
# warn and die, as errors are swallowed by HTML-FormFu
carp "'$_' is not defined";
croak "'$_' is not defined";
}
}
# get stash
my $stash = $self->form->stash;
my $schema;
if ( defined $stash->{schema} ) {
$schema = $stash->{schema};
}
elsif ( defined $stash->{context} && defined $self->model ) {
$schema = $stash->{context}->model( $self->model );
}
elsif ( defined $stash->{context} ) {
$schema = $stash->{context}->model;
}
if ( !defined $schema ) {
# warn and die, as errors are swallowed by HTML-FormFu
carp 'could not find DBIC schema';
croak 'could not find DBIC schema';
}
my $resultset = $schema->resultset( $self->resultset );
if ( !defined $resultset ) {
# warn and die, as errors are swallowed by HTML-FormFu
carp 'could not find DBIC resultset';
croak 'could not find DBIC resultset';
}
if ( my $method_name = $self->method_name ) {
# warn "using $method_name to look for $value";
# need to be able to tell $method_name about record on the form stash
my $pk_val;
if ( defined( my $self_stash_key = $self->self_stash_key ) ) {
if ( defined( my $self_stash = $stash->{ $self_stash_key } ) ) {
my ($pk) = $resultset->result_source->primary_columns;
$pk_val = $self_stash->$pk;
}
}
return $resultset->$method_name( $value, $pk_val );
}
else {
my $column = $self->column || $self->parent->name;
my %others;
if ( $self->others ) {
my @others = ref $self->others ? @{ $self->others }
: $self->others;
my $params = $self->form->input;
%others =
grep {
defined && length
}
map {
$_ => $self->get_nested_hash_value( $params, $_ )
} @others;
}
my $existing_row = eval {
$resultset->find( { %others, $column => $value } );
};
if ( my $error = $@ ) {
# warn and die, as errors are swallowed by HTML-FormFu
carp $error;
croak $error;
}
# if a row exists, first check whether it matches a known object on the
# form stash
if ( $existing_row && defined( my $self_stash_key = $self->self_stash_key ) ) {
if ( defined( my $self_stash = $stash->{ $self_stash_key } ) ) {
my ($pk) = $resultset->result_source->primary_columns;
if ( $existing_row->$pk eq $self_stash->$pk ) {
return 1;
}
}
}
elsif ( $existing_row && defined (my $id_field = $self->id_field ) ) {
my $value = $self->get_nested_hash_value( $self->form->input, $id_field );
if ( defined $value && length $value ) {
my ($pk) = $resultset->result_source->primary_columns;
return ($existing_row->$pk eq $value);
}
}
return !$existing_row;
}
}
after repeatable_repeat => sub {
my ( $self, $repeatable, $new_block ) = @_;
# rename any 'id_field' fields
if ( my $id_field = $self->id_field ) {
my $block_fields = $new_block->get_fields;
my $field = $repeatable->get_field_with_original_name( $id_field, $block_fields );
if ( defined $field ) {
DEBUG_CONSTRAINTS && debug(
sprintf "Repeatable renaming constraint 'id_field' '%s' to '%s'",
$id_field,
$field->nested_name,
);
$self->id_field( $field->nested_name );
}
}
};
1;
__END__
=head1 NAME
HTML::FormFu::Constraint::DBIC::Unique - unique constraint for HTML::FormFu::Model::DBIC
=head1 SYNOPSIS
$form->stash->{schema} = $dbic_schema; # DBIC schema
$form->element('text')
->name('email')
->constraint('DBIC::Unique')
->resultset('User')
;
$form->stash->{context} = $c; # Catalyst context
$form->element('text')
->name('email')
->constraint('DBIC::Unique')
->model('DBIC::User')
;
$form->element('text')
->name('user')
->constraint('DBIC::Unique')
->model('DBIC')
->resultset('User')
;
or in a config file:
---
elements:
- type: text
name: email
constraints:
- Required
- type: DBIC::Unique
model: DBIC::User
- type: text
name: user
constraints:
- Required
- type: DBIC::Unique
model: DBIC::User
column: username
=head1 DESCRIPTION
Checks if the input value exists in a DBIC ResultSet.
=head1 METHODS
=head2 model
Arguments: $string # a Catalyst model name like 'DBIC::User'
=head2 resultset
Arguments: $string # a DBIC resultset name like 'User'
=head2 self_stash_key
reference to a key in the form stash. if this key exists, the constraint
will check if the id matches the one of this element, so that you can
use your own name.
=head2 id_field
Use this key to define reference field which consist of primary key of
resultset. If the field exists (and $self_stash_key not defined), the
constraint will check if the id matches the primary key of row object:
---
elements:
- type: Hidden
name: id
constraints:
- Required
- type: Text
name: value
label: Value
constraints:
- Required
- type: DBIC::Unique
resultset: ControlledVocab
id_field: id
=head2 others
Use this key to manage unique compound database keys which consist of
more than one column. For example, if a database key consists of
'category' and 'value', use a config file such as this:
---
elements:
- type: Text
name: category
label: Category
constraints:
- Required
- type: Text
name: value
label: Value
constraints:
- Required
- type: DBIC::Unique
resultset: ControlledVocab
others: category
=head2 method_name
Name of a method which will be called on the resultset. The method is passed
two argument; the value of the field, and the primary key value (usually `id`)
of the record in the form stash (as defined by self_stash_key). An example
config might be:
---
elements:
- type: text
name: user
constraints:
- Required
- type: DBIC::Unique
model: DBIC::User
method_name: is_username_available
=head2 SEE ALSO
Is a sub-class of, and inherits methods from L<HTML::FormFu::Constraint>
L<HTML::FormFu::FormFu>
=head1 AUTHOR
Jonas Alves C<jgda@cpan.org>
=head1 LICENSE
This library is free software, you can redistribute it and/or modify it under
the same terms as Perl itself.
|