CodeIgniter Forums
validation class and callbacks on private function - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: validation class and callbacks on private function (/thread-6476.html)

Pages: 1 2


validation class and callbacks on private function - El Forum - 02-28-2008

[eluser]naytro[/eluser]
hi

this is posible to use private funtion on callbacks ?

ciao


validation class and callbacks on private function - El Forum - 02-28-2008

[eluser]Michael Wales[/eluser]
Yes.

Code:
// Callbacks begin with callback_
// Our function is _my_function
// Final result: callback__my_function
$rules['field'] - 'callback__my_function';
$this->validation->set_rules($rules);

function _my_function() {
  // Normal callback function rules
}



validation class and callbacks on private function - El Forum - 02-28-2008

[eluser]naytro[/eluser]
I try that, but this doesnt works :/


validation class and callbacks on private function - El Forum - 02-28-2008

[eluser]sikkle[/eluser]
You probably miss the part : $rules['field'] - 'callback__my_function'.

That mean CALLBACK _ _ my_function, two _.


validation class and callbacks on private function - El Forum - 02-29-2008

[eluser]naytro[/eluser]
I see two _ but this doesnt works :/


validation class and callbacks on private function - El Forum - 02-29-2008

[eluser]xwero[/eluser]
Is the function in your controller where you validate?


validation class and callbacks on private function - El Forum - 02-29-2008

[eluser]naytro[/eluser]
yes, I have


validation class and callbacks on private function - El Forum - 02-29-2008

[eluser]naytro[/eluser]
my example code:
<?php

class Welcome extends Controller {

function Welcome()
{
parent::Controller();
}

function _username_check($username)
{
if($username === "test")
{
$this->validation->set_message('username_check', 'The %s field can not be the word "test"');
return FALSE;
}
else
return TRUE;
}


function index()
{
$this->load->library('validation');
$this->load->helper(array('form', 'url'));
$this->validation->set_error_delimiters('', '');
$rules['username'] = "required|callback__username_check";

$this->validation->set_rules($rules);

$fields['username'] = 'username';

$this->validation->set_fields($fields);

if ($this->validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->output->set_output("ok");
}
}
}
?>

myform.php
<html>
<head>
<title>My Form</title>
</head>
<body>
<?=form_open(''); ?>
<?=$this->validation->username_error; ?>
<input type="text" name="username" value="<?=$this->validation->username;?>" size="50" />
<div>&lt;input type="submit" value="Submit" /&gt;</div>
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;


validation class and callbacks on private function - El Forum - 02-29-2008

[eluser]Michael Wales[/eluser]
Code:
function _username_check($username)
{
if($username === "test")
{
$this->validation->set_message('username_check', 'The %s field can not be the word "test"');
return FALSE;
}
else
return TRUE;
}

Your missing a { after else.


validation class and callbacks on private function - El Forum - 02-29-2008

[eluser]MadZad[/eluser]
naytro,
I cut-n-paste your code and everything worked fine except for the error message. Looks like you'll need an underscore in the set_message statement:

Code:
$this->validation->set_message('_username_check', 'The %s field can not be the word "test"');

So, as far as I can see, that's the only problem with the code you posted. If that doesn't do the trick, I must give the dreaded "dunno, works on my machine" reply.