Welcome Guest, Not a member yet? Register   Sign In
validation class and callbacks on private function
#1

[eluser]naytro[/eluser]
hi

this is posible to use private funtion on callbacks ?

ciao
#2

[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
}
#3

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

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

That mean CALLBACK _ _ my_function, two _.
#5

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

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

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

[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;
#9

[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.
#10

[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.




Theme © iAndrew 2016 - Forum software by © MyBB