I have tried to copy from Codegniter’s documentation, but I can’t make form validation callbacks working.
I added helper form, url and library form_validation. It’s not working and always returns “false”
Controller
public function addtest() { $this->load->helper(array('form', 'url')); $this->load->library('form_validation'); $this->form_validation->set_rules('username', 'Username', 'callback_username_check'); if($_SERVER['REQUEST_METHOD'] == 'POST') { if ($this->form_validation->run() == TRUE) { die('true'); } else { die('false'); } } $this->template ->build('myform',array()); } public function username_check($str) { if ($str == 'test') { return TRUE; } else { return FALSE; } }
View
<form method="post" action="" class="form-horizontal form-label-left"> <div class="col-xs-12 col-md-9"> <div class="x_panel"> <div class="form-group col-xs-12"> <div class="col-xs-3"> <label class="control-label">Folder name</label> </div> <div class="col-xs-9"> <input type="text" name="username" value="" class="form-control " id="" placeholder=""> </div> </div> <button type="submit" class="btn btn-success">Submit</button> </div> </div> </form>
Advertisement
Answer
Extend your form_validation library in Libraries.php
MY_Form_validation.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); class MY_Form_validation extends CI_Form_validation { function run($module = '', $group = '') { (is_object($module)) AND $this->CI =& $module; return parent::run($group); } } /* End of file MY_Form_validation.php */ /* Location: ./application/libraries/MY_Form_validation.php */