how load helper in a model? |
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 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 )
(11-14-2019, 09:57 AM)aparedesv Wrote: Hi, you can load help in BaseController or Config\Autoload or function __contruct using helper('herlpername') ;
While you can totally use the Controller's $helper var, you can load a helper at anytime with helper('my_helper_name')
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!
(04-28-2020, 10:54 AM)segungreat Wrote: Hello You can read about it in the user guide: https://codeigniter.com/user_guide/general/helpers.html |
Welcome Guest, Not a member yet? Register Sign In |