]> git.agnieray.net Git - galette.git/commitdiff
Add missing validations on entities add/edit
authorGuillaume AGNIERAY <dev@agnieray.net>
Sat, 11 Mar 2023 11:07:54 +0000 (12:07 +0100)
committerJohan Cwiklinski <johan@x-tnd.be>
Sat, 11 Mar 2023 15:10:10 +0000 (16:10 +0100)
Check for empty values when required

galette/lib/Galette/Controllers/Crud/EntitledsController.php
galette/lib/Galette/Controllers/Crud/PaymentTypeController.php
galette/lib/Galette/Controllers/Crud/TitlesController.php

index 5d9727e415f1a58398dbafc6e9077dfec8648094..a47cc93d10bd67690e43de925a2421819b713d02 100644 (file)
@@ -253,6 +253,9 @@ class EntitledsController extends CrudController
     ): Response {
         $post = $request->getParsedBody();
 
+        $error_detected = [];
+        $msg = null;
+
         switch ($class) {
             case 'status':
                 $entitled = new Status($this->zdb);
@@ -267,36 +270,50 @@ class EntitledsController extends CrudController
         $label = trim($post[$entitled::$fields['libelle']]);
         $field = (int)trim($post[$entitled::$fields['third']] ?? 0);
 
-        $ret = ($action === 'add' ? $entitled->add($label, $field) : $entitled->update($id, $label, $field));
+        if ($label != '') {
+            $ret = ($action === 'add' ? $entitled->add($label, $field) : $entitled->update($id, $label, $field));
+        } else {
+            $ret = false;
+            $error_detected[] = _T('Missing required %type name!');
+        }
+        $redirect_uri = $this->routeparser->urlFor('entitleds', ['class' => $class]);
 
         if ($ret !== true) {
-            $msg_type = 'error_detected';
-            $msg = $action === 'add' ?
+            $error_detected[] = $action === 'add' ?
                 _T("%type has not been added :(") : _T("%type #%id has not been updated");
+            if ($action === 'edit') {
+                $redirect_uri = $this->routeparser->urlFor('editEntitled', ['id' => $id, 'class' => $class]);
+            }
         } else {
-            $msg_type = 'success_detected';
             $msg = $action === 'add' ?
                 _T("%type has been successfully added!") : _T("%type #%id has been successfully updated!");
         }
 
-        $this->flash->addMessage(
-            $msg_type,
-            str_replace(
-                ['%type', '%id'],
-                [$entitled->getI18nType(), $id],
-                $msg
-            )
-        );
+        if (count($error_detected) > 0) {
+            foreach ($error_detected as $error) {
+                $this->flash->addMessage(
+                    'error_detected',
+                    str_replace(
+                        ['%type', '%id'],
+                        [$entitled->getI18nType(), $id],
+                        $error
+                    )
+                );
+            }
+        } else {
+            $this->flash->addMessage(
+                'success_detected',
+                str_replace(
+                    ['%type', '%id'],
+                    [$entitled->getI18nType(), $id],
+                    $msg
+                )
+            );
+        }
 
         return $response
             ->withStatus(301)
-            ->withHeader(
-                'Location',
-                $this->routeparser->urlFor(
-                    'entitleds',
-                    ['class' => $class]
-                )
-            );
+            ->withHeader('Location', $redirect_uri);
     }
 
 
index 55cd1d5e229d7616bb44b7b0b8165c26ee01d361..73b5f5831f02f444d240a428291460b818b8da94 100644 (file)
@@ -199,51 +199,47 @@ class PaymentTypeController extends CrudController
                 ->withHeader('Location', $this->cancelUri($this->getArgs($request)));
         }
 
+        $error_detected = [];
+        $msg = null;
+
         $ptype = new PaymentType($this->zdb, $id);
         $ptype->name = $post['name'];
