@@ -27,34 +27,37 @@ public function readResponse(
2727 $ entity = \App::$ http ->readGetResult ('/department/ ' . $ entityId . '/ ' , ['resolveReferences ' => 1 ])->getEntity ();
2828 $ organisation = \App::$ http ->readGetResult ('/department/ ' . $ entityId . '/organisation/ ' )->getEntity ();
2929 $ input = $ request ->getParsedBody ();
30+ $ result = null ;
3031
3132 if ($ request ->getMethod () === 'POST ' ) {
3233 $ input = $ this ->withCleanupLinks ($ input );
3334 $ input = $ this ->withCleanupDayoffs ($ input );
3435 $ input = $ this ->withEmailReminderDefaultValues ($ input );
35- $ entity = (new Entity ($ input ))->withCleanedUpFormData ();
36- $ entity ->id = $ entityId ;
37- $ entity ->dayoff = $ entity ->getDayoffList ()->withTimestampFromDateformat ();
38- $ entity = \App::$ http ->readPostResult (
39- '/department/ ' . $ entity ->id . '/ ' ,
40- $ entity
41- )->getEntity ();
42- return \BO \Slim \Render::redirect ('department ' , ['id ' => $ entityId ], [
43- 'success ' => 'department_saved '
44- ]);
36+ $ result = $ this ->writeUpdatedEntity ($ input , $ entityId );
37+ if ($ result instanceof Entity) {
38+ return \BO \Slim \Render::redirect ('department ' , ['id ' => $ entityId ], [
39+ 'success ' => 'department_saved '
40+ ]);
41+ }
4542 }
4643
44+ // If there was an error, use the submitted input data for form re-population
45+ $ departmentData = (isset ($ result ) && is_array ($ result ) && isset ($ result ['data ' ]))
46+ ? array_merge ((new Schema ($ entity ))->toSanitizedArray (), $ input ?? [])
47+ : (new Schema ($ entity ))->toSanitizedArray ();
48+
4749 return \BO \Slim \Render::withHtml (
4850 $ response ,
4951 'page/department.twig ' ,
5052 array (
5153 'title ' => 'Standort ' ,
5254 'workstation ' => $ workstation ,
5355 'organisation ' => $ organisation ,
54- 'department ' => ( new Schema ( $ entity ))-> toSanitizedArray () ,
56+ 'department ' => $ departmentData ,
5557 'hasAccess ' => $ entity ->hasAccess ($ workstation ->getUseraccount ()),
5658 'menuActive ' => 'owner ' ,
5759 'success ' => $ success ,
60+ 'exception ' => (isset ($ result ) && !($ result instanceof Entity)) ? $ result : null ,
5861 )
5962 );
6063 }
@@ -102,4 +105,17 @@ private function withEmailReminderDefaultValues(array $input)
102105
103106 return $ input ;
104107 }
108+
109+ protected function writeUpdatedEntity ($ input , $ entityId )
110+ {
111+ $ entity = (new Entity ($ input ))->withCleanedUpFormData ();
112+ $ entity ->id = $ entityId ;
113+ $ entity ->dayoff = $ entity ->getDayoffList ()->withTimestampFromDateformat ();
114+ return $ this ->handleEntityWrite (function () use ($ entity ) {
115+ return \App::$ http ->readPostResult (
116+ '/department/ ' . $ entity ->id . '/ ' ,
117+ $ entity
118+ )->getEntity ();
119+ });
120+ }
105121}
0 commit comments