Welcome Guest, Not a member yet? Register   Sign In
help me about form validation callback function
#1

[eluser]ali oygur[/eluser]
hi,

Sorry, im little speak english my from turkey.

i have a problem about form_validation callback.

form validation callback function not work in my class.

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
* @Package    : Gazale kimlik doğrulandırma kütüphanesi (gazale_auth)
* @Author     : Ali OYGUR
* @Link       : www.adagrafik.com
* @Version    : 1.0.0
* Last update : 17.05.2010
*/

class Gazale_auth{
    
    var $CI;
    
    function __construct()
    {
        $this->CI =& get_instance();
        $this->CI->load->library('session');
        $this->CI->load->library('form_validation');
       // $this->CI->load->helper('url');
        $this->CI->load->database();
       // $this->CI->load->helper('email');
       // $this->CI->load->helper('string');
    }
    
    
    /*
     * YENİ ÜYE KAYDI
     */
    function new_user()
    {
    
        # Kurallara uygunluk kontrolü
        $this->CI->form_validation->set_rules('username', 'Kullanıcı adı', 'trim|required|min_length[3]|max_length[150]|htmlspecialchars|callback__username_exists');
        $this->CI->form_validation->set_rules('password', 'Şifre', 'trim|required|min_length[4]|matches[password2]|md5');
        $this->CI->form_validation->set_rules('password2', 'Şifre tekrarı', 'trim|required');
        $this->CI->form_validation->set_rules('email', 'E mail', 'trim|required|htmlspecialchars|valid_email|callback__email_exists');        
        
        if($this->CI->form_validation->run() === FALSE):
            
            $returns = array('error' => TRUE, 'msg' => $this->CI->form_validation->error_string());
            
        else:
            
            $table = 'user';
            $data  = array
            (
                'id'         => NULL,
                'username'   => $this->CI->input->post('username'),
                'password'   => $this->CI->input->post('password'),
                'email'      => $this->CI->input->post('email'),
                'last_visit' => date('Y-m-d H:i:s'),
                'avatar'     => 'avatar_yok.gif',
                'date'       => date('Y-m-d H:i:s')
            );
            
            $insert = $this->CI->db->insert('user', $data);
        
            
            if($insert):
                $returns = array('error' => FALSE);
                $this->CI->session->set_flashdata('status', 'Üyelik işleminiz başarı ile tamamlanmıştır. Şimdi aşağdaki forumdan giriş yapabilirsiniz');
            else:
                $returns = array('error' => TRUE, 'msg' => 'Kayıt esnasında bir problem oluştu. lütfen bunu '.$this->CI->config->item('mh_mail').' adresine bildirin.');
            endif;        
            
        endif;
        
        return $returns;
        
    }
    
    
    /*
     * ÜYE BİLGİLERİ
     * herhangi bir üye id verilmezse sessionda kayıtlı üye id 'ye ait üye bilgilerini çeker.
     * verilirse verilen üye id ye ait bilgileri çeker.
     */
     function get_userdata($user_id = NULL)
     {
        if(is_null($user_id)) $user_id = $this->CI->session->userdata('user_id');

        $query = $this->CI->db->get_where('user', array('id' => $user_id));
        
        $user_data = $query->row();
        
        return $user_data;
     }
    /*
     * //ÜYE BİLGİLERİ
     */
    
    /*
     * ORTAK FONKSİYONLAR
     */
    # Sistemde bu isimle kayıtlı herhangi bir üye olup olmadığına bakılıyor
    function _username_exists($username)
    {
        
        $table = 'user';
        $where = array('username' => $username);
        
        $query = $this->CI->db->get_where($table, $where);
        
        if($query->num_rows() > 0):
            $return = false;
            $this->CI->form_validation->set_message('_check_username', 'Bu kullanıcı adı sistemde zaten kayıtlı');
        else:
            $return = true;
        endif;
        
        return $return;
                
    }

but this code working in controller file.

Please help me Sad
#2

[eluser]LuckyFella73[/eluser]
Hi ali oygur

Looks like you put all/most of the code usually found in a controller/ model
into a library and load that stuff into your controller. I recommend you to
organize your code better to make everything much easier! Setting up a new
library makes sense if you build a class and functions you don't have yet.
Your library is just a mix of libraries allready existing, what is making
everything more complicated then necessary.

Would be better if you place the rules (including the callback function) inside
your controller and set up one model with the database functions.
#3

[eluser]ali oygur[/eluser]
[quote author="LuckyFella73" date="1274127050"]Hi ali oygur

Looks like you put all/most of the code usually found in a controller/ model
into a library and load that stuff into your controller. I recommend you to
organize your code better to make everything much easier! Setting up a new
library makes sense if you build a class and functions you don't have yet.
Your library is just a mix of libraries allready existing, what is making
everything more complicated then necessary.

Would be better if you place the rules (including the callback function) inside
your controller and set up one model with the database functions.[/quote]

@luckyfella73

very thanx for your help.

i will do your say. but One way to do it yet?

So, not work in other class (my class, my librariy) callback function ?

note : im sorry for my english
#4

[eluser]WanWizard[/eluser]
Form validation looks for the callback method only in the loaded controller, so in your case $this->CI->_username_exists() must exist for the callback to function.

You have a few options:

If this validation code is specific to this controller, put the callback code back into your controller.
If you want to reuse the validation code in a few other controllers, put the code in a library or model, then create a callback method in your controller that calls the model/library method.
If you want to use the validation code everywhere, extend the form validation library and put the method in your extension, so you can use it like any other rule (without the callback prefix).
#5

[eluser]LuckyFella73[/eluser]
What exactly does not work? Do you get any error messages?
If yes please post along with your controller code.
I don't know if I'll be able to help you but I can at least have a look at it
and try.
#6

[eluser]ali oygur[/eluser]
Okey.

thanx for your help WanWizard
#7

[eluser]WanWizard[/eluser]
@luckyfella73,

You don't get an error message.
If $this->CI->your_callmethod_method() does not exist, the form validation library simply skips your rule.
#8

[eluser]LuckyFella73[/eluser]
Ah ok - good to know - thanks!




Theme © iAndrew 2016 - Forum software by © MyBB