[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;
}
}
}