CodeIgniter Forums

Full Version: Manual form validation error message
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]veledrom[/eluser]
Hi,

Form validation of username and password works fine. After validation I check username and password against database. If it fails then it should display an error "Invalid username and password" in page but I cannot do it? Validation errors shows up fine when validation fails but, how do I manually set an error after validation?

Thanks

Code:
$this->load->library('form_validation');
$this->form_validation->set_rules('text_username', 'Username', 'trim|required');
$this->form_validation->set_rules('text_password', 'Password', 'trim|required');

if($this->form_validation->run() != true)
{
  $this->load->view('login_view');
}

$username = $this->input->post('text_username', true);
$password = $this->input->post('text_password', true);

if($username != 'admin' || $password != 'admin')
{
  //HOW DO I SET MY MANUAL ERROR HERE SO IT WOULD SHOW UP IN PAGE
  $this->load->view('login_view');
}

El Forum

[eluser]zoreli[/eluser]
Hi

Try this...

Quote: //HOW DO I SET MY MANUAL ERROR HERE SO IT WOULD SHOW UP IN PAGE

$data = "Your error message come here"
$this->load->view('login_view', $data);

El Forum

[eluser]veledrom[/eluser]
I can do that but I'm more interested in use of validation library.

I've done something and it works but is it good practise though?

Code:
public function invalid_login()
{
   $this->form_validation->set_message('invalid_login', 'Invalid username and password');
   return false;
}

Code:
if($username != 'admin' || $password != 'admin')
{
  $this->form_validation->set_rules('null', 'null', 'callback_invalid_login');
  $this->form_validation->run();
  $this->load->view('login_view');
}

El Forum

[eluser]zoreli[/eluser]
Hi

I can not see any reason why this practice is bad. My preference is to keep things simple where applicable. Of course you know your reasons why would you like to go with validation library here.

Regards, Zoreli

El Forum

[eluser]Mauricio de Abreu Antunes[/eluser]
Are you using echo for validation_errors() in your view?

El Forum

[eluser]veledrom[/eluser]
[quote author="Mauricio de Abreu Antunes" date="1330693924"]Are you using echo for validation_errors() in your view?[/quote]

YES

Code:
$errors = validation_errors();
echo ($errors != "") ? $errors . '<br />' : null;

El Forum

[eluser]Mauricio de Abreu Antunes[/eluser]
"Validation errors shows up fine when validation fails but, how do I manually set an error after validation?"

Do you mean "message error"?

El Forum

[eluser]Matalina[/eluser]
You can use callback functions in the validation rule and then it will work with the form_validation class like you want.

Code:
$this->form_validation->set_rules('username','Username','trim|required|callback__invalid_login');
Code:
function _invalid_login($username) {
$password = trim($this->input->post('password',TRUE));
// validate from database below
}

El Forum

[eluser]johnpeace[/eluser]
Yah, I would suggest you just use a callback to check the username and password.

Sounds like you're hand-rolling an auth solution, which I can't imagine is an effective use of your time given the number of excellent options already out there...

El Forum

[eluser]veledrom[/eluser]
[quote author="johnpeace" date="1330698307"]Yah, I would suggest you just use a callback to check the username and password.

Sounds like you're hand-rolling an auth solution, which I can't imagine is an effective use of your time given the number of excellent options already out there...[/quote]


Ok please give me an example for full all-in-one (login, authentication, validation, page validation etc) library. Which one shall use?
Pages: 1 2