Welcome Guest, Not a member yet? Register   Sign In
is_unique issue on update
#16

(This post was last modified: 04-10-2022, 03:11 AM by Mano.)

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}]',    
]; 
Reply


Messages In This Thread
is_unique issue on update - by eleumas - 03-25-2021, 11:12 AM
RE: is_unique issue on update - by InsiteFX - 03-25-2021, 08:43 PM
RE: is_unique issue on update - by llyimo1920 - 04-20-2021, 10:47 PM
RE: is_unique issue on update - by Ahyaislive - 11-12-2021, 12:02 AM
RE: is_unique issue on update - by eleumas - 03-26-2021, 07:15 AM
RE: is_unique issue on update - by charleyskira - 12-16-2021, 08:30 AM
RE: is_unique issue on update - by kenjis - 12-16-2021, 09:48 PM
RE: is_unique issue on update - by BilltheCat - 01-20-2022, 03:00 AM
RE: is_unique issue on update - by Mano - 04-01-2022, 08:56 AM
RE: is_unique issue on update - by InsiteFX - 04-09-2022, 12:43 AM
RE: is_unique issue on update - by kenjis - 04-09-2022, 01:01 AM
RE: is_unique issue on update - by Mano - 04-10-2022, 03:04 AM
RE: is_unique issue on update - by ingrenee - 05-30-2022, 10:19 AM
RE: is_unique issue on update - by danger89 - 10-07-2022, 02:44 PM



Theme © iAndrew 2016 - Forum software by © MyBB