diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index 4a3e954d..7f4a6725 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -10,6 +10,7 @@ use Doctrine\ORM\Query; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface; +use Symfony\Component\Validator\Validator\ValidatorInterface; class UserController extends BaseController { @@ -112,47 +113,80 @@ class UserController extends BaseController return $this->render('user/form.html.twig', $params); } - public function createSubmit(Request $req, EncoderFactoryInterface $ef) + public function createSubmit(Request $req, EncoderFactoryInterface $ef, ValidatorInterface $validator) { - // TODO: validation, compare password fields - // create new row $em = $this->getDoctrine()->getManager(); $row = new User(); - // encode password - $enc = $ef->getEncoder($row); - $password = $enc->encodePassword($req->request->get('password'), $row->getSalt()); - // set and save values $row->setUsername($req->request->get('username')) ->setFirstName($req->request->get('first_name')) ->setLastName($req->request->get('last_name')) - ->setPassword($password) ->setEmail($req->request->get('email')) ->setContactNumber($req->request->get('contact_no')) - ->setEnabled($req->request->get('enabled') ? true : false); + ->setEnabled($req->request->get('enabled') ? true : false) + ->clearRoles(); // set roles - foreach ($req->request->get('roles') as $role_id) { - // check if role exists - $role = $em->getRepository(Role::class)->find($role_id); + $roles = $req->request->get('roles'); - if (!empty($role)) - $row->addRole($role); + if (!empty($roles)) { + foreach ($roles as $role_id) { + // check if role exists + $role = $em->getRepository(Role::class)->find($role_id); + + if (!empty($role)) + $row->addRole($role); + } } - $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('user_list'); + // add errors to list + foreach ($errors as $error) { + $error_array[$error->getPropertyPath()] = $error->getMessage(); + } + + // get password inputs + $password = $req->request->get('password'); + $confirm_password = $req->request->get('confirm_password'); + + // custom validation for password fields + if (!$password) { + $error_array['password'] = 'This value should not be blank.'; + } else if ($password != $confirm_password) { + $error_array['confirm_password'] = 'Passwords do not match.'; + } else { + // encode password + $enc = $ef->getEncoder($row); + $encoded_password = $enc->encodePassword($req->request->get('password'), $row->getSalt()); + + // set password + $row->setPassword($encoded_password); + } + + // 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) @@ -163,6 +197,7 @@ class UserController extends BaseController $em = $this->getDoctrine()->getManager(); $row = $em->getRepository(User::class)->find($id); + // make sure this row exists if (empty($row)) throw $this->createNotFoundException('The item does not exist'); @@ -177,12 +212,13 @@ class UserController extends BaseController return $this->render('user/form.html.twig', $params); } - public function updateSubmit(Request $req, EncoderFactoryInterface $ef, $id) + public function updateSubmit(Request $req, EncoderFactoryInterface $ef, ValidatorInterface $validator, $id) { // get row data $em = $this->getDoctrine()->getManager(); $row = $em->getRepository(User::class)->find($id); + // make sure this row exists if (empty($row)) throw $this->createNotFoundException('The item does not exist'); @@ -195,37 +231,64 @@ class UserController extends BaseController ->setEnabled($req->request->get('enabled') ? true : false) ->clearRoles(); - // update password if provided - $password_input = $req->request->get('password'); - $confirm_password_input = $req->request->get('confirm_password'); - - if ($password_input || $confirm_password_input) { - // encode password - $enc = $ef->getEncoder($row); - $password = $enc->encodePassword($req->request->get('password'), $row->getSalt()); - - $row->setPassword($password); - } - // set roles - foreach ($req->request->get('roles') as $role_id) { - // check if role exists - $role = $em->getRepository(Role::class)->find($role_id); + $roles = $req->request->get('roles'); - if (!empty($role)) - $row->addRole($role); + if (!empty($roles)) { + foreach ($roles as $role_id) { + // check if role exists + $role = $em->getRepository(Role::class)->find($role_id); + + if (!empty($role)) + $row->addRole($role); + } } - $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('user_list'); + // add errors to list + foreach ($errors as $error) { + $error_array[$error->getPropertyPath()] = $error->getMessage(); + } + + // get password inputs + $password = $req->request->get('password'); + $confirm_password = $req->request->get('confirm_password'); + + // custom validation for password fields + if ($password || $confirm_password) { + if ($password != $confirm_password) { + $error_array['confirm_password'] = 'Passwords do not match.'; + } else { + // encode password + $enc = $ef->getEncoder($row); + $encoded_password = $enc->encodePassword($req->request->get('password'), $row->getSalt()); + + // set password + $row->setPassword($encoded_password); + } + } + + // 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/user/form.html.twig b/templates/user/form.html.twig index a49688f8..fa371bdf 100644 --- a/templates/user/form.html.twig +++ b/templates/user/form.html.twig @@ -32,86 +32,72 @@ -
+
-
-