CodeIgniter Forums
is_unique issue on update - 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: is_unique issue on update (/showthread.php?tid=78903)

Pages: 1 2


RE: is_unique issue on update - Mano - 04-01-2022

Actually I'm getting similar issue...validation works partially or not as intended...
I've a form with two fields. If I change both fields, the validation is ok. If I change only one field, validation blocks the code.

PHP Code:
protected $validationRules      = [
        
'group_name'      => 'required|min_length[3]|max_length[50]|is_unique[groups.group_name,group_id,{group_id}]',
        
'group_sigla'       => 'required|min_length[2]|max_length[5]|is_unique[groups.group_sigla,group_id,{group_id}]',
    ]; 



RE: is_unique issue on update - ikesela - 04-01-2022

(01-20-2022, 01:40 AM)oneloop Wrote:
(03-26-2021, 07:05 AM)andre.tannus Wrote: is_unique allows you to check directly against the database to ensure the value is unique for a certain field. This is quite simple when you are creating a record, but updating is a little bit tricky.

If you have a table Users with fields 'id', 'name' and 'email', such as:

id, name, email
--------------------------
1, a, [email protected]
2, b, [email protected]

and you use a rule like:

'email' => 'is_unique[Users.email]'

then an entry will be invalid for if it has an email of [email protected], since it would be a duplicate.

This is fine for creating new records, but when you are trying to update the record with id=1, email should still check for uniqueness (you don't want to be able to change record 1's email to [email protected] for example), but you do want to exclude [email protected] from the validation check (since updating it to that value would not be a duplicate).

The way you do that is by setting the exception field in the rule, like this:

'email' => 'is_unique[Users.email,id,{id}]',

which you can read like:

Field 'email' must be unique except when id is "this record's id".

So when you update record '1', it will make sure the email is unique against every other email except for the record with id = 1.

You should not replace {id} with the record's id, though, the framework will do that for you.


Has anyone found a solution to this? I am still getting the same problem
You are d0ing it wrong, is_unique the check if value is exist. what you need to do is do the opposite

is_not_unique[Users.email]

to check value is not exist (means unique) , validation passed since value not exist

validation is ok, just the function is confusing. since the usage is on opposite


RE: is_unique issue on update - Mano - 04-08-2022

(04-01-2022, 09:24 AM)ikesela Wrote:
(01-20-2022, 01:40 AM)oneloop Wrote:
(03-26-2021, 07:05 AM)andre.tannus Wrote: is_unique allows you to check directly against the database to ensure the value is unique for a certain field. This is quite simple when you are creating a record, but updating is a little bit tricky.

If you have a table Users with fields 'id', 'name' and 'email', such as:

id, name, email
--------------------------
1, a, [email protected]
2, b, [email protected]

and you use a rule like:

'email' => 'is_unique[Users.email]'

then an entry will be invalid for if it has an email of [email protected], since it would be a duplicate.

This is fine for creating new records, but when you are trying to update the record with id=1, email should still check for uniqueness (you don't want to be able to change record 1's email to [email protected] for example), but you do want to exclude [email protected] from the validation check (since updating it to that value would not be a duplicate).

The way you do that is by setting the exception field in the rule, like this:

'email' => 'is_unique[Users.email,id,{id}]',

which you can read like:

Field 'email' must be unique except when id is "this record's id".

So when you update record '1', it will make sure the email is unique against every other email except for the record with id = 1.

You should not replace {id} with the record's id, though, the framework will do that for you.


Has anyone found a solution to this? I am still getting the same problem
You are d0ing it wrong, is_unique the check if value is exist. what you need to do is do the opposite

is_not_unique[Users.email]

to check value is not exist (means unique) , validation passed since value not exist

validation is ok, just the function is confusing. since the usage is on opposite

nice april's fool...

at any rate not finding any solution...whenever I have two fields the is_unique works only if I change both the fields and not if I change only one.


RE: is_unique issue on update - InsiteFX - 04-09-2022

@kenjis , I just looked at the CodeIgniter 4 User Guide and it seems that the last table column text is getting cut off,
so that you cannot read it all.


RE: is_unique issue on update - kenjis - 04-09-2022

@InsiteFX Do you mean "Example" column?
You could scroll it to left.


RE: is_unique issue on update - Mano - 04-10-2022

at the end I've find my issue.
the reference {id} to check against is_unique criteria must be actively passed to the $data array. I wasn't passing it into the $data array because my update function had the $id has a passed argument and it was recalled only into the update function $model->update($id,$data)
Actually the best solution (I wasn't aware of) is to simply use the save() function for both new inserts and updates, always putting the $id into the $data array, as follows in example.

PHP Code:
function saveupdate($id=null)
{
    $model = new myModel();

    $request service('request');
    $postData $request->getPost();

    if (isset($postData['submit']))
    {
          $data 
          [
              'id'    => $id,
              'firstfield'=> $this->request->getVar('firstfield'),
              'secondfield' => $this->request->getVar('secondfield'),
          ];
    
          
if ($model -> save($data) === false)
          {
              return redirect()->back()->withInput()->with('errors',$model->errors());
          }
              else
          {
              return redirect()->back();
          }
    }


while in the model I'll have the following validation rules:

PHP Code:
protected $validationRules 
[
    'firstfield'    => 'required|is_unique[table.firstfield,id,{id}]',
    'secondfield'    => 'required|is_unique[table.secondfield,id,{id}]',    
]; 



RE: is_unique issue on update - ingrenee - 05-30-2022

(04-10-2022, 03:04 AM)Thank for the solution !Mano Wrote: at the end I've find my issue.
the reference {id} to check against is_unique criteria must be actively passed to the $data array. I wasn't passing it into the $data array because my update function had the $id has a passed argument and it was recalled only into the update function $model->update($id,$data)
Actually the best solution (I wasn't aware of) is to simply use the save() function for both new inserts and updates, always putting the $id into the $data array, as follows in example.

PHP Code:
function saveupdate($id=null)
{
    $model = new myModel();

    $request service('request');
    $postData $request->getPost();

    if (isset($postData['submit']))
    {
          $data 
          [
              'id'    => $id,
              'firstfield'=> $this->request->getVar('firstfield'),
              'secondfield' => $this->request->getVar('secondfield'),
          ];
    
          
if ($model -> save($data) === false)
          {
              return redirect()->back()->withInput()->with('errors',$model->errors());
          }
              else
          {
              return redirect()->back();
          }
    }


while in the model I'll have the following validation rules:

PHP Code:
protected $validationRules 
[
    'firstfield'    => 'required|is_unique[table.firstfield,id,{id}]',
    'secondfield'    => 'required|is_unique[table.secondfield,id,{id}]',    
]; 



RE: is_unique issue on update - danger89 - 10-07-2022

Just a small remark (for myself and others).

I was using the model update() method in combination with post data coming from a HTML form:

PHP Code:
$postData      $this->request->getPost();
$model->update($id$postData); 
This "is_unique" validation rule only works if the id (primary key) is set. Despite I'm explicitly providing the id already to the model update() method.

TLDR: be sure that the data contains the id key even when using the model update() method. Example:

PHP Code:
$postData['id'] = $theId