Welcome Guest, Not a member yet? Register   Sign In
Language file in rule group error?
#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


Messages In This Thread
Language file in rule group error? - by sevmusic - 12-20-2021, 02:46 PM
RE: Language file in rule group error? - by stopz - 12-20-2021, 03:18 PM
RE: Language file in rule group error? - by sevmusic - 01-06-2022, 12:34 PM



Theme © iAndrew 2016 - Forum software by © MyBB