CodeIgniter Forums

Full Version: Can't set customer form error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I want to set a customer error message.  Can anyone tell me why this doesn't it appear?

Thanks,

James



PHP Code:
        $this->form_validation->set_rules('username''Username''trim|required|xss_clean');
        $this->form_validation->set_rules('password''Password''trim|required|xss_clean');


        if ($this->form_validation->run() == FALSE) {

            $this->load->view('register/header');
            $this->load->view('register/researcher_form');
            $this->load->view('footer');

        } else {

            $username $this->input->post('username'TRUE);
            $password $this->input->post('password'TRUE);

            // Check the username and password
            $researcher_id $this->user->researcher_login($username$password);

            if ($researcher_id == TRUE){

                redirect("/register/study/");

            } else {

                $this->form_validation->set_message('login failed''Your username and password may be wrong.');
                
                $this
->load->view('register/header');
                $this->load->view('register/researcher_form');
                $this->load->view('footer');

            }

        }

    
(03-07-2018, 01:52 PM)randomhacks Wrote: [ -> ]Hello,

I want to set a customer error message.  Can anyone tell me why this doesn't it appear?

Thanks,

James



PHP Code:
        $this->form_validation->set_rules('username''Username''trim|required|xss_clean');
        $this->form_validation->set_rules('password''Password''trim|required|xss_clean');


        if ($this->form_validation->run() == FALSE) {

            $this->load->view('register/header');
            $this->load->view('register/researcher_form');
            $this->load->view('footer');

        } else {

            $username $this->input->post('username'TRUE);
            $password $this->input->post('password'TRUE);

            // Check the username and password
            $researcher_id $this->user->researcher_login($username$password);

            if ($researcher_id == TRUE){

                redirect("/register/study/");

            } else {

                $this->form_validation->set_message('login failed''Your username and password may be wrong.');
                
                $this
->load->view('register/header');
                $this->load->view('register/researcher_form');
                $this->load->view('footer');

            }

        }

    

Just a quick look and im no expert

but "$this->form_validation->set_message" I use when creating a custom validation rule?

to see the error messages on the form after submit is thats what you looking for
i do this in my controller / model

$this->data[ 'submiterrorms' ] = validation_errors( '<p class="error_msg">', '</p>' );

and then have tis in my view

<?php if (! empty($submiterrorms)) { ?>
<?php echo $submiterrorms; ?>
<?php } ?>


or you can just use

<?php echo validation_errors(); ?>
You need to validate the login with a callback.
https://www.codeigniter.com/user_guide/l...on-methods

You can modify mine if you want.

PHP Code:
public function login()
{
    
$this->load->helper('form');
    
$this->load->library('form_validation');
    
    
$validation_rules = array(
        array(
            
'field' => 'username',
            
'label' => lang('login_email'),
            
'rules' => array(
                
'trim',
                
'valid_email'
            
)
        ),
        array(
            
'field' => 'password',
            
'label' => lang('login_password'),
            
'rules' => array(
                
'trim',
                
'required',
                array(
'validate_auth', array( $this'_validate_auth' ) )
            )
        )
    );
    
    
$this->form_validation->set_rules$validation_rules );
    
    if( 
$this->form_validation->run() === TRUE )
    {
        return 
TRUE;
    }
    return 
FALSE;
}

// --------------------------------------------------------------

public function _validate_auth()
{
    
$user_email        $this->input->post('username');
    
$user_password    $this->input->post('password');
    
    if( empty(
$user_email) OR empty($user_password) )
    {
        
$this->form_validation->set_message('validate_auth'lang('error_missing_fields'));                
        return 
FALSE;
    }
    
    if( 
$auth_data $this->auth_model->get_auth_data$user_email ) )
    {
        if( 
$auth_data->banned === '1' )
        {
            
$this->form_validation->set_message('validate_auth'lang('error_username_password'));
            return 
FALSE;
        }
        if( ! 
$this->check_password$auth_data->passwd$user_password ) )
        {
            
$this->form_validation->set_message('validate_auth'lang('error_username_password'));
        }
        else
        {
            
// Setup redirection if redirect required
            
$this->redirect_after_login();
                    
            
// Set session cookie and remember me
            
$this->maintain_state$auth_data );
            
            
// Send the auth data back to the controller
            
return TRUE;
        }
    }
    else
    {
        
$this->form_validation->set_message('validate_auth'lang('error_username_password'));
    }
    
    return 
FALSE;