CodeIgniter Forums
Codeigniter 4 unique value for single user - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Codeigniter 4 unique value for single user (/showthread.php?tid=77170)



Codeigniter 4 unique value for single user - Dr3am3rz - 07-28-2020

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!


RE: Codeigniter 4 unique value for single user - paulbalandan - 07-28-2020

It seems you are not capturing the user_id form element.


RE: Codeigniter 4 unique value for single user - ojmichael - 07-28-2020

PHP Code:
acc_id' => 'required|is_unique

You have no parameters for is_unique on this line.