CodeIgniter Forums
Save function - 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: Save function (/showthread.php?tid=74697)



Save function - edica - 10-26-2019

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/CodeIgniter4/models/model.html#validation-placeholders


RE: Save function - kilishan - 10-26-2019

That's not an error. You're telling it to check the database to make sure that the field slug is unique in the database. Since you're doing an update, it already exists. There are two other parts to the is_unique that you need to make use of. So your rule should be something like:

PHP Code:
'slug'  => 'is_unique[news.slug,slug,{slug}]'

This basically sets up a where clause that tells it which record to ignore when it checks for uniqueness. The {slug} acts as a placeholder for the value being set. So, it should ignore the record that has column slug that is equal to the value of slug that is being included in the data to insert.

Hopefully that makes sense.


RE: Save function - edica - 10-28-2019

Yes, I understood. Very good.

Enjoying the post. A suggestion about url_title. It would be nice to remove other special characters.

return preg_replace(array("/(á|à|ã|â|ä)/","/(Á|À|Ã|Â|Ä)/","/(é|è|ê|ë)/","/(É|È|Ê|Ë)/","/(í|ì|î|ï)/","/(Í|Ì|Î|Ï)/","/(ó|ò|õ|ô|ö)/","/(Ó|Ò|Õ|Ô|Ö)/","/(ú|ù|û|ü)/","/(Ú|Ù|Û|Ü)/","/(ñ)/","/(Ñ)/"),explode(" ","a A e E i I o O u U n N"),$string);


RE: Save function - kilishan - 10-28-2019

(10-28-2019, 08:15 AM)edica Wrote: Yes, I understood. Very good.

Enjoying the post. A suggestion about url_title. It would be nice to remove other special characters.

return preg_replace(array("/(á|à|ã|â|ä)/","/(Á|À|Ã|Â|Ä)/","/(é|è|ê|ë)/","/(É|È|Ê|Ë)/","/(í|ì|î|ï)/","/(Í|Ì|Î|Ï)/","/(ó|ò|õ|ô|ö)/","/(Ó|Ò|Õ|Ô|Ö)/","/(ú|ù|û|ü)/","/(Ú|Ù|Û|Ü)/","/(ñ)/","/(Ñ)/"),explode(" ","a A e E i I o O u U n N"),$string);

There is some discussion about that going on over at Github. The biggest problem, though, is that according to the HTTP spec, those are valid characters and we can definitely see them being important in many locales so, for now, the decision has been to leave them alone. I believe we are going to ensure they get lower-cased, though.


RE: Save function - edica - 10-29-2019

It could have one more parameter in url_title to remove these characters. By default do not override.
And use the ForeignCharacters.php file.
It is very useful.