aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/Roles/ConfirmValidation.pm
blob: 27592b33c1f144af8005a59ad17c62d4066957fb (plain)
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
package FixMyStreet::Roles::ConfirmValidation;
use Moo::Role;

=head1 NAME

FixMyStreet::Roles::ConfirmValidation - role for adding standard confirm validation

=head1 SYNOPSIS

This is applied to a Cobrand class to add validation of reports using standard
Confirm field lengths.

    use Moo;
    with 'FixMyStreet::Roles::ConfirmValidation';

=cut

has max_report_length => ( is => 'ro', default => 2000 );

has max_title_length => ( is => 'ro', default => 0 );

sub report_validation {
    my ($self, $report, $errors) = @_;

    if ( length( $report->name ) > 50 ) {
        $errors->{name} = sprintf( _('Names are limited to %d characters in length.'), 50 );
    }

    if ( $report->user->phone && length( $report->user->phone ) > 20 ) {
        $errors->{phone} = sprintf( _('Phone numbers are limited to %s characters in length.'), 20 );
    }

    if ( $self->max_title_length > 0 && length( $report->title ) > $self->max_title_length ) {
        $errors->{title} = sprintf( _('Summaries are limited to %d characters in length. Please shorten your summary'), 50 );
    }

    if ( length( $report->detail ) > $self->max_report_length ) {
        $errors->{detail} = sprintf( _('Reports are limited to %s characters in length. Please shorten your report'), $self->max_report_length );
    }

    return $errors;
}

1;