CodeIgniter Forums
Language file in rule group error? - 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: Language file in rule group error? (/showthread.php?tid=80810)



Language file in rule group error? - sevmusic - 12-20-2021

Is there a way to be able to use a lang() in a rule group to display errors?

In my Config/Validation.php
PHP Code:
public $clientRegistration = [
        'first_name' => ['label' => 'First Name''rules' => 'trim|required|min_length[2]|clients_name_check'],
        'last_name' => ['label' => 'Last Name''rules' => 'trim|required|min_length[2]|clients_name_check'],
        'birth_date' => ['label' => 'Birth Date''rules' => 'trim|required|check_date_format''errors' => ['check_date_format' => lang('Misc.field_not_formatted_correctly')]],
    ]; 

I am getting an error in my IDE:

"expression is not allowed as field default value"

Maybe I am missing on how to do this.

Thank you in advanced.


RE: Language file in rule group error? - stopz - 12-20-2021

Yes and there are two ways how to achieve it, I'll explain you the easy way.

First, you are facing your problem because in PHP you cannot run methods when doing class properties declaration out of constructor scope.
You are trying to declare it as public variable.

Instead describe your validation rules when class gets constructed:

PHP Code:
<?php

namespace App\Models;

class 
MyModel extends Model
{
    # ...
    function __construct()
    {
        # This completely rewrites what current model will do on validation event.
        $this->validation->setRules([
            # ...
            'birth_date' => [
                'label' => 'Birth Date'
                'rules' => 'trim|required|check_date_format'
                'errors' => [
                    'required' => lang('Misc.field_empty'),
                    'check_date_format' => lang('Misc.field_not_formatted_correctly')
                ]
            ]
        ]);
    }
    # ...


The hard answer would be to create your intermediate class that extends CI Model Class and then when creating your models you call your
intermediate constructor with all parameters that get passed to CI Model Class. A brief example:

PHP Code:
class YourModel extends IntermediateClass
{
    public function __construct()
    {
        # Init Parent Class.
        parent::__construct(
            'tableName',
            primaryKey'your_id',
            returnType'App\Entity\AnEntity',
            validationRules: [
                # ...
            ],
            allowedFields: [
                # ...
            ],
            validationMessages: [
                birth_date => lang('You.Get.The.Idea')
                # ...
            ]
        );
    }
}

# And your intermediate class looks about that:

class IntermediateClass extends Model
{
    public function __construct(
        # Model base table and id.
        protected $table null,
        protected $primaryKey 'id',

        # Model associated Data Transmission Object.
        protected $returnType 'array',

        # Validation rules and error messages.
        protected $validationRules = [],

        # List of editable columns within this model.
        protected $allowedFields = [],

        # Validation error messages.
        protected $validationMessages = []
    ) {
        # Call CodeIgniter Model that inherits model propagation.
        parent::__construct();
    }


In the long run you would benefit from your IntermediateClass simply cause you have the freedom to extend your "base model" without interfering with CI native code.


RE: Language file in rule group error? - sevmusic - 01-06-2022

Thank you for your response.

So the short answer is "no." You cannot use the lang() function in the Validation config file.

I know I can do it in the Controller but the purpose of the Validation config file (to my knowledge) is to store all those validation rules there and not the controller (or model).

Thank you for explaining why.


(12-20-2021, 03:18 PM)stopz Wrote: Yes and there are two ways how to achieve it, I'll explain you the easy way.

First, you are facing your problem because in PHP you cannot run methods when doing class properties declaration out of constructor scope.
You are trying to declare it as public variable.

Instead describe your validation rules when class gets constructed:

PHP Code:
<?php

namespace App\Models;

class 
MyModel extends Model
{
    # ...
    function __construct()
    {
        # This completely rewrites what current model will do on validation event.
        $this->validation->setRules([
            # ...
            'birth_date' => [
                'label' => 'Birth Date'
                'rules' => 'trim|required|check_date_format'
                'errors' => [
                    'required' => lang('Misc.field_empty'),
                    'check_date_format' => lang('Misc.field_not_formatted_correctly')
                ]
            ]
        ]);
    }
    # ...


The hard answer would be to create your intermediate class that extends CI Model Class and then when creating your models you call your
intermediate constructor with all parameters that get passed to CI Model Class. A brief example:

PHP Code:
class YourModel extends IntermediateClass
{
    public function __construct()
    {
        # Init Parent Class.
        parent::__construct(
            'tableName',
            primaryKey'your_id',
            returnType'App\Entity\AnEntity',
            validationRules: [
                # ...
            ],
            allowedFields: [
                # ...
            ],
            validationMessages: [
                birth_date => lang('You.Get.The.Idea')
                # ...
            ]
        );
    }
}

# And your intermediate class looks about that:

class IntermediateClass extends Model
{
    public function __construct(
        # Model base table and id.
        protected $table null,
        protected $primaryKey 'id',

        # Model associated Data Transmission Object.
        protected $returnType 'array',

        # Validation rules and error messages.
        protected $validationRules = [],

        # List of editable columns within this model.
        protected $allowedFields = [],

        # Validation error messages.
        protected $validationMessages = []
    ) {
        # Call CodeIgniter Model that inherits model propagation.
        parent::__construct();
    }


In the long run you would benefit from your IntermediateClass simply cause you have the freedom to extend your "base model" without interfering with CI native code.



RE: Language file in rule group error? - includebeer - 01-09-2022

The validation config file is a class. You can add a constructor in there.