Valiant/Errors.pm

filter: Statementstmt 83.0% 132 / 159 Branchbran 71.4% 30 / 42 Conditioncond 50.0% 16 / 32 MC/DCmcdc 25.0% 5 / 20 Subroutinesub 75.0% 36 / 48 Podpod 61.2% 19 / 31 totaltotal 71.6% 238 / 332 CC 42 SCAR 14.5
CC
42
Cov
76%
added
20.0
add
10.3
generate_message
6.0
CRAP 4.3
« Valiant/Validator/Unique.pm ↑ Summary Valiant/Validator/OnlyOf.pm »
1 package Valiant::Errors;
2  
3 45 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 45 ▶use Data::Perl::Collection::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 45 ▶use Valiant::NestedError;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 45 ▶use Valiant::Util 'throw_exception';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 use overload (
9 28 ▶ bool => sub { shift->size ? 1:0 },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
(shift())->size ? : 28 0 28
10 45 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 45 has 'object' => (
13 is => 'ro',
14 required => 1,
15 weak_ref => 1,
16 );
17  
18 has errors => (
19 is => 'ro',
20 init_arg => undef,
21 lazy => 1,
22 required => 1,
23 315 ▶ default => sub { Data::Perl::Collection::Array->new() },
Subroutine __ANON__: called CC=1 SCAR=0.0
24 45 handles => {
25 size => 'count',
26 count => 'count',
27 clear => 'clear',
28 blank => 'is_empty',
29 empty => 'is_empty',
30 }
31 );
32  
33 3 ▶sub i18n_class { 'Valiant::I18N' }
Subroutine i18n_class: called CC=1 SCAR=0.0
Pod undocumented
34  
35 has 'i18n' => (
36 is => 'ro',
37 required => 1,
38 3 ▶ default => sub { Module::Runtime::use_module(shift->i18n_class) },
Subroutine __ANON__: called CC=1 SCAR=0.0
39 45 );
40  
41 425 ▶sub error_class { 'Valiant::Error' }
Subroutine error_class: called CC=1 SCAR=0.0
Pod undocumented
42  
43 # return a flat list of all errors with duplicated removed. To we probably
44 # need to make use the the equals method on Valiant::Error
45 0 ▶sub uniq { die 'todo' }
Subroutine uniq: not called CC=1 SCAR=6.9
Pod undocumented
46  
47 sub any {
48 2 ▶ my ($self, $code) = @_;
Subroutine any: called CC=4 SCAR=14.5
Pod documented
49 2 ▶ $code ||= sub { $_ };
Subroutine __ANON__: not called CC=1 SCAR=2.0
Condition33%1/3
$code ||= sub {
l!l&&r!l&&!r
200
MC/DC0%0/2
$code ||= sub { $_; }
$code sub { $_; }
Truth table33%1/3
A ||= B
A= $codeB= sub { $_; }
ABresult
1X1
011
000
50 2 foreach my $error ($self->errors->all) {
51 10 local $_ = $error;
52 10 ▶ return 1 if $code->($error);
Branch: 1 branch
BranchTrueFalseTotal
if &$code($error) 1 9 10
53 }
54 1 return 0;
55 }
56  
57 sub copy {
58 1 ▶ my ($self, $other) = @_;
Subroutine copy: called CC=1 SCAR=0.0
Pod documented
59 my $errors = $other
60 ->errors
61 ->map(sub {
62 8 ▶ my $class = ref $_;
Subroutine __ANON__: called CC=1 SCAR=0.0
63 8 my $new = $class->new(
64 object => $self->object,
65 attribute => $_->attribute,
66 type => $_->type,
67 i18n => $_->i18n,
68 options => $_->options,
69 );
70 8 return $new;
71 1 });
72 1 $self->errors->clear;
73 1 $self->errors->push($errors->all);
74 }
75  
76 sub import_error {
77 99 ▶ my ($self, $error, $options) = @_;
Subroutine import_error: called CC=2 SCAR=6.9
Pod documented
78 $self->errors->push(
79 Valiant::NestedError->new(
80 inner_error => $error,
81 object => $error->object,
82 attribute => $error->attribute,
83 99 ▶ %{ $options||+{} },
Branch: 1 branch
BranchTrueFalseTotal
$options or {} 5 94 99
84 )
85 );
86 }
87  
88 sub merge {
89 1 ▶ my ($self, $other) = @_;
Subroutine merge: called CC=2 SCAR=6.9
Pod documented
90 1 foreach my $error ($other->errors->all) {
91 4 $self->import_error($error);
92 }
93 }
94  
95 sub where {
96 188 ▶ my $self = shift;
Subroutine where: called CC=1 SCAR=0.0
Pod documented
97 188 my ($attribute, $type, $options) = $self->_normalize_arguments(@_);
98 return $self->errors->grep(sub {
99 1615 ▶ $_->match($attribute, $type, $options);
Subroutine __ANON__: called CC=1 SCAR=0.0
100 188 })->all;
101 }
102  
103 sub _normalize_arguments {
104 616 ▶ my ($self, $attribute, $type, $options) = @_;
Subroutine _normalize_arguments: called CC=3 SCAR=12.0
105 616 ▶ if(ref($type) && ref($type) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $type and ref $type eq "CODE") 0 616 616
Condition66%2/3
ref $type and ref $type eq "CODE"
!ll&&!rl&&r
3163000
MC/DC0%0/2
ref $type and ref $type eq "CODE"
ref $type ref $type eq "CODE"
Truth table66%2/3
A and B
A= ref $typeB= ref $type eq "CODE"
ABresult
0X0
100
111
106 0 $type = $type->($self->object, $options);
107 }
108 return (
109 616 $attribute,
110 $type,
111 $options,
112 );
113 }
114  
115 # Returns +true+ if the error messages include an error for the given key
116 # +attribute+, +false+ otherwise.
117 sub include {
118 0 ▶ my ($self, $attribute) = @_;
Subroutine include: not called CC=1 SCAR=6.9
Pod documented
119 return scalar($self->any(sub {
120 0 ▶ $_->match($attribute);
Subroutine __ANON__: not called CC=1 SCAR=6.9
121 0 }));
122 }
123 45 *has_key = \&include;
124  
125 # Delete messages for +key+. Returns the deleted messages.
126 sub delete {
127 1 ▶ my $self = shift;
Subroutine delete: called CC=3 SCAR=11.0
Pod documented
128 1 my ($attribute, $type, $options) = $self->_normalize_arguments(@_);
129 1 my @deleted = ();
130 1 my $idx = 0;
131 1 foreach my $error($self->errors->all) {
132 8 ▶ if($error->match($attribute, $type, $options)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error->match($attribute, $type, $options)) { } 4 4 8
133 4 push @deleted, $self->errors->delete($idx);
134 } else {
135 4 $idx++
136 }
137 }
138 1 return @deleted;
139 }
140  
141 sub each {
142 17 ▶ my ($self, $block) = @_;
Subroutine each: called CC=3 SCAR=11.2
Pod documented
143 17 foreach my $error($self->errors->all) {
144 39 ▶ $block->((defined($error->attribute) ? $error->attribute : '*'), $error->message);
Branch: 1 branch
BranchTrueFalseTotal
defined $error->attribute ? : 39 0 39
145 }
146 }
147  
148 sub model_errors {
149 9 ▶ my $self = shift;
Subroutine model_errors: called CC=4 SCAR=13.9
Pod undocumented
150 9 my @errors;
151 9 foreach my $error($self->errors->all) {
152 138 ▶ push @errors, $error if !$error->has_attribute || !defined($error->attribute);
Branch: 1 branch
BranchTrueFalseTotal
unless $error->has_attribute and defined $error->attribute 16 122 138
Condition66%2/3
$error->has_attribute and defined $error->attribute
!ll&&!rl&&r
016122
MC/DC50%1/2
$error->has_attribute and defined $error->attribute
$error->has_attribute defined $error->attribute
Truth table66%2/3
A and B
A= $error->has_attributeB= defined $error->attribute
ABresult
0X0
100
111
153 }
154 9 return @errors;
155 }
156  
157 sub model_messages {
158 9 ▶ my ($self, $full_messages_flag) = @_;
Subroutine model_messages: called CC=2 SCAR=7.1
Pod documented
159 return map {
160 # AFAIK the full_messages_flag does nothing for model errors
161 9 ▶ $full_messages_flag ? $_->full_message : $_->message
Branch: 1 branch
BranchTrueFalseTotal
$full_messages_flag ? : 0 16 16
162 } $self->model_errors;
163 }
164  
165 sub attribute_errors {
166 4 ▶ my $self = shift;
Subroutine attribute_errors: called CC=4 SCAR=13.9
Pod undocumented
167 4 my @errors;
168 4 foreach my $error($self->errors->all) {
169 33 ▶ push @errors, $error if $error->has_attribute and defined($error->attribute);
Branch: 1 branch
BranchTrueFalseTotal
if $error->has_attribute and defined $error->attribute 29 4 33
Condition66%2/3
$error->has_attribute and defined $error->attribute
!ll&&!rl&&r
0429
MC/DC50%1/2
$error->has_attribute and defined $error->attribute
$error->has_attribute defined $error->attribute
Truth table66%2/3
A and B
A= $error->has_attributeB= defined $error->attribute
ABresult
0X0
100
111
170 }
171 4 return @errors;
172 }
173  
174 sub attribute_messages {
175 1 ▶ my ($self) = @_;
Subroutine attribute_messages: called CC=1 SCAR=0.0
Pod documented
176 return map {
177 1 $_->message;
178 } $self->attribute_errors;
179 }
180  
181 sub full_attribute_messages {
182 3 ▶ my ($self) = @_;
Subroutine full_attribute_messages: called CC=1 SCAR=0.0
Pod documented
183 return map {
184 3 $_->full_message;
185 } $self->attribute_errors;
186 }
187  
188  
189 sub group_by_attribute {
190 126 ▶ my $self = shift;
Subroutine group_by_attribute: called CC=4 SCAR=13.9
Pod undocumented
191 126 my %attributes;
192 126 foreach my $error($self->errors->all) {
193 411 ▶ next unless $error->has_attribute;
Branch: 1 branch
BranchTrueFalseTotal
unless $error->has_attribute 0 411 411
194 411 ▶ push @{$attributes{$error->attribute||'*'}}, $error;
Condition100%2/2
$error->attribute || '*'
l!l
39417
MC/DC100%1/1
$error->attribute || '*'
$error->attribute
Truth table100%2/2
A
A= $error->attribute
Aresult
11
00
195 }
196 126 return %attributes;
197 }
198  
199 # Returns a Hash of attributes with their error messages. If +full_messages+
200 # is +true+, it will contain full messages (see +full_message+).
201 sub to_hash {
202 126 ▶ my ($self, $full_messages_flag) = @_;
Subroutine to_hash: called CC=4 SCAR=13.9
Pod documented
203 126 my %hash = ();
204 126 my %grouped = $self->group_by_attribute;
205 126 foreach my $attr (keys %grouped) {
206 $hash{$attr} = [
207 map {
208 411 ▶ $full_messages_flag ? $_->full_message : $_->message
Branch: 1 branch
BranchTrueFalseTotal
$full_messages_flag ? : 319 92 411
209 312 ▶ } @{ $grouped{$attr}||[] }
Branch: 1 branch
BranchTrueFalseTotal
$grouped{$attr} or [] 0 312 312
210 ];
211 }
212 126 return %hash;
213 }
214  
215 sub as_json {
216 0 ▶ my ($self, %options) = @_;
Subroutine as_json: not called CC=1 SCAR=6.9
Pod undocumented
217 0 return $self->to_hash(exists $options{full_messages});
218 }
219  
220 0 ▶sub TO_JSON { shift->as_json(@_) }
Subroutine TO_JSON: not called CC=1 SCAR=6.9
Pod undocumented
221  
222 # Adds +message+ to the error messages and used validator type to +details+ on +attribute+.
223 # More than one error can be added to the same +attribute+.
224 sub add {
225 425 ▶ my ($self, $attribute, $type, $options) = @_;
Subroutine add: called CC=10 SCAR=23.4
Pod documented
226 425 ▶ unless(defined($type)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 1 424 425
227 1 $type = $self->i18n->make_tag('invalid');
228 }
229 425 ▶ $options ||= +{};
Condition100%2/2
$options ||= {}
l!l
32897
MC/DC100%1/1
$options ||= {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
230 425 ($attribute, $type, $options) = $self->_normalize_arguments($attribute, $type, $options);
231 425 my $error = $self->build_error($attribute, $type, $options);
232 425 ▶ if(my $exception = $options->{strict}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $exception = $options->{"strict"}) 3 422 425
233 3 my $message = $error->full_message;
234 3 ▶ throw_exception('Strict' => (msg=>$message)) if $exception =~m/^\d+$/ && $exception == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $exception =~ /^\d+$/ and $exception == 1 1 2 3
Condition66%2/3
$exception =~ /^\d+$/ and $exception == 1
!ll&&!rl&&r
201
MC/DC50%1/2
$exception =~ /^\d+$/ and $exception == 1
$exception =~ /^\d+$/ $exception == 1
Truth table66%2/3
A and B
A= $exception =~ /^\d+$/B= $exception == 1
ABresult
0X0
100
111
235 2 ▶ throw_exception('Strict' => (msg=>$exception)) if( (ref(\$exception)||'') eq 'SCALAR');
Branch: 1 branch
BranchTrueFalseTotal
if (ref \$exception || "") eq "SCALAR" 1 1 2
Condition50%1/2
ref \$exception || ''
l!l
20
MC/DC0%0/1
ref \$exception || ''
ref \$exception
Truth table50%1/2
A
A= ref \$exception
Aresult
11
00
236 1 ▶ $exception->($self->object, $message) if( (ref($exception)||'') eq 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if (ref $exception || "") eq "CODE" 1 0 1
Condition50%1/2
ref $exception || ''
l!l
10
MC/DC0%0/1
ref $exception || ''
ref $exception
Truth table50%1/2
A
A= ref $exception
Aresult
11
00
237 0 $exception->throw($message); # If not 1 then assume its a package name or exception object.
238 }
239
240 422 $self->errors->push($error);
241 422 return $error;
242 }
243  
244 sub build_error {
245 425 ▶ my ($self, $attribute, $type, $options) = @_;
Subroutine build_error: called CC=1 SCAR=0.0
Pod undocumented
246 425 return my $error = $self->error_class
247 ->new(
248 object => $self->object,
249 attribute => $attribute,
250 type => $type,
251 i18n => $self->i18n,
252 options => $options,
253 );
254 }
255  
256 # Returns +true+ if an error on the attribute with the given message is
257 # present, or +false+ otherwise. +message+ is treated the same as for +add+. ~
258 sub added {
259 0 ▶ my ($self, $attribute, $type, $options) = @_;
Subroutine added: not called CC=4 SCAR=30.0
Pod documented
260  
261 ## TODO ok so if the $attribute refers to an object which can->errors maybe we
262 ## need to call $self->$attribute->errors->add(undef, $type, $options) instead
263 ## so that any global errors to a nested object end in in the right place?
264 ## Afterwards we need to associate the nested object errors to $self so that
265 ## we know errors exist (for stuff like to_hash and all_errors, etc.
266  
267 0 ▶ $type ||= $self->i18n->make_tag('invalid');
Condition0%0/3
$type ||= $self->i18n->make_tag("invalid")
l!l&&r!l&&!r
000
MC/DC0%0/2
$type ||= $self->i18n->make_tag("invalid")
$type $self->i18n->make_tag("invalid")
Truth table0%0/3
A ||= B
A= $typeB= $self->i18n->make_tag("invalid")
ABresult
1X1
011
000
268 0 ($attribute, $type, $options) = $self->_normalize_arguments($attribute, $type, $options);
269 0 ▶ if($self->i18n->is_i18n_tag($type)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->i18n->is_i18n_tag($type)) { } 0 0 0
270 return $self->any(sub {
271 0 ▶ $_->strict_match($attribute, $type, $options);
Subroutine __ANON__: not called CC=1 SCAR=6.9
272 0 });
273 } else {
274 return scalar(grep {
275 0 ▶ $_ eq $type;
Branch: 1 branch
BranchTrueFalseTotal
scalar grep({$_ eq $type;} $self->messages_for($attribute)) ? : 0 0 0
276 } $self->messages_for($attribute)) ? 1:0
277 }
278 }
279  
280 # Similar to ->added except we don't care about options
281 sub of_kind {
282 2 ▶ my ($self, $attribute, $type) = @_;
Subroutine of_kind: called CC=4 SCAR=15.2
Pod documented
283 2 ▶ $type ||= $self->i18n->make_tag('invalid');
Condition33%1/3
$type ||= $self->i18n->make_tag("invalid")
l!l&&r!l&&!r
200
MC/DC0%0/2
$type ||= $self->i18n->make_tag("invalid")
$type $self->i18n->make_tag("invalid")
Truth table33%1/3
A ||= B
A= $typeB= $self->i18n->make_tag("invalid")
ABresult
1X1
011
000
284 2 ($attribute, $type) = $self->_normalize_arguments($attribute, $type);
285 2 ▶ if($self->i18n->is_i18n_tag($type)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->i18n->is_i18n_tag($type)) { } 0 2 2
286 return $self->any(sub {
287 0 ▶ $_->strict_match($attribute, $type);
Subroutine __ANON__: not called CC=1 SCAR=6.9
288 0 });
289 } else {
290 return scalar(grep {
291 2 ▶ $_ eq $type;
Branch: 1 branch
BranchTrueFalseTotal
scalar grep({$_ eq $type;} $self->messages_for($attribute)) ? : 1 1 2
292 } $self->messages_for($attribute)) ? 1:0
293 }
294 }
295  
296 1 ▶sub messages { map { $_->message } shift->errors->all }
Subroutine messages: called CC=1 SCAR=0.0
Pod documented
297
298 sub full_messages {
299 3 ▶ my $self = shift;
Subroutine full_messages: called CC=1 SCAR=0.0
Pod documented
300 3 $self->full_messages_collection->all;
301 }
302  
303 sub full_messages_collection {
304 3 ▶ my $self = shift;
Subroutine full_messages_collection: called CC=1 SCAR=0.0
Pod undocumented
305 3 ▶ return $self->errors->map(sub { $_->full_message });
Subroutine __ANON__: called CC=1 SCAR=0.0
306 }
307  
308  
309 sub full_messages_for {
310 20 ▶ my ($self, $attribute) = @_;
Subroutine full_messages_for: called CC=1 SCAR=0.0
Pod documented
311 return map {
312 20 $_->full_message
313 } $self->where($attribute);
314 }
315  
316 sub messages_for {
317 4 ▶ my ($self, $attribute) = @_;
Subroutine messages_for: called CC=1 SCAR=0.0
Pod documented
318 return map {
319 4 $_->message
320 } $self->where($attribute)
321 }
322  
323 sub full_message {
324 0 ▶ my ($self, $attribute, $message) = @_;
Subroutine full_message: not called CC=1 SCAR=6.9
Pod undocumented
325 0 $self->error_class->full_message(
326 $attribute,
327 $message,
328 $self->object,
329 $self->i18n);
330 }
331  
332 sub generate_message {
333 0 ▶ my ($self, $attribute, $type, $options) = @_;
Subroutine generate_message: not called CC=2 SCAR=17.9
Pod undocumented
334 0 ▶ $type ||= $self->i18n->make_tag('invalid');
Condition0%0/3
$type ||= $self->i18n->make_tag("invalid")
l!l&&r!l&&!r
000
MC/DC0%0/2
$type ||= $self->i18n->make_tag("invalid")
$type $self->i18n->make_tag("invalid")
Truth table0%0/3
A ||= B
A= $typeB= $self->i18n->make_tag("invalid")
ABresult
1X1
011
000
335 0 return $self->error_class->generate_message(
336 $attribute,
337 $type,
338 $self->object,
339 $options,
340 $self->i18n);
341 }
342  
343 sub _dump {
344 0 ▶ require Data::Dumper;
Subroutine _dump: not called CC=1 SCAR=6.9
345 0 return Data::Dumper::Dumper( +{shift->to_hash(full_messages=>1)} );
346 }
347  
348 45 1;
349  
350 =head1 NAME
351  
352 Valiant::Errors - A collection of errors associated with an object
353  
354 =head1 SYNOPSIS
355  
356 =head1 DESCRIPTION
357  
358 A collection of errors (each instances of L<Valiant::Error>) associated with attributes
359 or a model. This class provides methods for adding, retrieving and introspecting
360 error, typically via a L<Valiant::Validator> or L<Valiant::Validator::Each> subclass.
361  
362 The goal of this class is to make it as easy as possible to work with and understand
363 errors that have been added to your instance. In general you will never make an instance
364 of this directly since it will be used via the L<Valiant::Validates> role.
365  
366 =head1 ATTRIBUTES
367  
368 This class defined the following attributes
369  
370 =head2 object
371  
372 This is a weak reference to the object which the errors belong to.
373  
374 =head2 errors
375  
376 This is an instance of L<Data::Perl::Collection::Array> which in a collection of
377 L<Valiant::Error> objects added by validators.
378  
379 =head2 i18n
380  
381 The internationalization and translation class. Generally this is an instance of
382 L<Valiant::I18N>. You won't need to supply this as it normally is built automatically.
383  
384 =head1 METHODS
385  
386 The class defines the following methods
387  
388 =head2 count
389  
390 =head2 size
391  
392 The number of errors collected. If there are no errors then the size is 0.
393  
394 =head2 empty
395  
396 =head2 blank
397  
398 Returns true if there are no errors collected.
399  
400 =head2 any(\&code)
401  
402 Accepts a coderef that will receive each error object in the collect and return true
403 if any of the coderef calls return true. Used to determine if the errors collection
404 contains at least one type of error.
405  
406 my $has_invalids = $user1->errors->any(sub {
407 ${\$_->type} eq 'invalid';
408 });
409  
410 =head2 copy
411  
412 Copy an errors collection into the current (replacing any existing). The copies are
413 new instances of L<Valiant::Error>, not references to the original objects.
414  
415 =head2 import_error ($error)
416  
417 Given a single L<Valiant::Error> inport it into the current errors collection
418  
419 =head2 merge ($collectio)
420  
421 Given a L<Valiant::Errors> collection, merge it into the current one.
422  
423 =head2 where ($attribute, $message, \%options)
424  
425 return all the L<Valiant::Error> objects in the current collection which match criteria.
426  
427 =head2 include ($attribute)
428  
429 Returns +true+ if the error messages include an error for the given key
430 +attribute+, +false+ otherwise.
431  
432 =head2 delete ($attribute)
433  
434 Delete messages for +key+. Returns the deleted messages.
435  
436 =head2 each ($coderef)
437  
438 Iterates through each error key, value pair in the error messages hash.
439 Yields the attribute and the error for that attribute. If the attribute
440 has more than one error message, yields once for each error message.
441  
442 $object->errors->each(sub {
443 my ($attribute, $message) = @_;
444 });
445  
446 If the error is a model error then C<$attribute> will be '*'.
447  
448 =head2 model_messages
449  
450 Returns an array of all the errors that are associated with the model (Localized
451 if needed).
452  
453 =head2 attribute_messages
454  
455 Returns an array of all the errors that are associated with attributes (localized if needed).
456  
457 =head2 full_attribute_messages
458  
459 Returns an array of the full messages of all attributes (localized if needed).
460  
461 =head2 to_hash (?$flag)
462  
463 Returns a hash where each key is an attribute (or '*' for the model) and
464 each value is an arrayref of errors. C<?$flag> when true will return
465 the full messages for each error.
466  
467 =head2 add ($attribute|undef, $message, \%opts)
468  
469 Add a new error message to the object. Error can be associated with an attribute
470 or with the object itself. C<$message> can be one of:
471  
472 =over 14
473  
474 =item A string
475  
476 If a string this is the error message recorded.
477  
478 validates name => (
479 format => 'alphabetic',
480 length => [3,20],
481 message => 'Unacceptable Name',
482 );
483  
484 =item A translation tag
485  
486 use Valiant::I18N;
487  
488 validates name => (
489 format => 'alphabetic',
490 length => [3,20],
491 message => _t('bad_name'),
492 );
493  
494 This will look up a translated version of the tag. See L<Valiant::I18N> for more details.
495  
496 =item A scalar reference
497  
498 validates name => (
499 format => 'alphabetic',
500 length => [3,20],
501 message => \'Unacceptable {{attribute}}',
502 );
503  
504 Similar to string but we will expand any placeholder variables which are indicated by the
505 '{{' and '}}' tokens (which are removed from the final string). You can use any placeholder
506 that is a key in the options hash (and you can pass additional values when you add an error).
507 By default the following placeholder expansions are available attribute (the attribute name),
508 value (the current attribute value), model (the human name of the model containing the
509 attribute and object (the actual object instance that has the error).
510  
511 =item A subroutine reference
512  
513 validates name => (
514 format => 'alphabetic',
515 length => [3,20],
516 message => sub {
517 my ($self, $attr, $value, $opts) = @_;
518 return "Unacceptable $attr!";
519 }
520 );
521  
522 Similar to the scalar reference option just more flexible since you can write custom code
523 to build the error message. For example you could return different error messages based on
524 the identity of a person. Also if you return a translation tag instead of a simple string
525 we will attempt to resolve it to a translated string (see L<Valiant::I18N>).
526  
527 =back
528  
529 =head2 added ($attribute|undef, $message, \%opts)
530  
531 Return true if the error has already been created.
532  
533 =head2 of_kind ($attribute|undef, $message)
534  
535 Similar to <added> expect we don't need to match options
536  
537 =head2 messages
538  
539 An array of the error messages. All translation tags will be translated to strings in the
540 expected local langauge.
541  
542 =head2 full_messages
543  
544 An array of the full messages (localized if needed).
545  
546 =head2 messages_for ($attribute)
547  
548 An array of all the messages for the given attribute (localized if needed).
549  
550 =head2 full_messages_for ($attribute)
551  
552 An array of all the full messages for the given attribute (localized if needed).
553  
554 =head1 JSONification
555  
556 This class provides a C<TO_JSON> method suitable for use in some of the common
557 JSON serializers. When supported it will delegate the job of turning the object
558 into a hash that can be serialized to JSON to the C<to_hash> method.
559  
560 =head1 SEE ALSO
561
562 L<Valiant>, L<Valiant::Error>.
563  
564 =head1 AUTHOR
565
566 See L<Valiant>
567
568 =head1 COPYRIGHT & LICENSE
569
570 See L<Valiant>
571  
572 =cut
573  
574 45 1;
« Valiant/Validator/Unique.pm ↑ Summary Valiant/Validator/OnlyOf.pm »