From aece8408e8c7e6b84110da7dd3dab38f84e25cc3 Mon Sep 17 00:00:00 2001 From: Ramon Gutierrez Date: Wed, 10 Jan 2018 04:42:13 +0800 Subject: [PATCH] Use server side form submission and validation for role crud --- src/Controller/RoleController.php | 79 +++++++++++++++++-------- templates/role/form.html.twig | 95 ++++++++++++++++++++++++++----- templates/role/list.html.twig | 2 +- 3 files changed, 139 insertions(+), 37 deletions(-) diff --git a/src/Controller/RoleController.php b/src/Controller/RoleController.php index 066e88c0..612ed1f0 100644 --- a/src/Controller/RoleController.php +++ b/src/Controller/RoleController.php @@ -8,6 +8,7 @@ use App\Entity\Role; use Doctrine\ORM\Query; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Validator\Validator\ValidatorInterface; class RoleController extends BaseController { @@ -103,10 +104,8 @@ class RoleController extends BaseController return $this->render('role/form.html.twig', $params); } - public function createSubmit(Request $req) + public function createSubmit(Request $req, ValidatorInterface $validator) { - // TODO: validation - // create new row $em = $this->getDoctrine()->getManager(); $row = new Role(); @@ -115,17 +114,34 @@ class RoleController extends BaseController $row->setID($req->request->get('id')) ->setName($req->request->get('name')); - $em->persist($row); - $em->flush(); + // validate + $errors = $validator->validate($row); - // set success - $this->addFlash( - 'success', - 'Changes have been saved!' - ); + // initialize error list + $error_array = []; - // response - return $this->redirectToRoute('role_list'); + // add errors to list + foreach ($errors as $error) { + $error_array[$error->getPropertyPath()] = $error->getMessage(); + } + + // check if any errors were found + if (!empty($error_array)) { + // return validation failure response + return $this->json([ + 'success' => false, + 'errors' => $error_array + ], 422); + } else { + // validated! save the entity + $em->persist($row); + $em->flush(); + + // return successful response + return $this->json([ + 'success' => 'Changes have been saved!' + ]); + } } public function update($id) @@ -136,6 +152,7 @@ class RoleController extends BaseController $em = $this->getDoctrine()->getManager(); $row = $em->getRepository(Role::class)->find($id); + // make sure this row exists if (empty($row)) throw $this->createNotFoundException('The item does not exist'); @@ -146,14 +163,13 @@ class RoleController extends BaseController return $this->render('role/form.html.twig', $params); } - public function updateSubmit(Request $req, $id) + public function updateSubmit(Request $req, ValidatorInterface $validator, $id) { - // TODO: validation - // get row data $em = $this->getDoctrine()->getManager(); $row = $em->getRepository(Role::class)->find($id); + // make sure this row exists if (empty($row)) throw $this->createNotFoundException('The item does not exist'); @@ -161,16 +177,33 @@ class RoleController extends BaseController $row->setID($req->request->get('id')) ->setName($req->request->get('name')); - $em->flush(); + // validate + $errors = $validator->validate($row); - // set success - $this->addFlash( - 'success', - 'Changes have been saved!' - ); + // initialize error list + $error_array = []; - // response - return $this->redirectToRoute('role_list'); + // add errors to list + foreach ($errors as $error) { + $error_array[$error->getPropertyPath()] = $error->getMessage(); + } + + // check if any errors were found + if (!empty($error_array)) { + // return validation failure response + return $this->json([ + 'success' => false, + 'errors' => $error_array + ], 422); + } else { + // validated! save the entity + $em->flush(); + + // return successful response + return $this->json([ + 'success' => 'Changes have been saved!' + ]); + } } public function destroy($id) diff --git a/templates/role/form.html.twig b/templates/role/form.html.twig index 2b80257c..f78faa84 100644 --- a/templates/role/form.html.twig +++ b/templates/role/form.html.twig @@ -32,25 +32,25 @@ -
+
-
- +
+
- - {% if error.id is defined %} - - {% endif %} + + Unique identifier for this role
-
- +
+
- - {% if error.name is defined %} - - {% endif %} + + Display name for this role
@@ -70,4 +70,73 @@
+{% endblock %} + +{% block scripts %} + {% endblock %} \ No newline at end of file diff --git a/templates/role/list.html.twig b/templates/role/list.html.twig index f176627e..89d4d833 100644 --- a/templates/role/list.html.twig +++ b/templates/role/list.html.twig @@ -124,7 +124,7 @@ swal({ title: 'Confirmation', - text: 'Are you sure you want to delete ' + id + '?', + html: 'Are you sure you want to delete ' + id + '?', type: 'warning', showCancelButton: true }).then((result) => {