Welcome Guest, Not a member yet? Register   Sign In
How would you display message/errors/notices to the user?
#1

[eluser]shreder[/eluser]
Hey.

I'm in the middle of developing my first app using CI Smile
I have created my own login library and was wondering how you guys pass errors/messages from the libraries to the views?

Say I want to show the user an error in case his password doesn't match, how would I do it?
I do not want to use the show_error() function as I want my errors to display on the same view as the form.

Thanks!
#2

[eluser]Mike Ryan[/eluser]
Hi,

For something simple like login (it either worked or it didn't) I just use the library's return value in the controller:
Code:
//controller
if (!$this->auth->do_login($user,$pass))
{
$data['error_message'] = "Login failed...";
$this->load->view('login_form',$data);
}

For something more complicated I would set this information in the library then return that object for the controller to process:
Code:
//library
if ($something_went_wrong)
{
  $result['error_code'] = '42';
  $result['error_text'] = 'Invalid format in TPS report';
  return $result;
}
#3

[eluser]shreder[/eluser]
Thanks a lot Grad! I will certainly look into this solution.

I just found the flashdata function.
Do you recommend using flashdata?

How would I pass few errors to flashdata and then display them all on a view?

Thanks!
#4

[eluser]bretticus[/eluser]
CI does have an entire form validation class at your disposal too. Smile
#5

[eluser]shreder[/eluser]
[quote author="bretticus" date="1240350297"]CI does have an entire form validation class at your disposal too. Smile[/quote]

Hey.

I'm already using the validation class but isn't it only for forms?
Can I pass generate errors not related to forms?
#6

[eluser]simshaun[/eluser]
What is keeping you from using show_error() on the same page as your form? I do it all the time.

Edit (realized I didnt really answer your question):
I use session flash data var called "message" for a one-time error message. On my form, I see if the var is set, and display it if so.

For multiple error messages, I just store them in an array, then store the array in the flashdata.
#7

[eluser]shreder[/eluser]
[quote author="simshaun" date="1240357009"]What is keeping you from using show_error() on the same page as your form? I do it all the time.

Edit (realized I didnt really answer your question):
I use session flash data var called "message" for a one-time error message. On my form, I see if the var is set, and display it if so.

For multiple error messages, I just store them in an array, then store the array in the flashdata.[/quote]

How would you use the show_error() on the same page as the form?

Thanks!
#8

[eluser]simshaun[/eluser]
Sorry. It seems in my reply earlier I was not thinking clearly. I meant form_error(), not show_error()
#9

[eluser]Mike Ryan[/eluser]
Flashdata is used to store information between requests. Think of it as a session variable that only lasts for one page request. If the error occurs and you wish to immediately communicate this to the user, just pass the $errors array to your view:

Code:
//controller
$data['errors'] = array('error_msg1','error_msg2');
$this->load->view('viewfile',$data);

//view
foreach ($errors as $error)
{
echo $error;
}
#10

[eluser]shreder[/eluser]
I have decided to use flashdata() to display errors.

I ahve the following code in my controller:
Code:
$this->session->set_flashdata('notice', 'You have not selected a domain');
$this->session->set_flashdata('notice', 'You have not selected an extension');

As you can see, I want to display 2 errors.
How can you show these 2 errors inside a view?

I have tried this but it does not work:

Code:
$errors = array($this->session->flashdata('notice'));
foreach ($errors as $row)
{
echo $row."<br />";
}

Please advise.

Thanks in advance!




Theme © iAndrew 2016 - Forum software by © MyBB