• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Codeigniter 4 unique value for single user

#1
I have a form to let user to register their IGN for a few games. I would like to make the IGN unique, meaning User A can have ignA for any games that he is playing into the database. If User B were to register his IGN in the database using any of the IGN registered by User A, it will show error as that is unique. But I have no clue how do I go about doing this.

After searching for so long, I have managed to find this but I am not sure whether is it what I am looking for. I have tried and I get an error and I can't figure out what's wrong with the string.

[Image: v9gdK.jpg]

Below is my code :


Code:
public function game_reg()
    {
        $data = [];
        helper(['form']);
       
        $validation =  \Config\Services::validation();
       
        if(!session()->get('isLoggedIn')){
            return redirect()->to(base_url('account/login'));
        }else{
       
            if($this->request->getMethod() == 'post'){
                //validations
                $rules = [
                    'game_id' => 'required',
                    'ign' => 'required|is_unique[game_reg.ign,user_id,1]',
                    'acc_id' => 'required|is_unique'
                ];
               
                $errors = [
                    'ign' => [
                        'is_unique' => 'IGN already exist!'
                    ],
                    'acc_id' => [
                        'is_unique' => 'Account ID already exist!'
                    ]
                ];
               
                if(!$this->validate($rules, $errors)){
                    $data['validation'] = $this->validator;
                }else{
                    //store information into database
                   
                    $model = new AccountModel();
                   
                    $newData = [
                        'game_id' => $this->request->getVar('game_id'),
                        'ign' => $this->request->getVar('ign'),
                        'acc_id' => $this->request->getVar('acc_id'),
                        'created_by' => $this->request->getVar('username')
                    ];
                    $model->save($newData);
                   
                    return redirect()->to(base_url('account/game_reg'));
                }
            }
           
        }
       
        echo view('templates/header', $data);
        echo view('account/game_reg');
        echo view('templates/footer');
    }

Hope someone out there can help me on what and how do I do a check for this case. Thanks in advance guys!
Reply

#2
It seems you are not capturing the user_id form element.
Reply

#3
PHP Code:
acc_id' => 'required|is_unique

You have no parameters for is_unique on this line.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.