Advertisement
Guest User

Untitled

a guest
Mar 14th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. $title = 'Add user';
  2. $errors = [];
  3.  
  4. if (!empty($_POST)) {
  5. $fullname = $_POST["fullname"] ?? "";
  6. $email = $_POST["email"] ?? "";
  7. $user_type = $_POST["user_type"] ?? "";
  8. $password = $_POST["password"] ?? "";
  9. $password_confirmation = $_POST["password_confirmation"] ?? "";
  10.  
  11. if (empty($fullname)) {
  12. $errors["fullname"] = "Fullname is required";
  13. } else if (!filter_var($fullname, FILTER_VALIDATE_REGEXP, array("options" => ["regexp" => "/^[a-zA-Z\s]*$/"]))) {
  14. $errors["fullname"] = "Fullname must only contain letters and spaces";
  15. }
  16.  
  17. if (empty($email)) {
  18. $errors["email"] = "Email is required";
  19. } else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  20. $errors["email"] = "Email is not valid";
  21. }
  22.  
  23. if (empty($password)) {
  24. $errors["password"] = "Email is required";
  25. } else if (strlen($password) < 8) {
  26. $errors["password"] = "Password must have at least 8 characters";
  27. } else if ($password != $password_confirmation) {
  28. $errors["password_confirmation"] = "The passwords do not match";
  29. }
  30.  
  31. if (empty($user_type)) {
  32. $errors["user_type"] = "Type is required";
  33. } else if (!filter_var($user_type, FILTER_VALIDATE_INT, array('options' => ["min_range" => 0, "max_range"=> 2]))) {
  34. $errors["user_type"] = "Type is not valid";
  35. }
  36. }
  37.  
  38. if (empty($errors)) {
  39. render_view('users.add', compact('title','user','errors'));
  40. } else {
  41. $user = new User(['user_id' => 0, 'email' => $email, 'password' => $password, 'fullname' => $fullname, 'type' => $user_type, 'registered_at' => date("Y-m-d H:i:s")]);
  42.  
  43. User::add($user);
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement