Welcome Guest, Not a member yet? Register   Sign In
$model->errors() behaviour
#1

Greetings.
I have a problem to extend custom error into in-model validation errors. So in false condition when I run db operation, lets say insert, then I tried catch errors via errors() method.
Here example code:

Code:
$data = [
  'name' => 'Ilyani',
  'birthday' => '',
  'gender' => 'Female',
  'weight' => '',
  'height' => '',
];

if($this->model->insert($data)) {
   // success condition
} else {
   $errors = $this->model->errors();
   if( $this->request->getPost('height') == null ) {
     $errors['height'] = 'Height must be filled.'
   }
   dd($errors);
}

My question is why existing array in $this->model->errors() gone, replaced by item its supposed being added into existing array.

How the behaviour of this method ($model->errors)


Thank you.
Reply
#2

> My question is why existing array in $this->model->errors() gone, replaced by item its supposed being added into existing array.

What do you mean?

$model->errors() returns the validation errors if the validation errors exist.
Reply
#3

Should he not be using $model->getErrors(); to get all the errors?
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

(09-15-2022, 07:31 PM)kenjis Wrote: > My question is why existing array in $this->model->errors() gone, replaced by item its supposed being added into existing array.

What do you mean?

$model->errors() returns the validation errors if the validation errors exist.

Thank you so much for the answer. 

$this->model->errors() not gone, I try to add item on it (appended), with push a new item.

So, in my case, some field mandatory with condition, like field height, for example. Only mandatory (required) if other field, lets say checkbox checked then this field  height required otherwise no (if that checkbox unchecked).
And also maybe other different form, like some form have height field, and other not.

So, this slightly code that I been trying to do.


PHP Code:
$errors $this->model->errors();

if(
$this->request->getPost('checkbox') == true && $this->request->getPost('height') == '') {
$errors['height'] = 'Height must be filled.'



But, what I got was the whole errors replaced by only item height. Not appended.
Why is that?


Anyway thank you so much
Reply
#5

(09-16-2022, 12:29 AM)InsiteFX Wrote: Should he not be using $model->getErrors(); to get all the errors?

Thanks for replying.

$model->getErrors() shows what we define in model. I want to append new item that not we define in model.


Once again, thank you.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB