Welcome Guest, Not a member yet? Register   Sign In
Manual form validation error message
#1

[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');
}
#2

[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);
#3

[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');
}
#4

[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
#5

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

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

[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"?

#8

[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
}
#9

[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...
#10

[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?




Theme © iAndrew 2016 - Forum software by © MyBB