I am unable to get the callable form validation feature of CodeIgniter 3 to work when the validation rules are placed in a separate config file.
I am getting the following error message:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_Config::$form_validation_callback_library
The config file with the form validation rules are as follows (config/fvalidation.php):
$config['client_details'] = array(
array(
'field' => 'client_abn',
'label' => 'Client ABN',
'rules' => array('trim', 'required', array('abn_callable', array($this->form_validation_callback_library, 'abn_check'))),
'errors' => array('abn_callable' => 'Invalid ABN has been entered %s.')
)
);
The form validation class attempting to be called is (i.e. $this->form_validation_callback_library):
class Form_validation_callback_library
{
public function abn_check()
{
$this->load->library('abn_validator');
$abn = $this->input->post_get('abn', TRUE);
if (!$this->abn_validator->isValidAbn($abn)) {
return FALSE;
}
return TRUE;
}
}
The controller is:
$this->config->load('fvalidation');
$validation_rules = $this->config->item('client_details');
$this->form_validation->set_rules($validation_rules);
if ($this->form_validation->run() == FALSE) {
// show form
} else {
// process form data
}
Any help would be greatly appreciated.
Cheers,
VeeDee
I would use codeigniter callback example below callback
http://www.codeigniter.com/user_guide/libraries/form_validation.html#callbacks-your-own-validation-methods
<?php
class Example extends CI_Controller {
public function index() {
$this->load->library('form_validation');
$this->form_validation->set_rules('client_abn', 'ABN Number', 'required|callback_checkabn');
if ($this->form_validation->run() == FALSE) {
$this->load->view('something');
} else {
// Redirect to success page i.e login or dashboard or what ever
redirect('/'); // Currently would redirect to home '/'
}
}
public function checkabn() {
$this->load->library('abn_validator');
$abn = $this->input->post('abn');
if (!$this->abn_validator->isValidAbn($abn)) {
$this->form_validation->set_message('checkabn', 'Invalid ABN has been entered %s.');
return FALSE;
} else {
return TRUE;
}
}
}
And on your view in or above form add
<?php echo validation_errors('<div class="error">', '</div>'); ?>
<form action="<?php echo base_url('example');?>" method="post">
<input type="text" name="client_abn" placeholder="" value="" />
</form>
This is a most common problem we face when we run custom form validation in CI. Whether the callback function is in the same controller or it is in the a library of callback function we need to pass the accessible object of the class containing the callback function.
So when your run the
$callable_validations = new Form_validation_callback_library();
$this->form_validation->run($callable_validations)
Looks like this is not possible currently on CodeIgniter 3.
I have created a crude workaround.. so please go ahead an improve it because it doesn't look pretty :)
Update the config file like so (/config/fvalidation.php):
$config['client_details'] = = array(
array(
'field' => 'client_abn',
'label' => 'Client ABN',
'rules' => array('trim', 'required', array('abn_callable', array("library:form_validation_callback_library", 'abn_check'))),
'errors' => array('abn_callable' => 'Invalid %s has been entered .')
)
);
Note the following line in the config file above as we will be using them as flags in the controller code:
array('abn_callable', array("library:form_validation_callback_library", 'abn_check'))
The Library is pretty much the same except we load the instance (/libraries/Form_validation_callback_library.php):
class Form_validation_callback_library
{
private $_CI;
function Form_validation_callback_library() {
$this->_CI =& get_instance();
log_message('info', "Form_validation_callback_library Library Initialized");
}
public function abn_check($abn)
{
$this->_CI->load->library('abn_validator');
if (!$this->_CI->abn_validator->isValidAbn($abn)) {
return FALSE;
}
return TRUE;
}
}
In the controller we load the library (/controllers/Foo.php):
// load the config file and store
$this->config->load('fvalidation', TRUE);
$rule_dataset = $this->config->item('client_details', 'fvalidation');
// search and load the 'callable' library
foreach ($rule_dataset as $i => $rules) {
if (isset($rules['rules'])) {
foreach ($rules['rules'] as $k => $rule) {
if (is_array($rule) && preg_match("/_callable/",$rule[0]) && isset($rule[1][0])) {
list ($load_type, $load_name) = explode(":", $rule[1][0]);
// load the library
$this->load->$load_type($load_name);
$rule_dataset[$i]['rules'][$k][1][0] = $this->$load_name;
}
}
}
}
// set the rules
$this->form_validation->set_rules($rule_dataset);
// load the form
if ($this->form_validation->run() == FALSE) {
// show form
} else {
// process form data
}
I did something similar to Vidura, but extended the Form Validation library by adding MY_Form_validation.php with the following code
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class GS_Form_validation extends CI_Form_validation {
public function set_rules($field, $label = '', $rules = array(), $errors = array())
{
if (is_array($rules))
{
foreach ($rules as &$rule)
{
if (is_array($rule))
{
if (is_array($rule[1]) and is_string($rule[1][0]))
{
// handles rule like ['password_check', ['library:passwords', 'check_valid_password']]
// You would set_message like $this->form_validation->set_message('password_check', 'Incorrect password');
// The advantage of defining the rule like this is you can override the callback functions error message
list ($load_type, $load_name) = explode(":", $rule[1][0]);
$CI =& get_instance();
$CI->load->$load_type($load_name);
$rule[1][0] = $CI->$load_name;
}
else if (is_string($rule[0]))
{
// handles rule like ['library:password', 'check_valid_password']
// You would set_message like $this->form_validation->set_message('check_valid_password', 'Incorrect password');
list ($load_type, $load_name) = explode(":", $rule[0]);
$CI =& get_instance();
$CI->load->$load_type($load_name);
$rule[0] = $rule[1];
$rule[1] = [$CI->$load_name, $rule[1]];
}
}
}
}
return parent::set_rules($field, $label, $rules, $errors);
}
}
Then you can define callbacks to library functions like:
$this->form_validation->set_rules(['library:passwords', 'check_valid_password']);
Where passwords is the library and check_valid_password is the method.
I've simply do (config/form_validation.php):
$CI =& get_instance();
$CI->load->model('form_validation_callback_library');
$config['client_details'] = array(
array(
'field' => 'client_abn',
'label' => 'Client ABN',
'rules' => array('trim', 'required', array('abn_callable', array($CI->form_validation_callback_library, 'abn_check'))),
'errors' => array('abn_callable' => 'Invalid ABN has been entered %s.')
)
And it works to me...
I'm running on Codeigniter 3.0.4
Related
note : everything going well when I try in Localhost.
So I have a problem when I want to call my do_login controller in my login form.
this is my controller :
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Do_login extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('login_model', '', TRUE);
}
public function index()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'email', 'trim|required');
$this->form_validation->set_rules('password', 'password', 'trim|required|callback_check_database');
if($this->form_validation->run() == FALSE)
{
$this->load->view('admin/login_view');
}
else
{
redirect('home', 'refresh');
}
}
public function check_database($password)
{
$email = $this->input->post('email', TRUE);
$result = $this->login_model->check_login($email, $password);
if($result)
{
$sess_array = array();
foreach($result as $row)
{
$sess_array = array(
'user_id' => $row->user_id,
'email' => $row->email
);
$this->session->set_userdata('logged_in', $sess_array);
}
return TRUE;
}
else
{
$this->form_validation->set_message('check_database', 'Email / Password salah');
return FALSE;
}
}
}
?>
this is my view :
<?php
$attributes = array('class' => 'form-signin', 'id' => 'myform');
echo form_open('do_login', $attributes);
?>
When I try it in Localhost, everything going well and smooth.
But when I try in my web server, everytime I submit the login form, I directed into 404.
Thanks for your help :)
Check your file names Because it happens with me that different case file name was worked on localhost but not on server.
So check it once.
I am trying to add a captcha for my login form in codeigniter.
The captcha is displaying fine. and problem is in verifying it.
When validate_captcha is being called the value from input post is correct but session value is new page value.(For example , if on the 1st page load captcha was 12345 (let's assume in second load it will be 54321) . then when in first load user inputs 12345 it will be checked with 54321.
What can I do?
Here is what I have tried
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller
{
public function index()
{
$capCode = rand(10000, 99999);
$this->session->set_userdata(array('captcha'=>$capCode));
echo $this->session->userdata['captcha'];//for debug only
$this->load->helper('captcha');
$vals = array(
'word' => $capCode ,
'img_path' => CAPTCHA_PATH,
'img_url' => base_url().CAPTCHA_PATH,
'img_width' => '150',
'img_height' => 30,
'expiration' => 1200
);
$cap = create_captcha($vals);
$data = array('un' => $un,'defTab'=>'','captcha'=>$cap);
$this->load->library('form_validation');
//I need to load different data if form is result of a post($data['defTab'])
if($this->input->post('submit'))
{
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('captcha', 'Captcha', 'required|callback_validate_captcha');
if ($this->form_validation->run() == FALSE)
{
$data['defTab'] = 'what i need';
$this->load->view('login',$data);
}
else
{
print_r($this->input->post());
}
}
else
{
$this->load->view('login',$data);
}
}
public function validate_captcha()
{
$sss=$this->input->post('captcha');
//I Use this line to find problem
$this->form_validation->set_message('validate_captcha', 'session:'.$this->session->userdata['captcha'].'\nPosted val:'.$sss);
if($sss!= $this->session->userdata['captcha'])
{
return false;
}
else
{
return true;
}
}
}
You have to set the session during creation of your form:
.
.
.
} else {
if (isset($cap["word"])) {
$this->session->set_userdata("word", $cap["word"]);
}
$this->load->view('login',$data);
}
And during the validation check it with:
if($this->input->post("word", TRUE) == $this->session->userdata("word")){
// do something
}
Before calling the create_captcha method use the below code to set the previous captcha
$this->session->set_userdata('prev_captcha',$this->session->userdata('captcha_word'));
provided captcha_word contains current captcha
and check like below
function checkCaptcha($str){
$word = $this->session->get('prev_captcha');
if(strcmp(strtoupper($str),strtoupper($word)) == 0){
return true;
}else{
return false;
}
}
need help on this one:
here's a my sample code,
i would like to add a validation message if a preg_match occurs:
pls. see inline comments for more details..
public function supplier_entry()
{
if (preg_match("/[\'^£#&*...etc.../", $this->input->post('supplier')))
{
//add or pass validation message, ex. $msg = 'Invalid Supplier Name';
// i tried $this->supplier_entry_form($msg); but its not working.
$this->supplier_entry_form();
}else{
$post_data = array(
'supplier_name' =>$this->input->post('supplier'),
'user' => $this->input->post('user'),
'trx_id' =>$this->input->post('trx_id'),
);
$this->load->model('user_model');
$this->load->model('product_model');
$this->product_model->add_new_supplier($post_data);
$user_data['trx'] = 'Supplier Entry';
$user_data['username'] = $this->user_model->user_info();
$trx_data['supplier'] = $this->product_model->get_supplier_list();
$trx_data['msg'] = 'Supplier Posted.';
$this->load->view('header',$user_data);
$this->load->view('item_supplier', $trx_data);
}
}
thanks in advance..
public function supplier_entry_form()
{
$this->load->model('user_model');
$this->load->model('product_model');
$user_data['username'] = $this->user_model->user_info();
$user_data['trx'] = 'Supplier Entry';
$trx_data['supplier'] = $this->product_model->get_supplier_list();
$this->load->view('header', $user_data);
$this->load->view('item_supplier', $trx_data);
}
Use the built in form validation.
$this->load->library('form_validation');
$this->form_validation->set_rules($this->input->post('supplier'), 'Supplier', 'trim|callback_pregMatchSupplier');
if($this->form_validation->run()==FALSE)
{
$this->supplier_entry_from();
}
// continue code here if validation passes.
function pregMatchSupplier()
{
if (preg_match("/[\'^£#&*...etc.../", $this->input->post('supplier')))
{
return FALSE;
} else {
return TRUE;
}
Then in the view you echo out the validation errors:
<?php echo validation_errors(); ?>
http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html
I want to limit my registration to emails with #mywork.com I made the following in My_Form_validation.
public function email_check($email)
{
$findme='mywork.com';
$pos = strpos($email,$findme);
if ($pos===FALSE)
{
$this->CI->form_validation->set_message('email_check', "The %s field does not have our email.");
return FALSE;
}
else
{
return TRUE;
}
}
I use it as follows. I use CI rules for username and password and it works, for email it accepts any email address. Any I appreciate any help.
function register_form($container)
{
....
....
/ Set Rules
$config = array(
...//for username
// for email
array(
'field'=>'email',
'label'=>$this->CI->lang->line('userlib_email'),
'rules'=>"trim|required|max_length[254]|valid_email|callback_email_check|callback_spare_email"
),
...// for password
);
$this->CI->form_validation->set_rules($config);
The problem with creating a callback directly in the controller is that it is now accessible in the url by calling http://localhost/yourapp/yourcontroller/yourcallback which isn't desirable. There is a more modular approach that tucks your validation rules away into configuration files. I recommend:
Your controller:
<?php
class Your_Controller extends CI_Controller{
function submit_signup(){
$this->load->library('form_validation');
if(!$this->form_validation->run('submit_signup')){
//error
}
else{
$p = $this->input->post();
//insert $p into database....
}
}
}
application/config/form_validation.php:
<?php
$config = array
(
//this array key matches what you passed into run()
'submit_signup' => array
(
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'required|max_length[255]|valid_email|belongstowork'
)
/*
,
array(
...
)
*/
)
//you would add more run() routines here, for separate form submissions.
);
application/libraries/MY_Form_validation.php:
<?php
class MY_Form_validation extends CI_Form_validation{
function __construct($config = array()){
parent::__construct($config);
}
function belongstowork($email){
$endsWith = "#mywork.com";
//see: http://stackoverflow.com/a/619725/568884
return substr_compare($endsWith, $email, -strlen($email), strlen($email)) === 0;
}
}
application/language/english/form_validation_lang.php:
Add: $lang['belongstowork'] = "Sorry, the email must belong to work.";
Are you need validation something like this in a Codeigniter callback function?
$this->form_validation->set_rules('email', 'email', 'trim|required|max_length[254]|valid_email|xss_clean|callback_spare_email[' . $this->input->post('email') . ']');
if ($this->form_validation->run() == FALSE)
{
// failed
echo 'FAIL';
}
else
{
// success
echo 'GOOD';
}
function spare_email($str)
{
// if first_item and second_item are equal
if(stristr($str, '#mywork.com') !== FALSE)
{
// success
return $str;
}
else
{
// set error message
$this->form_validation->set_message('spare_email', 'No match');
// return fail
return FALSE;
}
}
A correction to Jordan's answer, the language file that you need to edit should be located in
system/language/english/form_validation_lang.php
not application/.../form_validation_lang.php. If you create the new file under the application path with the same name, it will overwrite the original in the system path. Thus you will lose all the usage of the original filters.
I have a form on my website header where i allow the user to log in with his username/password... then i POST to /signin page and check if the username exists to allow the user to log in.. if there is a problem upon login i output these errors...
i tried using the following code to show a custom error but with no luck
if ($this->form_validation->run() == false){
$this->load->view("login/index", $data);
}else{
$return = $this->_submitLogin();
if ($return == true){
//success
}else{
$this->form_validation->set_message('new_error', 'error goes here');
//error
}
$this->load->view("login/index", $data);
}
how does set_message work and if this is the wrong method, which one allow me to show a custom error in this case?
EDIT :
validation rules:
private $validation_rules = array(
array(
'field' => 'username',
'label' => 'Username',
'rules' => 'trim|required|callback__check_valid_username|min_length[6]|max_length[20]|xss_clean'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'trim|required|min_length[6]|max_length[32]'
),
);
The set_message method allows you to set your own error messages on the fly. But one thing you should notice is that the key name has to match the function name that it corresponds to.
If you need to modify your custom rule, which is _check_valid_username, you can do so by perform set_message within this function:
function _check_valid_username($str)
{
// Your validation code
// ...
// Put this in condition where you want to return FALSE
$this->form_validation->set_message('_check_valid_username', 'Error Message');
//
}
If you want to change the default error message for a specific rule, you can do so by invoking set_message with the first parameter as the rule name and the second parameter as your custom error. E.g., if you want to change the required error :
$this->form_validation->set_message('required', 'Oops this %s is required');
If by any chance you need to change the language instead of the error statement itself, create your own form_validation_lang.php and put it into the proper language folder inside your system language directory.
As you can see here, you can display the custom error in your view in the following way:
<?php echo form_error('new_error'); ?>
PS: If this isn't your problem, post your corresponding view code and any other error message that you're getting.
The problem is that your form is already validated in your IF part! You can fix the problem by this way:
if ($this->form_validation->run() == false){
$this->load->view("login/index", $data);
}else{
$return = $this->_submitLogin();
if ($return == true){
//success
}else{
$data['error'] = 'Your error message here';
//error
}
$this->load->view("login/index", $data);
}
In the view:
echo $error;
The CI way to check user credentials is to use callbacks:
$this->form_validation->set_rules('username', 'Username', 'callback_username_check');
...
public function username_check($str) {
// your code here
}
I recommend you to read CI documentation: http://codeigniter.com/user_guide/libraries/form_validation.html
The way I did this was to add another validation rule and run the validation again. That way, I could keep the validation error display in the view consistent.
The following code is an edited excerpt from my working code.
public function login() {
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
$data['content'] = 'login';
if($this->form_validation->run()) {
$sql = "select * from users where email = ? and password = ?";
$query = $this->db->query($sql, array($this->input->post('email'), $this->input->post('password')));
if($query->num_rows()==0) {
// user not found
$this->form_validation->set_rules('account', 'Account', 'callback__noaccount');
$this->form_validation->run();
$this->load->view('template', $data);
} else {
$this->session->set_userdata('userid', $query->id);
redirect('/home');
}
} else {
$this->load->view('template', $data);
}
}
public function _noaccount() {
$this->form_validation->set_message('_noaccount', 'Account must exist');
return FALSE;
}
Require Codeigniter 3.0
Using callback_ method;
class My_controller extends CI_Controller {
function __construct() {
parent::__construct();
$this->form_validation->set_message('date_control', '%s Date Special Error');
}
public function date_control($val, $field) { // for special validate
if (preg_match("/^[0-9]{2}.[0-9]{2}.[0-9]{4}$/", $val)) {
return true;
} else {
return false;
}
}
public function my_controller_test() {
if ($this->input->post()) {
$this->form_validation->set_rules('date_field', 'Date Field', 'trim|callback_date_control[date_field]|xss_clean');
if ($this->form_validation->run() == FALSE) {
$data['errors']=validation_errors();
$this->load->view('my_view',$data);
}
}
}
}
Result:
if date = '14.07.2017' no error
if date = '14-7-2017' Date Field Date Special Error