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