Welcome Guest, Not a member yet? Register   Sign In
How to use validation check with validation geterrors?
#1

(This post was last modified: 12-20-2021, 01:48 PM by Kabouter.)

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.
Reply
#2

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' )){ 
Reply
#3

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.
Reply
#4

(This post was last modified: 01-04-2022, 06:13 PM by kenjis.)

(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...
Reply
#5

(This post was last modified: 06-07-2022, 07:18 PM by kirlin97.)

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

waffle game
Reply
#6

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




Theme © iAndrew 2016 - Forum software by © MyBB