CodeIgniter Forums

Full Version: Crazy with callbacks
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I'm going crazy,
I think I did everything correctly, because I took the code of another example of my project and everything works properly them.

Controller:
PHP Code:
public function title_check_exist($title) {
                
$this->pages->title_check($title);
    }
    public function 
link_check_exist($link) {
                
$this->pages->link_check($link);
    }
        public function 
create() {
                
$rules = array(
                array(
                    
'field'=>'title',
                    
'label'=>'Titolo della pagina',
                    
'rules'=>'alpha|required|min_length[3]|callback_title_check_exist'
                
),
                                array(
                    
'field'=>'link',
                    
'label'=>'Link della pagina',
                    
'rules'=>'min_length[3]|required|callback_link_check_exist'
                
),
                                array(
                    
'field'=>'message',
                    
'label'=>'Testo della pagina',
                    
'rules'=>'min_length[20]|required'
                
)

            );
.... 

Model:

PHP Code:
public function title_check($title) {
        
$db $this->load->database('default'TRUE);
        
$db->where('title',$title);
        
$query $db->get('pagine');
        if (
$query->num_rows() > 0){
            return 
TRUE;
            
$this->form_validation->set_message('title_check''Il titolo è già utilizzato.');
        }else{
            return 
FALSE;
        }
    }
    public function 
link_check($link) {
        
$db $this->load->database('default'TRUE);
        
$db->where('link',$link);
        
$query $db->get('pagine');
        if (
$query->num_rows() > 0){
            return 
TRUE;
            
$this->form_validation->set_message('link_check''Il link è già utilizzato.');
        }else{
            return 
FALSE;
        }
    } 

When I insert a title or a link already exists in the database, it is not calculated but also created the page.

All other rules instead are read properly and when something deliberately wrong in the callback is signaled.

What is the error that I can not find?



Thanks for your continued support that you give me. Big Grin

EDIT: I can try edit this part
PHP Code:
if ($query->num_rows() > 0){
            return 
TRUE;
            
$this->form_validation->set_message('title_check''Il titolo è già utilizzato.');
        }else{
            return 
FALSE;
        } 

with this code:
PHP Code:
if ($title 'title of page'){
            return 
TRUE;
            
$this->form_validation->set_message('title_check''Il titolo è già utilizzato.');
        }else{
            return 
FALSE;
        } 

and the result it is the same.
Ok, I'm stupid ahah

with the is_unique rules I can solve this problem peacefully.

I would really like to know why this method does not work Smile
You set the error message when the condition is FALSE, not true. True means it passed the rule, so no error message. Your rule is probably working but you probably just aren't seeing the error message.
I also tried to move the message after the else.

But if even so if I write the same values as the page is created. In db sprouting 2 equal voices.
Your callback:
callback_title_check_exist

Your function name:
title_check (should be title_check_exist)

Also, you want to set the error message BEFORE returning false in the callback.
I show you the code of the other case. I seeing that this solved is_unique.

Model:
PHP Code:
public function username_check($username) {
            
$account $this->load->database('account'TRUE);
            
$account->where('login',$username);
            
$query $account->get('account');
            if (
$query->num_rows() > 0){
                echo 
$query->num_rows();
                
$this->form_validation->set_message('username_check_exist''L´username è già utilizzato.');
                return 
FALSE;
            }else{
                return 
TRUE;
            }
        } 

Controller:
PHP Code:
public function username_check_exist($username) {
        
$this->auth->username_check($username);
    }
public function 
register() {
        
$rules = array(
                array(
                    
'field'=>'username',
                    
'label'=>'Account ID',
                    
'rules'=>'trim|required|min_length[6]|max_length[16]|callback_username_check_exist'
                
),
ecc.... 

In this case I can try to stamp the $query->num_rows() and the result is 1 in the page... but It does not show the error code.

And if I fill out all the fields also launches the query.
public function username_check($username) {
...
$this->form_validation->set_message('username_check_exist', 'L´username è già utilizzato.');

Your message name needs to match the rule (function) name. Rule name is "username_check", so message also must be "username_check" and not "username_check_exist"
(06-10-2015, 06:49 AM)CroNiX Wrote: [ -> ]public function username_check($username) {
...
$this->form_validation->set_message('username_check_exist', 'L´username è già utilizzato.');

Your message name needs to match the rule (function) name. Rule name is "username_check", so message also must be "username_check" and not "username_check_exist"


But in the controller my rules is 

PHP Code:
public function username_check_exist($username) {
        
$this->auth->username_check($username);
    } 

for this reason in the callback I can use callback_sername_check_exist


But at this point, even if it does not show me the error message, you should at least stop sending the query, no?
I think it's because you're setting the error message OUTSIDE of where the actual validation rule (username_check_exist) is defined.
In your auth:
PHP Code:
public function username_check($username) {
    
$account $this->load->database('account'TRUE);
    
$account->where('login',$username);
    
$query $account->get('account');
    return (
$query->num_rows() > 0);  //returns TRUE/FALSE


In your controller:
PHP Code:
public function username_check_exist($username) {
  
//set the error message for THIS validation rule
  
$this->form_validation->set_message('username_check_exist''L´username è già utilizzato.');

  
//just return result from auth::username_check(), which will be true/false
  
return $this->auth->username_check($username);

(06-10-2015, 07:01 AM)CroNiX Wrote: [ -> ]I think it's because you're setting the error message OUTSIDE of where the actual validation rule (username_check_exist) is defined.
In your auth:

PHP Code:
public function username_check($username) {
 
   $account $this->load->database('account'TRUE);
 
   $account->where('login',$username);
 
   $query $account->get('account');
 
   return ($query->num_rows() > 0);  //returns TRUE/FALSE


In your controller:

PHP Code:
public function username_check_exist($username) {
 
 //set the error message for THIS validation rule
 
 $this->form_validation->set_message('username_check_exist''L´username è già utilizzato.');

 
 //just return result from auth::username_check(), which will be true/false
 
 return $this->auth->username_check($username);



I change the auth and controller with your code, meanwhile, I thank you for your time that you're offering me
And now I can tell you that is not working. Big Grin It does not display the error and still launches the query  Confused

As absurd thing. All other rules of the same field are used properly
Pages: 1 2