Welcome Guest, Not a member yet? Register   Sign In
CI version 4.1.1 Call to a member function setRules() on null
#1

(This post was last modified: 03-08-2021, 09:28 AM by lnavarro.)

Hi all, im having this problem using validations, on my production server




here is my controller code



PHP Code:
<?php 
namespace App\Controllers\Auth;

use 
CodeIgniter\Controller;
use 
Config\Services;
use 
App\Models\UsersAuthModel;
use 
CodeIgniter\HTTP\Request;
use 
CodeIgniter\HTTP\RequestInterface;

//use App\Models\LogsModel;

class UsersAuth extends Controller
{

    
/**
     * Access to current session.
     *
     * @var \CodeIgniter\Session\Session
     */
    
protected $session;

    
/**
     * Authentication settings.
     */
    
protected $config;


    //--------------------------------------------------------------------

    
public function __construct()
    {
        
// start session
        
$this->session Services::session();
    }

public function 
registerAttemp()
    {
        
helper(['form''url''text']);

        
//guarda el usuario, la validación ocurre en el modelo
        
$users = new UsersAuthModel();
        
$getRule $users->getRule('reglasregistro');
        
$users->setValidationRules($getRule);
        
        $user = [
            'email_user'        => $this->request->getPost('email_user'),
            'rol'                => $this->request->getPost('rol'),
            'password'             => $this->request->getPost('password'),
            'password_confirm'    => $this->request->getPost('password_confirm')
        ];

        var_dump($user);

        if (!$users->save($user)) {
            return 
redirect()->back()->withInput()->with('errors'$users->errors());
        }

        
//registro exitoso
        return redirect()->to('ingresar')->with('success''Registro Exitoso');
    }






here my model code


PHP Code:
?php namespace App\Models;

use 
CodeIgniter\Model;

class 
UsersAuthModel extends Model
{

    protected 
$table      'usersauth';
    protected 
$primaryKey 'id_user';

    protected 
$useAutoIncrement true;

    protected 
$returnType 'array';
    protected 
$useSoftDeletes false;

    
// el modelo remueve los demas campos
    
protected $allowedFields = [
        
'email_user''rol''password''password_confirm'
    
];

    protected 
$useTimestamps true;
    protected 
$createdField  'created_at';
    protected 
$updatedField  'updated_at';
    protected 
$dateFormat       'datetime';

    protected 
$validationRules = [];

    
// se requieren diferentes reglas de validacion para registro, account update, etc
    
protected $dynamicRules = [
        
'reglasregistro' => [
            
'email_user'         => 'required',
            
'rol'                 => 'required',
            
'password'            => 'required',
            
'password_confirm'    => 'matches[password]'
        
]
    ];

    protected 
$validationMessages = [];

    protected 
$skipValidation false;

    
// this runs after field validation
    
protected $beforeInsert = ['hashPassword'];
    protected 
$beforeUpdate = ['hashPassword'];


    //--------------------------------------------------------------------

    /**
     * Retrieves validation rule
     */
    
public function getRule(string $rule)
    {
        return 
$this->dynamicRules[$rule];
    }

    //--------------------------------------------------------------------

    /**
     * Hashes the password after field validation and before insert/update
     */
    
protected function hashPassword(array $data)
    {
        if (! isset(
$data['data']['password'])) return $data;

        
$data['data']['password_hash'] = password_hash($data['data']['password'], PASSWORD_DEFAULT);
        unset(
$data['data']['password']);
        unset(
$data['data']['password_confirm']);

        return 
$data;
    }






my html form code


Code:
<form action="registrar" method="POST" accept-charset="UTF-8" onsubmit="registerButton.disabled = true; return true;">
                    <?php echo csrf_field() ?>
                   
                    <div class="form-group">
                        <input required type="email" id="mail" name="email_user" placeholder="[email protected]"/>
                    </div>
                    <div class="form-group">
                       
                        <div>
                        <input type="hidden" id="admin" name="rol" value="admin">
                       
                        </div>
                    </div>
                   
                    <div class="form-group">
                        <input class="form-control" required type="password" name="password" placeholder="Contraseña" />
                    </div>
                    <div class="form-group">
                        <input class="form-control" required type="password" name="password_confirm" placeholder="Confirmar Contraseña" />
                    </div>
                   
                    <button name="registerButton" class="btn btn-primary shadow-2 mb-4">Registrar</button>
                </form>





I have search in this forum, but nothing found





pd: sorry for my bad english Smile
Reply
#2

Is this the complete model code?
I am asking because it seems to me that you have override the constructor in your model.
Reply
#3

(03-08-2021, 10:46 AM)iRedds Wrote: Is this the complete model code?
I am asking because it seems to me that you have override the constructor in your model.

thank for responce.....yes, this is my complete model code
Reply
#4

Can you show the complete error stack?
Reply
#5

(03-09-2021, 11:20 AM)paulbalandan Wrote: Can you show the complete error stack?

there is some screen shot

Attached Files Thumbnail(s)
       
Reply
#6

The value of the validation property is initially undefined. Only in the constructor is it assigned an instance of the validation class.

An error means that either the constructor in the BaseModel class is not called (overridden in the inheritors), or the property value is reset.
Reply
#7

@iRedds is right. The validation property was not initialized with the Validation instance.

In your controller code, right after instantiating UsersAuthModel to $users, can you do a dump using dd()?

dd($users);
Reply




Theme © iAndrew 2016 - Forum software by © MyBB