Welcome Guest, Not a member yet? Register   Sign In
Check if textarea is empty, won't work
#1

[eluser]Fredrik-s[/eluser]
Hello,

This is my third day using CodeIgniter and falling in love with it more and more.

I have run into a problem though. I wan't to check so a textarea have anything written in it. If not I will output a message through a flash "function", if it has any thing written in it I will output a message saying everything is good also through a flash "function".

My controller look like this:
Code:
function add_domains(){
        $this->form_validation->set_rules('domains', 'Domains', 'trim|required|xss_clean');
        
        if (!$this->form_validation->run())
        {
            $this->flash->add('You have to write something','valid');
        }
        else
        {
            $this->flash->add('You HAVE wrote something, good!','valid');
}
}

My view looks like this:
Code:
<?PHP
$domains = array(
              'name'        => 'domains',
              'id'          => 'domains',
              'value'       => '',
              'cols'           => '50',
              'rows'        => '30',
            );
?>
<?PHP
echo $this->flash->get();

?>

<?php echo form_open($this->uri->uri_string()); ?>
<?php echo form_textarea($domains); ?>
<?php echo form_error($domains['name']); ?><?php echo isset($errors[$$domains['name']])?$errors[$domains['name']]:''; ?>
&lt;?php echo "<br />"; ?&gt;
&lt;?php echo form_submit('submit', 'Lägg till sidor/domäner'); ?&gt;
&lt;?php echo form_close(); ?&gt;

The thing is that it recognize when something is written and output the following "You HAVE wrote something, good!". But it doesn't recognize when there is NOT anything written. When I haven't written anything pressing the submit button the page just reload without any error message being displayed.

How come the error message won't show up?
#2

[eluser]oppenheimer[/eluser]
If I'm not mistaken, flash messages show up on the next page you load, not the current page.

So the success message you are seeing was actually from loading the form. I think flash messages won't work well for this. You can just use a regular variable for your errors.
#3

[eluser]InsiteFX[/eluser]
It's flashdata not just flash!
Code:
// To add flashdata:
$this->session->set_flashdata('item', 'value');
// To read a flashdata variable:
$this->session->flashdata('item');
// If you find that you need to preserve a flashdata variable through an additional request,
// you can do so using the keep_flashdata() function.
$this->session->keep_flashdata('item');

InsiteFX
#4

[eluser]Fredrik-s[/eluser]
Thanks for replies!

I'm using this library http://forrst.com/posts/Flashdata_with_e...gniter-0Dw which make me use the syntax presented in my code above.

oppenheimer: I don't think the problem is that showing on the next page view. Because a new page view is generated and the one of the messages is coming through, which is "You HAVE wrote something, good!"

Any other suggestions on what the problem could be?
#5

[eluser]InsiteFX[/eluser]
Did you try to echo a message to make sure you are getting there?

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB