Welcome Guest, Not a member yet? Register   Sign In
Undocumented use of $this->validation->error_string
#1

[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!';
My views often looked like this then
Code:
<form action="/admin/news/new_item" method="post">
<?=$this->validation->error_string?>
<? if (isset($msg)) echo $msg; ?>

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.
#2

[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($this->validation->run()) {
...
}

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?
#3

[eluser]xwero[/eluser]
Bramme why not do this in your view
Code:
&lt;?php if(! empty($this->validation->error_string)): echo $this->validation->error_string; else: ?&gt;<p>success message</p>&lt;?php endif ?&gt;
The validation->error_string is a marked up string but the success string isn't in your example. You could mark it up in php like the error_string but then the (future) designers can't change it without touching the php code. If there is only one success message on your form you can hard code it like i did in the snippet.

If you don't like the code in your view you can create a helper (validation_helper.php)
Code:
function message($no_error = '')
{
   if( isset($this->validation->error_string) && ! empty($this->validation->error_string))
   {
      return $this->validation->error_string;
   }
   else
   {
      return $no_error;
   }
}
This allows you to add any message you want to display and as a bonus the dependence of the validation class is obscured, which is a good thing if you want to reuse the view in non CI applications.
#4

[eluser]Jonathon Hill[/eluser]
@xwero: I think the helper idea is what Michael Wales does in his tutorial (http://michaelwales.com/tutorials/easy-f...fications/).
#5

[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 Wink
#6

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

[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).
#8

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




Theme © iAndrew 2016 - Forum software by © MyBB