CodeIgniter Forums
Retrieve Model Validation Error Messages with Ajax - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Retrieve Model Validation Error Messages with Ajax (/showthread.php?tid=79743)



Retrieve Model Validation Error Messages with Ajax - the_steff - 07-25-2021

Hello !
I use Codeigniter 4 model with some validation rules and messages using protected $validationRules and $validationMessages (https://codeigniter4.github.io/userguide/models/model.html#validating-data).
I want to retrieve error messages on update but I don't succeed whereas the model respect the rules correctly (no effective update into the database if the rule isn't respected).
I tried this kind of thing in my Controller wich is called by an "ajax-javascript" function :
PHP Code:
(...)
$validation = \Config\Services::validation();
$member_model->update($memberId$data);
log_message("debug",$validation->listErrors());
(...) 
These lines are executed during an ajax call and I'm not trying to reproduce a "classic" php form validation process (view is'nt "recalled", I just echo a state message destinated to javascript ajax initial call). The boolean return value of the update function gives to me the information of succeed or not but I also want to get the message error with it.
Here is what I get when I'm not respecting the model's rule (and then there's no update so it's working, but I just want to retrieve the error message recorded in my model) :
Code:
<!-- DEBUG-VIEW START 1 SYSTEMPATH\Validation\Views\list.php -->

<!-- DEBUG-VIEW ENDED 1 SYSTEMPATH\Validation\Views\list.php -->
I suppose this validation system is directly related to views and html forms. But maybe there's another way to get all benefits of codeigniter model ?
Thanks !

I find the solution (few lines lower in the doc) ....

if ($model->save($data) === false) { return view('updateUser', ['errors' => $model->errors()]); }