Welcome Guest, Not a member yet? Register   Sign In
SOLVED: form validation set_message() going nuts "Unable to access an error message corresponding to your field name." e
#1

[eluser]atno[/eluser]
controllers/welcome.php

Code:
public function index()
    {
            $this->load->library('form_validation');

            $val = $this->form_validation;
            $val->set_error_delimiters('<div class="error">', '</div>');
            $val->set_rules('char','character name','trim|required|alpha_numeric|callback_char_check');
            $val->set_rules('pass','password','trim|required|alpha_numeric|min_length[5]|max_length[10]|callback_pass_check');
            
            if($val->run() == FALSE){
                $val->set_message('char_check','Not a registered charname.');
            }
            
            $data['main_content'] = 'welcome_message';
            $this->load->view('includes/template',$data);
    }

        
        function char_check() {
            $char = $this->input->post('char');
            $query = $this->db->get_where('characters', array('characterName' => $char), '1');
            echo $query->num_rows();

            if($query->num_rows() == 0){
                return false;
            }

            return true;
        }

views/welcome_message.php

Code:
&lt;?php echo form_open(' ','id="login"');?&gt;
            character name<br />
            &lt;input type="text" name="char" id="char"&gt;&lt;br />
            &lt;?php echo form_error('char');?&gt;
            password<br />
            &lt;input type="password" name="pass" id="pass"&gt;&lt;br />
            &lt;?php echo form_error('pass');?&gt;
            &lt;input type="submit" value="login" name="log"&gt;&lt;br />
            <span style="font-size:11px;">
                &lt;?php echo anchor('register/get_chars','register'); ?&gt;
                <a href="rpass">retrieve password</a>
            </span>
        &lt;/form&gt;

So when i just click the submit button i normally get the errors "The character name field is required" and "The password field is required.". But when i enter a name on character name and character name doesnt exists on database i get "Unable to access an error message corresponding to your field name."

I tried to add [code]&lt;?php echo form_error('char_check');?&gt; on the view but still the same.

What the hell am i doing wrong.

Cheers
#2

[eluser]boltsabre[/eluser]
Have you tried setting the error message for char_check inside that function rather than in your index function? May not be the problem, it just seems a little weird to me to separate your custom validation code across two functions.
#3

[eluser]atno[/eluser]
[quote author="boltsabre" date="1305141141"]Have you tried setting the error message for char_check inside that function rather than in your index function? May not be the problem, it just seems a little weird to me to separate your custom validation code across two functions.[/quote]
yes i've tried that and getting the following error as $val is declared on index();
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: val
#4

[eluser]atno[/eluser]
bah, i moved
Code:
$val->set_message('char_check','You doent exist :). You might want to register first.');
under
Code:
set_rules
and it's working now :S

Code:
$val = $this->form_validation;
            $val->set_error_delimiters('<div class="error">', '</div>');
            $val->set_rules('char','character name','trim|required|alpha_numeric|callback_char_check');
            $val->set_rules('pass','password','trim|required|alpha_numeric|min_length[5]|max_length[10]|callback_pass_check');
            $val->set_message('char_check','You doent exist :). You might want to register first.');
#5

[eluser]boltsabre[/eluser]
Good good!!!




Theme © iAndrew 2016 - Forum software by © MyBB