CodeIgniter Forums
How to use validation check with validation geterrors? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: How to use validation check with validation geterrors? (/showthread.php?tid=80808)



How to use validation check with validation geterrors? - Kabouter - 12-20-2021

I'm using Codeingiter 4 with php 8.

I'm trying to validate values before they are set in an entity. Then I call the validation getErrors function to pass them to the view.

The problem is that service('validation')->getErrors() prints "The field check is mandatory." My field is called Number not check. The function is check. Is this a bug, if this isn't a bug how can I group all my errors together or do I use the check function?

Also an unrelated question what does the return $this; do in a set function of entity. I saw this in the documentation.

I do something like this:
PHP Code:
<?php

namespace App\Entities;

use 
CodeIgniter\Entity\Entity;

class 
TestEntity extends Entity
{
    
    
protected $attributes = [
        'ID' => null,
        'Number' => null


    ];
    protected $dates = ['created_at''updated_at''deleted_at'];
    protected $validationRules    = [
        'Number'    => 'regex_match[/[0-9]{11}/]',
        
    
];


    public function setNumber(string $pass)
    {
        if(service('validation')->check($pass$this->validationRules['Number'] )){
            $this->attributes['Number'] = $pass;

        }

        

      
return $this;
    }
  


PHP Code:
<?php

namespace App\Controllers;

use 
App\Models\TestModel;
use 
CodeIgniter\Controller;


helper('form');

class 
Test extends Controller
{
  

public function index()
{
    

    $test 
= new \App\Entities\TestEntity();
    $test->Number =  $this->request->getPost('Number');

print_r(service('validation')->getErrors());
        

}


If you see other problems please tell me. I'm not good at this.


RE: How to use validation check with validation geterrors? - sprhld - 01-04-2022

Hi!
return $this is for method chaining as far as i know.

I'm not sure about this line:

PHP Code:
if(service('validation')->check($pass$this->validationRules['Number'] )){ 

I would try

PHP Code:
if(service('validation')->check($pass'Number' )){ 



RE: How to use validation check with validation geterrors? - iRedds - 01-04-2022

Entity is not for validation.
Entity to represent a record from a database.

For validation, you can use a model or a service or a controller.


RE: How to use validation check with validation geterrors? - kenjis - 01-04-2022

(12-20-2021, 01:40 PM)Kabouter Wrote: The problem is that service('validation')->getErrors() prints "The field check is mandatory." My field is called Number not check. The function is check. Is this a bug, if this isn't a bug how can I group all my errors together or do I use the check function?

It is not a bug. It seems it is by design. It seems `check()` does not care about the error message.
check() is to validate only one value one time. It resets the all data and validates the value.
You can't use check() for your use case.

If you want to validate and want to get correct error messaages, use `run()` instead.
But your use case validates only one value., so run() is also not good for it...


RE: How to use validation check with validation geterrors? - kirlin97 - 06-06-2022

The entity is not intended for validation.
Representational object for a database record.

waffle game


RE: How to use validation check with validation geterrors? - stegatymi - 04-07-2023

It would seem that the function 'check()' does not care about the error message.