| 1 | package Dancer2::Plugin::FormValidator::Extension::DBIC::Exist; | ||
| 2 | |||
| 3 | use strict; use warnings; | ||
| 4 | |||
| 5 | use Moo; | ||
| 6 | use utf8; | ||
| 7 | use namespace::clean; | ||
| 8 | |||
| 9 | with 'Dancer2::Plugin::FormValidator::Role::Validator'; | ||
| 10 | |||
| 11 | sub message { | ||
| 12 | return { | ||
| 13 | en => '%s does not exists', | ||
| 14 | ru => '%s не существует', | ||
| 15 | de => '%s existiert nicht', | ||
| 16 | }; | ||
| 17 | } | ||
| 18 | |||
| 19 | sub validate { | ||
| 20 | my ($self, $field, $input, $source, $attribute) = @_; | ||
| 21 | |||
| 22 | if ($self->_field_defined_and_non_empty($field, $input)) { | ||
| 23 | return !!$self->extension->schema->resultset($source)->single( | ||
| 24 | { | ||
| 25 | $attribute => $input->{$field}, | ||
| 26 | } | ||
| 27 | ); | ||
| 28 | } | ||
| 29 | |||
| 30 | return 1; | ||
| 31 | } | ||
| 32 | |||
| 33 | 1; |