root/lang/perl/FormValidator-Nested/lib/FormValidator/Nested/Validator/DBIC.pm @ 34906

Revision 34906, 1.1 kB (checked in by chiba, 4 years ago)

initial import FormValidator::Nested

Line 
1package FormValidator::Nested::Validator::DBIC;
2use strict;
3use warnings;
4use utf8;
5
6
7sub unique {
8    my ( $value, $options, $req, $param_name ) = @_;
9
10    return 0 if !defined $options->{resultset};
11
12    my %criteria = ();
13    if ( !$options->{criteria} ) {
14        # criteriaが指定されてない場合
15        %criteria = (
16            $param_name => $value,
17        );
18    }
19    else {
20        while ( my ($name, $check_value) = each %{$options->{criteria}} ) {
21            my $op = '=';
22            if ( ref $check_value eq 'HASH' ) {
23                # operand指定
24                ($op)          = keys   %{$check_value};
25                ($check_value) = values %{$check_value};
26            }
27            if ( $check_value =~ m/^`([^`]*)`$/ ) {
28                # eval
29                $check_value = eval($1); ## no critic
30           
31            }
32            elsif ( $check_value eq '__value__' ) {
33                $check_value = $value;
34            }
35            $criteria{$name} = {$op => $check_value};
36        }
37    }
38
39    my $count = $options->{resultset}->count(\%criteria);
40
41    return $count > 0 ? 0 : 1;
42}
43
44sub exist {
45    return !unique(@_);
46}
47
48
491;
Note: See TracBrowser for help on using the browser.