4

im new on codeigniter and try to resolve following problem for hours now. I try to set_value in a form but when i set it i get following error:

A PHP Error was encountered Severity: Error Message: Call to undefined function set_value() Filename: modals/register_modal.php Line Number: 27 Backtrace:

When i delete set_value() on line 27, everything works fine.

Line number 27 is the set_value() line:

<div class="row"> 
 <div class="form-group">
  <label class="control-label sr-only">Vorname:</label>
   <div class="col-md-8 col-md-offset-2 col-xs-10 col-xs-offset-1">
   <input type="text" class="form-control" name="firstname" placeholder="Vorname" value="<?php echo set_value('firstname'); ?>" size="50" />
   <i class="fa form-control-feedback" aria-hidden="true"></i>
   </div>
  </div>
</div>

Thats my controller:

class Form extends CI_Controller {

        public function index()
        {
                $this->load->helper('form');

                $this->load->library('form_validation');

                $this->form_validation->set_rules('firstname', 'Vorname', 'required|callback_username_check');
                $this->form_validation->set_rules('surename', 'Nachname', 'required');
                $this->form_validation->set_rules('email', 'Email', 'required');
                $this->form_validation->set_rules('password', 'Passwort', 'required');

                if ($this->form_validation->run() == FALSE)
                {
                        $this->load->view('modals/register_modal');
                }
                else
                {
                        $this->load->view('modals/login_modal');
                }
        }

        public function username_check($str)
        {
                if ($str == 'test')
                {
                        $this->form_validation->set_message('username_check', 'The {field} field can not be the word "test"');
                        return FALSE;
                }
                else
                {
                        return TRUE;
                }
        }

}

I also set Auto-Load:

$autoload['helper'] = array('form');
Hadi Reda
  • 43
  • 1
  • 1
  • 7
  • You don't have to load form helper twice. Leave it in autoload only to have it globally in all controllers, but remove from controller. Also, change the name of controller to avoid possible name collision since CodeIgniter already has Form class. After all, autoload url helper too. – Tpojka Jan 02 '16 at 12:14
  • Thank you, changed everything but still the same problem. Is there any order to set in autoload? – Hadi Reda Jan 02 '16 at 13:09
  • Not much because all classes loaded in `autoload.php` are loaded before controllers. Basic sample from documentation works or not for you? – Tpojka Jan 02 '16 at 13:38
  • Yes the basic sample works fine. I thought i can take it 1:1 with my own form and controller but i went wrong. – Hadi Reda Jan 02 '16 at 13:47
  • Maybe i get this error because my form is in a modal... – Hadi Reda Jan 02 '16 at 13:53
  • Try to narrow your code to conclude when exactly you get the error. Is modal fully loaded at once or is it loaded through AJAX afterward? Post your files on pastebin.com and link it to let us see whole story instead of code snippets. – Tpojka Jan 02 '16 at 14:04
  • I had look in here http://www.codeigniter.com/user_guide/helpers/form_helper.html#set_value and says todo `` –  Jan 03 '16 at 05:13

5 Answers5

10

You must load library('form_validation') in your Controller. This work for me:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends CI_Controller{

    public function __construct(){
        parent::__construct();
        $this -> load -> library('form_validation');
    }
....    
Michael Lihs
  • 7,460
  • 17
  • 52
  • 85
sama roeintan
  • 101
  • 1
  • 3
2

In CodeIgniter4 you have only to use the helper function to load the library

<?php namespace App\Controllers;
use App\Models\UserModel;
class Users extends BaseController {
public function index()
{
    helper (['form']);   
    ......
1

Add helper HTML

protected $helpers = ['form','url','html'];

It worked for me

Dejan Dozet
  • 948
  • 10
  • 26
0
helper(['form']);

Load the form helper in your function

cottontail
  • 10,268
  • 18
  • 50
  • 51
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 17 '22 at 22:36
0

You can add form helper in BaseController.php. you don't need to add in every cotnroller - codeigniter 4

Edit in BaseController.php

protected $helpers = ['form'];
cottontail
  • 10,268
  • 18
  • 50
  • 51