CodeIgniter Forums
validation - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: validation (/showthread.php?tid=86562)



validation - devo - 02-02-2023

how to use custome validation in model??

i have tried and followed the documentation but i am getting the following error

the documentation i follow : 

model validation
validation


this is my validation file code App\Validation :
Code:
<?php

namespace App\Validation;

class Administrators
{
    public $administrator = [
        'full_name'    => 'required',
        'username'    => 'required',
        'email'        => 'required|valid_email',
        'password'    => 'required',
        'password_confirmation' => 'required|matches[password]',
    ];
}


this is my model 

PHP Code:
<?php

namespace App\Models;

use 
CodeIgniter\Model;

class 
AdministratorModel extends Model
{
 
// Validation
 
protected $validationRules      'administrators';
 protected 
$validationMessages  = [];
 protected 
$skipValidation      false;
 protected 
$cleanValidationRules true;

 
// Callbacks
 
protected $allowCallbacks true;
 protected 
$beforeInsert  = [];
 protected 
$afterInsert    = [];
 protected 
$beforeUpdate  = [];
 protected 
$afterUpdate    = [];
 protected 
$beforeFind    = [];
 protected 
$afterFind      = [];
 protected 
$beforeDelete  = [];
 protected 
$afterDelete    = [];



this is my controller :

PHP Code:
<?php

namespace App\Controllers\Administrator;

use 
App\Controllers\BaseAdministratorController;

class 
Administrator extends BaseAdministratorController
{
private 
$model;

public function 
initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
{
parent::initController($request$response$logger);
$this->model model("App\Models\AdministratorModel");
}

/**
* Create a new resource object, from "posted" parameters
*
* @return mixed
*/
public function create()
{
$data $this->request->getPost();

echo 
'<pre>';
print_r($this->model->save($data));
echo 
'</pre>';die;
}




RE: validation - kenjis - 02-02-2023

Quote:To store your validation rules, simply create a new public property in the Config\Validation class with the name of your group.
https://codeigniter.com/user_guide/libraries/validation.html#how-to-save-your-rules



RE: validation - devo - 02-02-2023

(02-02-2023, 05:02 AM)kenjis Wrote:
Quote:To store your validation rules, simply create a new public property in the Config\Validation class with the name of your group.
https://codeigniter.com/user_guide/libraries/validation.html#how-to-save-your-rules

ohh i see, sorry I misunderstood, Is there another way to store validation rules per file?


RE: validation - kenjis - 02-02-2023

The validation rule is an array.
See https://codeigniter.com/user_guide/models/model.html#in-model-validation

You can get it from another class like Administrators
and set it to the property.