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
|
package FormValidator::Simple::Constraint;
use strict;
use base qw/Class::Accessor::Fast/;
use FormValidator::Simple::Exception;
use FormValidator::Simple::Validator;
use FormValidator::Simple::Constants;
__PACKAGE__->mk_accessors(qw/name command negative args/);
sub new {
my $class = shift;
my $self = bless { }, $class;
$self->_init(@_);
return $self;
}
sub _init {
my ($self, $setting) = @_;
if (ref $setting) {
my($name, @args) = @$setting;
$self->name($name);
$self->args( [@args] );
} else {
$self->name($setting);
$self->args( [] );
}
$self->_check_name;
}
sub _check_name {
my $self = shift;
my $name = $self->name;
if($name =~ /^NOT_(.+)$/) {
$self->command($1);
$self->negative( TRUE );
} else {
$self->command($name);
$self->negative( FALSE );
}
}
sub check {
my ($self, $params) = @_;
my $command = $self->command;
FormValidator::Simple::Exception->throw(
qq/Unknown validation "$command"./
) unless FormValidator::Simple::Validator->can($command);
my ($result, $data) = FormValidator::Simple::Validator->$command($params, $self->args);
$result = not $result if $self->negative;
return ($result, $data);
}
1;
__END__
|