Welcome Guest, Not a member yet? Register   Sign In
Save function
#1

(This post was last modified: 10-26-2019, 10:41 AM by edica.)

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



Theme © iAndrew 2016 - Forum software by © MyBB