• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Messages showing at the wrong time

#1
[eluser]Mithun[/eluser]
Hi I use Message and Template libraries.

I have a message_area in my template section
views/template/default.php
Code:
<div id="main">
                <div id="side_bar">
                    &lt;?php echo $side_bar; ?&gt;
                </div>
                <div id="content">
                    &lt;?php echo $message_area; ?&gt;
                    &lt;?php echo $search_area; ?&gt;
                    &lt;?php echo $content; ?&gt;
                </div>
            </div>
views/template/message_area.php
Code:
<div id="messages">
&lt;?php
// display all messages
if (is_array($messages)):
    foreach ($messages as $type => $msgs):
        if (count($msgs > 0)):
            foreach ($msgs as $message):
                echo ('<span class="' .  $type .'">' . $message . '</span>');
           endforeach;
       endif;
    endforeach;
endif;
?&gt;
</div>
and i use auto load to load Message and Template libraries
Code:
$autoload['libraries'] = array('database', 'session', 'template', 'DX_Auth', 'messages', 'Form_validation', 'pagination');

and here is my MY_Controller section
&lt;?php
Code:
class MY_Controller extends Controller {

    function MY_Controller()
    {
        parent::Controller();
        if($this->dx_auth->is_logged_in())
        {
            $this->template->set_template('default');
            $data['messages']= $this->messages->get();
            $this->template->write_view('message_area','templates/message_area', $data);
            
        }
   }

}


Problem is that when ever i set an message say E1 after form validation fails, the message E1 is not displayed in the form repopulate page for the first time, but error messages for individual form elements are displayed. And submitting the form again will bring the error message E1 to the screen which is outdated.

Code:
&lt;?php
class Locations extends MY_Controller {
    
    function Locations()
    {
        parent::MY_Controller();
    }
    
function add()
    {
        $data = array();
        $data['user'] = array();
        if(empty($_POST))
        {
            $this->db->select('users.id, users.username');
            $this->db->order_by("users.username", "ASC");
            $users = $this->db->get('users');
            foreach($users->result() as $user)
            {
                $data['users'][$user->id] = $user->username;
            }
            $this->template->write_view('content','locations/add',$data);
        }
        else
        {
            $this->form_validation->set_rules('name', 'Name', 'trim|required|min_length[1]|max_length[20]|xss_clean');
            $this->form_validation->set_rules('assigned_id', 'Assigned User', 'trim');
            $this->form_validation->set_rules('address', 'Address', 'trim');
            
            if ($this->form_validation->run())
            {
                $insert_data['name'] = $_POST['name'];
                $insert_data['address'] = $_POST['address'];
                $insert_data['assigned_id'] = $_POST['assigned_id'];
                if($this->db->insert('locations', $insert_data))
                {
                    $this->messages->add('Location details successfully added.', 'success');
                    redirect('/locations/index/');
                }
                else
                {
                    $data['name'] = $_POST['name'];
                    $data['address'] = $_POST['address'];
                    $data['assigned_id'] = $_POST['assigned_id'];
                    $this->db->select('users.id, users.username');
                    $this->db->order_by("users.username", "ASC");
                    $users = $this->db->get('users');
                    foreach($users->result() as $user)
                    {
                        $data['users'][$user->id] = $user->username;
                    }
                    $this->messages->add('Failed to add Location details.', 'success');
                    $this->template->write_view('content','locations/add',$data);
                    
                }
            }
            else
            {
                $this->db->select('users.id, users.username');
                $this->db->order_by("users.username", "ASC");
                $users = $this->db->get('users');
                foreach($users->result() as $user){
                    $data['users'][$user->id] = $user->username;
                }
                $this->messages->add('Please correct the following errors!', 'error');
                $this->template->write_view('content','locations/add',$data);
            }
        }
        $this->template->render();
    }
Any solution to show the message for the first time? is there any break in the order?

#2
[eluser]Mithun[/eluser]
Any update?

#3
[eluser]davidbehler[/eluser]
The problem is, that you get the messages in the contructor of your controller and that's before validation was even run.

Try editing your template library to get the messages right before it renders the page (inside template->render). That should fix your problem


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.