From 3ea3a6039063f35a5dffc726d5f0e9a1ab85ef11 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sven=20Sch=C3=B6ling?= Date: Wed, 8 Jan 2014 13:59:55 +0100 Subject: [PATCH] Kundennamenvalidierung: Keinen dberror werfen, sondern flash benutzen. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Ausserdem REs etwas aufgeräumt und ausgelagert --- SL/Controller/CustomerVendor.pm | 60 ++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 23 deletions(-) diff --git a/SL/Controller/CustomerVendor.pm b/SL/Controller/CustomerVendor.pm index e5cb556ac..dcb8b2d6c 100644 --- a/SL/Controller/CustomerVendor.pm +++ b/SL/Controller/CustomerVendor.pm @@ -6,6 +6,7 @@ use parent qw(SL::Controller::Base); use SL::JSON; use SL::DBUtils; use SL::Helper::Flash; +use SL::Locale::String; use SL::DB::Customer; use SL::DB::Vendor; @@ -28,7 +29,6 @@ __PACKAGE__->run_before( $::auth->assert('customer_vendor_edit'); } ); - __PACKAGE__->run_before( '_instantiate_args', only => [ @@ -62,6 +62,9 @@ __PACKAGE__->run_before( ] ); +__PACKAGE__->run_before('normalize_name'); + + sub action_add { my ($self) = @_; @@ -167,34 +170,34 @@ sub _save { sub action_save { my ($self) = @_; - my $pattern = '[ \t\n\r]+'; - my $name = $self->{cv}->name; - $name =~ s/^$pattern//; - $name =~ s/$pattern$//; - $name =~ s/$pattern/ /g; - if ( $name eq '' ) { - $::form->dberror( $::locale->text('Customer missing!') ); + if (!$self->{cv}->name) { + flash('error', t8('Customer missing!')); + $self->_pre_render(); + $self->render( + 'customer_vendor/form', + title => ($self->is_vendor() ? t8('Edit Vendor') : t8('Edit Customer')), + %{$self->{template_args}} + ); } else { - $self->{cv}->name($name); - } - $self->_save(); + $self->_save(); - my @redirect_params = ( - action => 'edit', - id => $self->{cv}->id, - db => ($self->is_vendor() ? 'vendor' : 'customer'), - ); + my @redirect_params = ( + action => 'edit', + id => $self->{cv}->id, + db => ($self->is_vendor() ? 'vendor' : 'customer'), + ); - if ( $self->{contact}->cp_id ) { - push(@redirect_params, contact_id => $self->{contact}->cp_id); - } + if ( $self->{contact}->cp_id ) { + push(@redirect_params, contact_id => $self->{contact}->cp_id); + } - if ( $self->{shipto}->shipto_id ) { - push(@redirect_params, shipto_id => $self->{shipto}->shipto_id); - } + if ( $self->{shipto}->shipto_id ) { + push(@redirect_params, shipto_id => $self->{shipto}->shipto_id); + } - $self->redirect_to(@redirect_params); + $self->redirect_to(@redirect_params); + } } sub action_save_and_close { @@ -859,4 +862,15 @@ sub _pre_render { $::request->{layout}->add_javascripts('kivi.CustomerVendor.js'); } +sub normalize_name { + my ($self) = @_; + + return unless $self->{cv}; + my $name = $self->{cv}->name; + $name =~ s/\s+$//; + $name =~ s/^\s+//; + $name =~ s/\s+/ /g; + $self->{cv}->name($name); +} + 1; -- 2.20.1