Welcome Guest, Not a member yet? Register   Sign In
Crazy with callbacks
#1

(This post was last modified: 06-10-2015, 05:48 AM by StratoKyke.)

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.
Reply
#2

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
Reply
#3

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.
Reply
#4

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.
Reply
#5

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.
Reply
#6

(This post was last modified: 06-10-2015, 06:47 AM by StratoKyke.)

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.
Reply
#7

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"
Reply
#8

(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?
Reply
#9

(This post was last modified: 06-10-2015, 07:02 AM by CroNiX.)

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);

Reply
#10

(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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB