Welcome Guest, Not a member yet? Register   Sign In
how load helper in a model?
#1

Hi,

I need load a custom helper in a Model...

It's possible?

I need translate $validationsRules label

Some idea?

I think to load custom helper for get user lang and set label each validationRule

thanks

the idea is something like:

helper('admin_auth'); // method comprovacio_idioma_usuari()
$idioma = comprovacio_idioma_usuari();
protected $validationRules    = [
   'nom' => ['label' => lang('Admin.nom', [], $idioma), 'rules' => 'min_length[2]|max_length[100]|required']
];
Reply
#2

You can always just add a require_once in the top of your model

Helpers are nothing but stand alone methods.

Did you try loading it in your controller then using it in your model?
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(11-14-2019, 09:57 AM)aparedesv Wrote: Hi,

I need load a custom helper in a Model...

It's possible?

I need translate $validationsRules label

Some idea?

I think to load custom helper for get user lang and set label each validationRule

thanks

the idea is something like:

helper('admin_auth'); // method comprovacio_idioma_usuari()
$idioma = comprovacio_idioma_usuari();
protected $validationRules    = [
   'nom' => ['label' => lang('Admin.nom', [], $idioma), 'rules' => 'min_length[2]|max_length[100]|required']
];




you can load help in BaseController or Config\Autoload or function __contruct using helper('herlpername') ;
Hernan Rodríguez
[email protected]
Reply
#4

While you can totally use the Controller's $helper var, you can load a helper at anytime with helper('my_helper_name')
Reply
#5

thanks!

finally, I load the helper in controller:

in model:
protected $validationRules = [
'nom' => ['rules' => 'required|min_length[2]|max_length[100]'],
'nom_anterior' => ['rules' => 'min_length[2]|max_length[100]'],
'imo' => ['rules' => 'required|exact_length[10]'],
];

in controller:
$validation = \Config\Services::validation();

$iotsModel = new IotsModel();
$rules = $iotsModel->getValidationRules();

$idioma = comprovacio_idioma_usuari();

$rules1 = [];
foreach($rules as $key => $rule)
{
$rule['label'] = lang('Admin.'.$key, [], $idioma);
array_push($rules1, $rule);
}

$validation->setRules($rules1);

surely there are more elegant ways, but it works perfectly!
Reply
#6

Hello

What about creating and loading a custom helper
Reply
#7

(04-28-2020, 10:54 AM)segungreat Wrote: Hello

What about creating and loading a custom helper

You can read about it in the user guide: https://codeigniter.com/user_guide/general/helpers.html
Reply




Theme © iAndrew 2016 - Forum software by © MyBB