Welcome Guest, Not a member yet? Register   Sign In
Setting error messages in callback functions only works on PHP5
#1

[eluser]Philipp Gérard[/eluser]
Hey there,

I encountered a bug when working locally under MAMPHP4 and remotely on a LAMPHP5. The following code works on the remote machine perfect, the local machine however can't validate the "login" field and says "Unable to access an error message corresponding to your field name." instead of - my german equivialent of "your login is too short" etc. If the chosen login, however, is okay, it will not return an error. It though seems to me, that your code has trouble setting error messages inside a callback function under PHP4.

Code:
function register()
    {
        if(is_logged_in())
        {
            redirect(index_page());
        }
        $this->template->set_pagetitle(array($this->lang->line('authentication_register')));
        // Set validation rules
        $this->form_validation->set_rules('login', 'Benutzername', 'callback_check_login');
        $this->form_validation->set_rules('password', 'Passwort', 'trim|required|xss_clean|matches[password2]|min_length[8]');
        $this->form_validation->set_rules('password2', 'Passwort (bestätigen)', 'trim|required|xss_clean|matches[password]|min_length[8]');
        $this->form_validation->set_rules('agb2', 'Nutzungsbedingungen', 'trim|required|xss_clean');
        // Chance some error messages
        $this->form_validation->set_message('matches', 'Die beiden Passwörter müssen übereinstimmen.');
        // Check if form validates
        if($this->form_validation->run())
        {
            $id = $this->authentication->create($this->input->post('login', TRUE), $this->input->post('password', TRUE));
            if($id != FALSE)
            {
                $this->session->set_flashdata('notice', $this->lang->line('authentication_register_success'));
                redirect('/user/login');
            }
            else
            {
                $this->session->set_flashdata('notice', $this->lang->line('authentication_register_error'));
                redirect('/user/register');
            }
        }
        else
        {
            $this->template->load('main', 'user/register');
        }
    }
    
    function check_login($str)
    {
        $str = trim($str);
        $str = xss_clean($str);
        if($str == '')
        {
            $this->form_validation->set_message('check_login', 'Das Feld Benutzername ist ein Pflichtfeld.');
            return FALSE;
        }
        elseif(strlen($str) < 5)
        {
            $this->form_validation->set_message('check_login', 'Dieser Benutzername ist zu kurz.');
            return FALSE;
        }
        elseif(strlen($str) > 15)
        {
            $this->form_validation->set_message('check_login', 'Dieser Benutzername ist zu lang.');
            return FALSE;
        }
        else
        {
            // Now lets check if the username is already taken
            $this->db->where($this->config->item('identifier'), $str);
            $query = $this->db->getwhere($this->config->item('accounts_table_name'));
            if($query->num_rows() > 0)
            {
                $this->form_validation->set_message('check_login', 'Dieser Benutzername ist bereits vergeben.');
                return FALSE;
            }
            else
            {
                return $str;
            }
        }
    }
#2

[eluser]Unknown[/eluser]
Yes I've noticed this problem with CI and php4 what you need to do is
within /system/language/german/form_validation_lang.php

file add this line

$lang['check_login'] = "'Dieser Benutzername ist bereits vergeben.";
#3

[eluser]Philipp Gérard[/eluser]
Well, thanks for that hint. But this breaks the functionality of my callback function that is not only used to validate the value of a given input but also to dynamically set the error message returned depending on the given input (too short, too long, already taken, ...). Simply setting one fixed value won't work in this case.




Theme © iAndrew 2016 - Forum software by © MyBB