Welcome Guest, Not a member yet? Register   Sign In
Language file in rule group error?
#1

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

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

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

The validation config file is a class. You can add a constructor in there.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply




Theme © iAndrew 2016 - Forum software by © MyBB