CodeIgniter Forums

Full Version: Cannot load Language Files from module
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi
I followed the instructions https://bcit-ci.github.io/CodeIgniter4/g...dules.html but cannot load language .
Can you help me
Thanks all
You need to provide details of what you tried, how your code is setup, and what happened for us to be able to help.
In the model, the error messages of the validation, give an error if you try to use the lang helper.
(03-08-2018, 07:39 AM)dannywebdev Wrote: [ -> ]In the model, the error messages of the validation, give an error if you try to use the lang helper.

I think that you are trying something like that:

PHP Code:
<?php namespace App\Models;

use 
CodeIgniter\Model;

class 
UsersModel extends Model
{
    protected 
$table 'users';
    protected 
$allowedFields = ['email'];
    protected 
$validationRules = [
        
'email' => 'valid_email'
    
];
    protected 
$validationMessages = [
        
'email' => [
            
'valid_email' => lang('File.arrayKey'),
        ]
    ];


Then:


Quote:ErrorException #64

Constant expression contains invalid operations


PHP  does not allow this.


You will need to call the lang() inside the constructor:

PHP Code:
<?php namespace App\Models;

use 
CodeIgniter\Model;

class 
UsersModel extends Model
{
    protected 
$table 'users';
    protected 
$allowedFields = ['email'];
    protected 
$validationRules = [
        
'email' => 'valid_email'
    
];
    protected 
$validationMessages = [
        
'email' => [
            
'valid_email' => 'Opsss! We need a valid email.',
        ]
    ];

    public function 
__construct(...$params)
    {
        
parent::__construct(...$params);

        
// Override the "Opsss! We need a valid email."
        
$this->validationMessages['email']['valid_email'] = lang('Validation.timezone');
    }