Advertisement
Guest User

Codeigniter 4 - Controller - Insert validate

a guest
Jul 25th, 2021
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.14 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Controllers;
  4.  
  5. use CodeIgniter\RESTful\ResourceController;
  6.  
  7. class Learn extends ResourceController
  8. {
  9.  
  10.     public $user;
  11.  
  12.     public function __construct()
  13.     {
  14.         $this->user = model( 'UserModel' );
  15.     }
  16.  
  17.     public function create()
  18.     {
  19.         try {
  20.  
  21.             $data       = $this->request->getPost();
  22.             $this->user = $this->user->fill( $data );
  23.  
  24.             if (isset( $data['showPrefix'] ) && $data['showPrefix'] === 'on') {
  25.                 if (strlen( $data['prefix'] ) <> 4) {
  26.                     throw new \RuntimeException( "'Prefix must be exact 4 chars length'" );
  27.                 }
  28.                 $this->user->login = trim( $data['prefix'] . $data['login'] );
  29.             }
  30.  
  31.             // Have commented out this code, as when we use password hashing function with fill,
  32.             // our hashed password will be longer, than clean password and it will prevent validation rule
  33.             // for password min_length[8]..
  34.  
  35.             // if (isset($data['password']) && strlen( $data['password'] ) < PASSWORD_MIN_LENGTH) {
  36. //                log_message( 'info', 'pw min length error' );
  37. //                return $this->fail( 'Password minimum length is ' . PASSWORD_MIN_LENGTH );
  38. //            }
  39.             //                  ^
  40.             // INSTEAD OF THIS  | => GO TO LINE(64)
  41.  
  42.             // Validation @ Models/User
  43. //            protected $validationRules = [
  44. //                'login'    => [
  45. //                    'rules' => [
  46. //                        'required',
  47. //                        'min_length[' . LOGIN_MIN_LENGTH . ']',
  48. //                        'max_length[' . LOGIN_MAX_LENGTH . ']',
  49. //                        'is_unique[accounts.login]',
  50. //                        'regex_match[/^([a-zA-Z0-9_])\w+$/]'
  51. //                    ],
  52. //                ],
  53. //                'password' => [
  54. //                    'required',
  55. //                    'min_length[' . PASSWORD_MIN_LENGTH . ']',
  56. //                    'max_length[' . PASSWORD_MAX_LENGTH . ']'
  57. //                ],
  58. //                'email'    => [
  59. //                    'required',
  60. //                    'valid_email',
  61. //                ]
  62. //            ];
  63.  
  64.             // 64 LINE, WE USE THIS @ App\Entities\User
  65.             // set when we set password by entity $userEntity->password('asdd') || $userEntity->password = 'asd';
  66.             // we will get error, maybe there is better variants (and there is, but im showing what i know/done yet :( )
  67. //            public function setPassword(string $pass): static
  68. //            {
  69. //                try {
  70. //                    helper( 'lineage' );
  71. //
  72. //                    if (strlen( $pass ) < PASSWORD_MIN_LENGTH) {
  73. //                        throw new \RuntimeException( lang( 'Validation.min_length', [ 'min_length' => PASSWORD_MIN_LENGTH ] ) );
  74. //                    }
  75. //
  76. //                    if (strlen( $pass ) > PASSWORD_MAX_LENGTH) {
  77. //                        throw new \RuntimeException( lang( 'Validation.max_length', [ 'max_length' => PASSWORD_MAX_LENGTH ] ) );
  78. //                    }
  79. //
  80. //                    $this->attributes['password'] = hashPassword( $pass );
  81. //                } catch (\RuntimeException | \Exception $e) {
  82. //                    throw new \RuntimeException( $e->getMessage() );
  83. //                }
  84. //                return $this;
  85. //            }
  86.  
  87.             // Validate before insert action
  88.             if (!$this->validation->run( $this->user->toArray(), 'user', L2J_LS )) {
  89.                 return $this->fail( $this->validation->getErrors() );
  90.             }
  91.  
  92.             $user_id = $this->model->insert( $this->user );
  93.  
  94.             if ($this->model->errors()) {
  95.                 log_message( 'debug', 'model errors: ' . print_r( $this->model->errors(), true ) );
  96.                 return $this->fail( $this->model->errors() );
  97.             } else {
  98.                 // $user_id available if need
  99.             }
  100.  
  101.             return $this->respondCreated( lang( 'User.user_created' ) );
  102.  
  103.         } catch (\Exception $e) {
  104.             ErrorHandler::handle( $e );
  105.             return $this->fail( $e->getMessage() );
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement