diff options
author | Dave Arter <davea@mysociety.org> | 2016-07-14 15:46:54 +0100 |
---|---|---|
committer | Dave Arter <davea@mysociety.org> | 2016-07-19 18:10:04 +0100 |
commit | 54a3cabdd73020c3516caeae1cbb9689224e9698 (patch) | |
tree | 47e56bec11fb10c3cdf46f7c248fb23cfeaec3f9 | |
parent | def23b4c2ed67e3920489d07cf04689117274fc8 (diff) |
Disallow empty name when creating/editing bodies in admin
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Admin.pm | 49 | ||||
-rw-r--r-- | templates/web/base/admin/bodies.html | 4 | ||||
-rw-r--r-- | templates/web/base/admin/body-form.html | 6 | ||||
-rw-r--r-- | templates/web/base/admin/body.html | 4 |
4 files changed, 46 insertions, 17 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Admin.pm b/perllib/FixMyStreet/App/Controller/Admin.pm index 776a9276a..0da4a781a 100644 --- a/perllib/FixMyStreet/App/Controller/Admin.pm +++ b/perllib/FixMyStreet/App/Controller/Admin.pm @@ -244,13 +244,15 @@ sub bodies : Path('bodies') : Args(0) { $c->forward('/auth/check_csrf_token'); my $params = $c->forward('body_params'); - my $body = $c->model('DB::Body')->create( $params ); - my @area_ids = $c->get_param_list('area_ids'); - foreach (@area_ids) { - $c->model('DB::BodyArea')->create( { body => $body, area_id => $_ } ); - } + unless ( keys $c->stash->{body_errors} ) { + my $body = $c->model('DB::Body')->create( $params ); + my @area_ids = $c->get_param_list('area_ids'); + foreach (@area_ids) { + $c->model('DB::BodyArea')->create( { body => $body, area_id => $_ } ); + } - $c->stash->{updated} = _('New body added'); + $c->stash->{updated} = _('New body added'); + } } $c->forward( 'fetch_all_bodies' ); @@ -410,18 +412,20 @@ sub update_contacts : Private { $c->forward('/auth/check_csrf_token'); my $params = $c->forward( 'body_params' ); - $c->stash->{body}->update( $params ); - my @current = $c->stash->{body}->body_areas->all; - my %current = map { $_->area_id => 1 } @current; - my @area_ids = $c->get_param_list('area_ids'); - foreach (@area_ids) { - $c->model('DB::BodyArea')->find_or_create( { body => $c->stash->{body}, area_id => $_ } ); - delete $current{$_}; - } - # Remove any others - $c->stash->{body}->body_areas->search( { area_id => [ keys %current ] } )->delete; + unless ( keys $c->stash->{body_errors} ) { + $c->stash->{body}->update( $params ); + my @current = $c->stash->{body}->body_areas->all; + my %current = map { $_->area_id => 1 } @current; + my @area_ids = $c->get_param_list('area_ids'); + foreach (@area_ids) { + $c->model('DB::BodyArea')->find_or_create( { body => $c->stash->{body}, area_id => $_ } ); + delete $current{$_}; + } + # Remove any others + $c->stash->{body}->body_areas->search( { area_id => [ keys %current ] } )->delete; - $c->stash->{updated} = _('Values updated'); + $c->stash->{updated} = _('Values updated'); + } } } @@ -440,9 +444,20 @@ sub body_params : Private { deleted => 0, ); my %params = map { $_ => $c->get_param($_) || $defaults{$_} } keys %defaults; + $c->forward('check_body_params', [ \%params ]); return \%params; } +sub check_body_params : Private { + my ( $self, $c, $params ) = @_; + + $c->stash->{body_errors} ||= {}; + + unless ($params->{name}) { + $c->stash->{body_errors}->{name} = _('Please enter a name for this body'); + } +} + sub display_contacts : Private { my ( $self, $c ) = @_; diff --git a/templates/web/base/admin/bodies.html b/templates/web/base/admin/bodies.html index 4b4fc83ef..e98e2d350 100644 --- a/templates/web/base/admin/bodies.html +++ b/templates/web/base/admin/bodies.html @@ -1,5 +1,9 @@ [% INCLUDE 'admin/header.html' title=loc('Bodies') -%] +[% IF body_errors.size %] + <p class="error">[% loc('Please correct the errors below') %]</p> +[% END %] + [% IF bodies.size == 0 %] <p class="fms-admin-info"> [% loc('Currently no bodies have been created.') %] diff --git a/templates/web/base/admin/body-form.html b/templates/web/base/admin/body-form.html index 8c4956f7f..13e688097 100644 --- a/templates/web/base/admin/body-form.html +++ b/templates/web/base/admin/body-form.html @@ -6,6 +6,12 @@ categories of problem) to each body." ) %] </div> + + [% IF body_errors.name %] + <div class="fms-admin-warning"> + [% body_errors.name %] + </div> + [% END %] <div class="admin-hint"> <p> [% loc( diff --git a/templates/web/base/admin/body.html b/templates/web/base/admin/body.html index 99b7249ce..a00c5ca4c 100644 --- a/templates/web/base/admin/body.html +++ b/templates/web/base/admin/body.html @@ -7,6 +7,10 @@ </p> [% END %] +[% IF body_errors.size %] + <p class="error">[% loc('Please correct the errors below') %]</p> +[% END %] + [% IF NOT errors %] <p> |