Undocumented use of $this->validation->error_string - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Undocumented use of $this->validation->error_string (/showthread.php?tid=10608) |
Undocumented use of $this->validation->error_string - El Forum - 08-06-2008 [eluser]Bramme[/eluser] After using CI for about 4 months or so, today I figured something silly out and I wished I learned about it a lot earlier. When working with forms and validation, most of the time, I just return to the original form (when the form is succesfully submitted) but display an additional line. Code: if ($this->db->insert()) $msg = 'success!'; Code: <form action="/admin/news/new_item" method="post"> Today I realised, since CI is also php 4 compatible there's no difference between public and private vars (or functions for that matter), so I could perfectly well use Code: if ($this->db->insert()) $this->validation->error_string = 'success!'; This means I don't have to add that extra lines in my view, which is a lot cleaner and the whole practice is imo semantically correcter. The only thing I'm annoyed by now is the error_string term... I'd rather see smth like feedback_string, but that's only a small thing. I just wanted to post this and hope it would get documented, that way other people might be helped earlier by it. Undocumented use of $this->validation->error_string - El Forum - 08-06-2008 [eluser]Jonathon Hill[/eluser] I've used a similar technique for setting default form values. Code: $this->validation->item = 'something'; // sets a default value If the user enters something in that field then it will replace your default value on $this->validation->run(). My view file looks like this: Code: <?=form_input('item', $this->validation->item);?> That is a creative technique you're using, but there is a better way using flashdata notifications. One problem with your method is, what if you want to have different styling for success messages vs. error messages? Undocumented use of $this->validation->error_string - El Forum - 08-06-2008 [eluser]xwero[/eluser] Bramme why not do this in your view Code: <?php if(! empty($this->validation->error_string)): echo $this->validation->error_string; else: ?><p>success message</p><?php endif ?> If you don't like the code in your view you can create a helper (validation_helper.php) Code: function message($no_error = '') Undocumented use of $this->validation->error_string - El Forum - 08-06-2008 [eluser]Jonathon Hill[/eluser] @xwero: I think the helper idea is what Michael Wales does in his tutorial (http://michaelwales.com/tutorials/easy-flashdata-notifications/). Undocumented use of $this->validation->error_string - El Forum - 08-06-2008 [eluser]xwero[/eluser] Yes but it uses a session functionality namely flashdata, hence the title of the article. My helper can accept custom messages and flashdata. If you look at the helper code you see it returns a div with a class which is limiting too. What if you want another tag. And if you are a html purist you get a fit if the div only contains text Undocumented use of $this->validation->error_string - El Forum - 08-07-2008 [eluser]Bramme[/eluser] I think I'm gonna go with Michael's solution. I like the idea of redirecting after a succesfully submitted form... That way you don't get the irritating "resend post" warning when refreshing and it feels (for me) like a clean start! Undocumented use of $this->validation->error_string - El Forum - 08-07-2008 [eluser]Hannes Nevalainen[/eluser] The get rid of the “resend post” warning redirect the user to the same controller again (after eventual processing of post data). Undocumented use of $this->validation->error_string - El Forum - 08-07-2008 [eluser]Michael Wales[/eluser] The code within that tutorial is a decent start but by no means ready for a live project - I wouldn't use a helper as limiting as that within any of my projects. That's a tutorial, whose goal is to teach you how to use flashdata and how to pull that functionality out into a helper. Nothing more than a tutorial. |