Welcome Guest, Not a member yet? Register   Sign In
Using language file for custom form validation
#1

[eluser]neomech[/eluser]
I have a callback function that I'm using for user registration that checks if the answer to a math question is correct. It works fine and I'm able to assign a simple message to pop up if the answer is correct. The code is here:
Code:
function check_answer()
    {
        $user_answer = $this->input->post('math_question');
        $correct_answer = $this->encrypt->decode($this->input->post('antispam'));

        if($user_answer != $correct_answer)
        {
                $this->form_validation->set_message('check_answer', 'Your answer to the math question was incorrect');
                return FALSE;
        } else {
                return TRUE;
        }
    }


What I don't know how to do is get it to work with the form validation language files so that I can easily have multiple languages for this particular error message.

I added a line to the english form_validation_lang.php file, like so:
Code:
$lang['check_answer']      = "Your answer to the math question was incorrect.";

Then I changed the one line from my callback function above to this:
Code:
$this->form_validation->set_message('check_answer');
Based on my reading of the user guide, I expected this should work, but it doesn't. Any advice on how to make callback function error messages properly localized? I don't understand how I'm supposed to handle multiple languages with the callback approach.

Thanks!
#2

[eluser]pkrstic[/eluser]
Code:
$this->form_validation->set_message(__FUNCTION__, lang('check_answer'));

Try this way, if __FUNCTION fails, use regular method with string (as you used in first example)




Theme © iAndrew 2016 - Forum software by © MyBB