-        $res = $ptype->store();
+        if (isset($post['name']) && $post['name'] != '') {
+            $res = $ptype->store();
+        } else {
+            $res = false;
+            $error_detected[] = _T("Missing required payment type's name!");
+        }
         $redirect_uri = $this->redirectUri($this->getArgs($request));
 
         if (!$res) {
             if ($id === null) {
-                $this->flash->addMessage(
-                    'error_detected',
-                    preg_replace(
-                        '(%s)',
-                        $ptype->getName(),
-                        _T("Payment type '%s' has not been added!")
-                    )
+                $error_detected[] = preg_replace(
+                    '(%s)',
+                    $ptype->getName(),
+                    _T("Payment type '%s' has not been added!")
                 );
             } else {
-                $this->flash->addMessage(
-                    'error_detected',
-                    preg_replace(
-                        '(%s)',
-                        $ptype->getName(),
-                        _T("Payment type '%s' has not been modified!")
-                    )
+                $error_detected[] = preg_replace(
+                    '(%s)',
+                    $ptype->getName(),
+                    _T("Payment type '%s' has not been modified!")
                 );
                 //redirect to payment type edition
                 $redirect_uri = $this->routeparser->urlFor('editPaymentType', ['id' => $id]);
             }
         } else {
             if ($id === null) {
-                $this->flash->addMessage(
-                    'success_detected',
-                    preg_replace(
-                        '(%s)',
-                        $ptype->getName(),
-                        _T("Payment type '%s' has been successfully added.")
-                    )
+                $error_detected[] = preg_replace(
+                    '(%s)',
+                    $ptype->getName(),
+                    _T("Payment type '%s' has been successfully added.")
                 );
             } else {
-                $this->flash->addMessage(
-                    'success_detected',
-                    preg_replace(
-                        '(%s)',
-                        $ptype->getName(),
-                        _T("Payment type '%s' has been successfully modified.")
-                    )
+                $msg = preg_replace(
+                    '(%s)',
+                    $ptype->getName(),
+                    _T("Payment type '%s' has been successfully modified.")
                 );
             }
         }
@@ -258,6 +254,20 @@ class PaymentTypeController extends CrudController
             }
         }
 
+        if (count($error_detected) > 0) {
+            foreach ($error_detected as $error) {
+                $this->flash->addMessage(
+                    'error_detected',
+                    $error
+                );
+            }
+        } else {
+            $this->flash->addMessage(
+                'success_detected',
+                $msg
+            );
+        }
+
         return $response
             ->withStatus(301)
             ->withHeader('Location', $redirect_uri);
index 74ddf49154976706133335a40c069c7a26602a47..9aeee14c7febce8d5501390660b15504ad8b6981 100644 (file)
@@ -193,55 +193,66 @@ class TitlesController extends CrudController
                 ->withHeader('Location', $this->cancelUri($this->getArgs($request)));
         }
 
+        $error_detected = [];
+        $msg = null;
+
         $title = new Title($id);
         $title->short = $post['short_label'];
         $title->long = $post['long_label'];
-        $res = $title->store($this->zdb);
+        if ((isset($post['short_label']) && $post['short_label'] != '') && (isset($post['long_label']) && $post['long_label'] != '')) {
+            $res = $title->store($this->zdb);
+        } else {
+            $res = false;
+            $error_detected[] = _T("Missing required title's short or long form!");
+        }
         $redirect_uri = $this->redirectUri($this->getArgs($request));
 
         if (!$res) {
             if ($id === null) {
-                $this->flash->addMessage(
-                    'error_detected',
-                    preg_replace(
-                        '(%s)',
-                        $title->short,
-                        _T("Title '%s' has not been added!")
-                    )
+                $error_detected[] = preg_replace(
+                    '(%s)',
+                    $title->short !== null ? $title->short : '',
+                    _T("Title '%s' has not been added!")
                 );
             } else {
-                $this->flash->addMessage(
-                    'error_detected',
-                    preg_replace(
-                        '(%s)',
-                        $title->short,
-                        _T("Title '%s' has not been modified!")
-                    )
+                $error_detected[] = preg_replace(
+                    '(%s)',
+                    $title->short !== null ? $title->short : '',
+                    _T("Title '%s' has not been modified!")
                 );
 
                 $redirect_uri = $this->routeparser->urlFor('editTitle', ['id' => $id]);
             }
         } else {
             if ($id === null) {
-                $this->flash->addMessage(
-                    'success_detected',
-                    preg_replace(
-                        '(%s)',
-                        $title->short,
-                        _T("Title '%s' has been successfully added.")
-                    )
+                $error_detected[] = preg_replace(
+                    '(%s)',
+                    $title->short,
+                    _T("Title '%s' has been successfully added.")
                 );
             } else {
+                $msg = preg_replace(
+                    '(%s)',
+                    $title->short,
+                    _T("Title '%s' has been successfully modified.")
+                );
+            }
+        }
+
+        if (count($error_detected) > 0) {
+            foreach ($error_detected as $error) {
                 $this->flash->addMessage(
-                    'success_detected',
-                    preg_replace(
-                        '(%s)',
-                        $title->short,
-                        _T("Title '%s' has been successfully modified.")
-                    )
+                    'error_detected',
+                    $error
                 );
             }
+        } else {
+            $this->flash->addMessage(
+                'success_detected',
+                $msg
+            );
         }
+
         return $response
             ->withStatus(301)
             ->withHeader('Location', $redirect_uri);