package Error;
use HTML::Template;
use strict;
sub new {
my ($class,$file) = @_;
my $self = {
ERRORS => undef, # this key is a list of hashes
file => $file,
};
bless $self, $class;
return $self;
}
sub add_error {
my ($self,$field,$reason) = @_;
push @{$self->{ERRORS}}, {
FIELD => $field,
REASON => $reason,
};
}
sub generate_errors {
my ($self,$file) = @_;
return unless ¬_empty;
my $template = HTML::Template->new(filename => $self->{file});
$template->param(
IS_SINGULAR => (scalar @{$self->{ERRORS}} == 1),
ERRORS => $self->{ERRORS},
);
@{$self->{ERRORS}} = ();
return $template->output;
}
sub not_empty {
my $self = shift;
return ($self->{ERRORS}) ? (scalar @{$self->{ERRORS}} > 0) : 0;
}
1;