How would you display message/errors/notices to the user? |
[eluser]shreder[/eluser]
Hey. I'm in the middle of developing my first app using CI 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!
[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 For something more complicated I would set this information in the library then return that object for the controller to process: Code: //library
[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!
[eluser]bretticus[/eluser]
CI does have an entire form validation class at your disposal too.
[eluser]shreder[/eluser]
[quote author="bretticus" date="1240350297"]CI does have an entire form validation class at your disposal too. [/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?
[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.
[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!
[eluser]simshaun[/eluser]
Sorry. It seems in my reply earlier I was not thinking clearly. I meant form_error(), not show_error()
[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
[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'); 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')); Please advise. Thanks in advance! |
Welcome Guest, Not a member yet? Register Sign In |