diff options
author | louise <louise> | 2009-11-09 13:58:35 +0000 |
---|---|---|
committer | louise <louise> | 2009-11-09 13:58:35 +0000 |
commit | 98049e41c1648a9ca8110798f50a79ef5cd99345 (patch) | |
tree | 64e36091411a77de9e12683768f31476fba26d27 | |
parent | e30e663f3eef7c6c89ab8566efb7224ebe3fbd6c (diff) |
Adding field-specific validation messages when submitting an update
-rw-r--r-- | templates/website/problem | 12 | ||||
-rwxr-xr-x | web/index.cgi | 21 |
2 files changed, 23 insertions, 10 deletions
diff --git a/templates/website/problem b/templates/website/problem index cd1dfe13f..53d899fac 100644 --- a/templates/website/problem +++ b/templates/website/problem @@ -38,9 +38,17 @@ <input type="hidden" name="id" value="{{ $input_h{id} }}"> <div><label for="form_name">{{ $name_label }}</label> <input type="text" name="name" id="form_name" value="{{ $input_h{name} }}" size="20"> (optional)</div> -<div><label for="form_rznvy">{{ $email_label }}</label> +<div> +{{ if ( $field_errors{email}) { + "<div class='form-error'>$field_errors{email}</div>"; +} }} +<label for="form_rznvy">{{ $email_label }}</label> <input type="text" name="rznvy" id="form_rznvy" value="{{ $input_h{rznvy} }}" size="20"></div> -<div><label for="form_update">{{ $update_label }}</label> +<div> +{{ if ( $field_errors{update}) { + "<div class='form-error'>$field_errors{update}</div>"; +} }} +<label for="form_update">{{ $update_label }}</label> <textarea name="update" id="form_update" rows="7" cols="30">{{ $input_h{update} }}</textarea></div> <div class="checkbox">{{ $fixedline_box }} {{ $fixedline_label }}</div> {{ $photo_element }} diff --git a/web/index.cgi b/web/index.cgi index 8cb376d31..0e5cbc930 100755 --- a/web/index.cgi +++ b/web/index.cgi @@ -6,7 +6,7 @@ # Copyright (c) 2006 UK Citizens Online Democracy. All rights reserved. # Email: matthew@mysociety.org. WWW: http://www.mysociety.org # -# $Id: index.cgi,v 1.313 2009-11-09 11:00:25 louise Exp $ +# $Id: index.cgi,v 1.314 2009-11-09 13:58:35 louise Exp $ use strict; use Standard; @@ -79,7 +79,7 @@ sub main { ($out, %params) = display_form($q, [], {}); $params{title} = _('Reporting a problem'); } elsif ($q->param('id')) { - ($out, %params) = display_problem($q); + ($out, %params) = display_problem($q, [], {}); $params{title} .= ' - ' . _('Viewing a problem'); } elsif ($q->param('pc') || ($q->param('x') && $q->param('y'))) { ($out, %params) = display_location($q); @@ -202,19 +202,19 @@ sub submit_update { my @vars = qw(id name rznvy update fixed upload_fileid add_alert); my %input = map { $_ => $q->param($_) || '' } @vars; my @errors; + my %field_errors; my $fh = $q->upload('photo'); if ($fh) { my $err = Page::check_photo($q, $fh); push @errors, $err if $err; } - - push(@errors, _('Please enter a message')) unless $input{update} =~ /\S/; + $field_errors{update} = _('Please enter a message') unless $input{update} =~ /\S/; $input{name} = undef unless $input{name} =~ /\S/; if ($input{rznvy} !~ /\S/) { - push(@errors, _('Please enter your email')); + $field_errors{email} = _('Please enter your email'); } elsif (!mySociety::EmailUtil::is_valid_email($input{rznvy})) { - push(@errors, _('Please enter a valid email')); + $field_errors{email} = _('Please enter a valid email'); } my $image; @@ -233,7 +233,7 @@ sub submit_update { close FP; } - return display_problem($q, @errors) if (@errors); + return display_problem($q, \@errors, \%field_errors) if (@errors || scalar(keys(%field_errors))); my $cobrand = Page::get_cobrand($q); my $cobrand_data = Cobrand::extra_update_data($cobrand, $q); my $id = dbh()->selectrow_array("select nextval('comment_id_seq');"); @@ -897,7 +897,10 @@ sub display_location { } sub display_problem { - my ($q, @errors) = @_; + my ($q, $errors, $field_errors) = @_; + my @errors = @$errors; + my %field_errors = %{$field_errors}; + push @errors, _('There were problems with your update. Please see below.') if (scalar keys %field_errors); my @vars = qw(id name rznvy update fixed add_alert upload_fileid x y submit_update); my %input = map { $_ => $q->param($_) || '' } @vars; @@ -984,6 +987,8 @@ sub display_problem { if (@errors) { $vars{errors} = '<ul class="error"><li>' . join('</li><li>', @errors) . '</li></ul>'; } + + $vars{field_errors} = \%field_errors; my $fixed = ($input{fixed}) ? ' checked' : ''; $vars{add_alert_checked} = ($input{add_alert} || !$input{submit_update}) ? ' checked' : ''; |