Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.08 KB | None | 0 0
  1. public function add()
  2.     {
  3.         $user = new User();
  4.         $title = 'Add user';
  5.         $errors = [];
  6.  
  7.         //Check errors when posting
  8.         if(!empty($_POST)){
  9.  
  10.             if( !preg_match('/^[A-Za-z ]+$/', $_POST['fullname'])){
  11.                 $errors["fullname"] = "Fullname is required";
  12.             }
  13.  
  14.             if(!is_null('email') && !filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) {
  15.                 $errors["email"] = "Email is required";
  16.             }
  17.  
  18.             if(strlen($_POST["password"]) < 8){
  19.                 $errors["password"] = "Password is required";
  20.             }
  21.  
  22.             if(strcmp($_POST["password"], $_POST["password_confirmation"])!=0){
  23.                 $errors["password_confirmation"] = "Password confirmation must be equal to password";
  24.             }
  25.  
  26.             $user_type = $_POST['user_type'] ?? null;
  27.  
  28.             if( is_null($user_type) || $user_type <0 || $user_type > 2){
  29.                     $errors["user_type"] = "Type is required";
  30.             }
  31.  
  32.             //se nao houver erros gg
  33.             if(!$errors){
  34.                 User::add($user = new User($_POST));
  35.             }
  36.  
  37.             if (isset($_POST['cancel'])){
  38.                 return header('Location: user.php');
  39.             }
  40.         }
  41.         return render_view('users.add', compact('title', 'user', 'errors'));
  42.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement