• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Save function

#1
Following the tutorial in the guide. The save function can insert or update. But using update will make an error when you have an is_unique rule in the model.

Controller News.php:


PHP Code:
$data = [
  'id'    => $this->request->getVar('id'),
  'title' => $this->request->getVar('title'),
  'slug'  => url_title($this->request->getVar('title'), '-'TRUE),
  'body' => $this->request->getVar('body')
];

if (
$model->save($data)) {
  echo view('news/success');
} else {
  echo view('news/form_news', ['errors' => $model->errors()]);



NewsModel.php:


PHP Code:
protected $validationRules = [
        'title' => 'required|min_length[3]|max_length[128]',
        'slug'  => 'is_unique[news.slug]',
        'body'  => 'required',
]; 


Ok, I think I found the error:

https://codeigniter4.github.io/CodeIgnit...aceholders
Reply


Messages In This Thread
Save function - by edica - 10-26-2019, 10:32 AM
RE: Save function - by kilishan - 10-26-2019, 08:32 PM
RE: Save function - by edica - 10-28-2019, 08:15 AM
RE: Save function - by kilishan - 10-28-2019, 08:33 AM
RE: Save function - by edica - 10-29-2019, 05:37 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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