Welcome Guest, Not a member yet? Register   Sign In
Retrieve Model Validation Error Messages with Ajax
#1

(This post was last modified: 07-25-2021, 03:09 PM by the_steff.)

Hello !
I use Codeigniter 4 model with some validation rules and messages using protected $validationRules and $validationMessages (https://codeigniter4.github.io/userguide...ating-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()]); }
Reply




Theme © iAndrew 2016 - Forum software by © MyBB