Notices helper |
[eluser]Bramme[/eluser]
Assume you have the typical form processing structure in your controller that comes with the validation library, stuff like this: Code: if ($this->validation->run() != FALSE) To do this with as less code as possible in my views and controller, I wrote a little helper that handles success and error messages (and redirects) but also handles validation->error_string without redirecting. notices_helper.php: Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); Code: if ($this->validation->run() != FALSE) And in your view you simply put Code: <?php echo get_notice(); ?> I'm setting up a sandbox CI application so I can show a demo, I'll add that in a bit and let you guys know.
[eluser]xwero[/eluser]
improvements - instead of the send_notice accepting an are it would be better if it accepted the class and the message removing the need to create an array. Code: function send_notice($class, $message, $redirect = '') - the example use of the send_notice would be better as follows, using my modification Code: if ($this->validation->run() == TRUE) - the get_notice function should be language neutral or if you want to add a string use a language file. - as mentioned in the thread that got you to create this helper i mentioned adding a div is limiting but you limit it even further by using a list which assumes the validation error strings are in a li tag. And who says the other messages are only strings? What if a developer adds a marked up message? Or he wants to attach an id to the container? Limiting the developer and the designer is not a good idea. What you could do is to create a custom element function Code: function tag($name,$attributes='',$content = '')
[eluser]Bramme[/eluser]
Thanks for the feedback xwero. I agree the helper is quite limiting, but I like it like that. This is for my own use and I will probably always use a structure like this. I'm going to implement some off your improvements but certainly not all. I posted this code mostly to set others on their way to create their own set of helpers. Though I agree it's not a bad idea to make it less limiting.
[eluser]ontguy[/eluser]
Take a look at the Messages library/helper http://codeigniter.com/wiki/Messages/. There maybe some overlap in that helper and what you've developed. |
Welcome Guest, Not a member yet? Register Sign In |