Welcome Guest, Not a member yet? Register   Sign In
Problem with messages script
#1

[eluser]Carmichael[/eluser]
Controller
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Messages extends MY_Controller {
    
function __construct()
{
  parent::__construct();
        
  $this->load->model('user_mod');
        
        $this->load->model('messages_mod');
        
        $this->receiver = $this->uri->segment(4, 0);
}
    
public function index()  
{
    
        $data['title'] = 'Inbox';
        
        $data['view'] = 'user/messages/inbox';
        
        $this->load->view(TEMPLATE, $data);
        
}
    
    function compose()
    {
        // get the username of the receiver
        if ($this->receiver > 0){
            $data['row'] = $this->user_mod->get_user_data($this->receiver, 'uid, username');
        }
        
        // validation
        $this->form_validation->set_rules('receiver', 'Receiver', 'required|trim|xss_clean');  
        $this->form_validation->set_rules('subject', 'Subject', 'required|trim|xss_clean');
        $this->form_validation->set_rules('content', 'Content', 'required|trim|xss_clean');  

      if ($this->form_validation->run() == TRUE)
      {  
                
                $result = $this->messages_mod->compose_message($sender, $receiver, $subject, $content);
                
                if ($result == TRUE)
                {
                
                    // message was sent
                    $this->session->set_flashdata('message', alert_message('success', 'Sent!'));
                    redirect('user/messages');
                }
                else
                {
                    // message wasn't sent
                    
                    $this->session->set_flashdata('message', alert_message('error', 'Error!'));
                    redirect('user/messages/compose/'.$this->receiver);
                }
              
            }
            else
            {
                // validation error
                $this->session->set_flashdata('message', alert_message('error', 'Validation error!'));
                redirect($this->uri->uri_string());
            }
        }
        
            
        $data['title'] = 'New message';
        
        $data['view'] = 'user/messages/compose';
        
        $data['receiver'] = array(
                "type"  =>  "text",
                "name"  =>  "receiver",
                "id"    =>  "receiver",
                "value" =>  isset($data['row']['username']) ? $data['row']['username'] : set_value('receiver')
        );
        
        $data['subject'] = array(
                "type"  =>  "text",
                "name"  =>  "subject",
                "id"    =>  "subject",
                "value" =>  set_value('subject')
        );
        
        $data['content'] = array(
                "name"  =>  "content",
                "id"    =>  "content",
                "rows"  =>  10,
                "cols"  =>  10
        );
        
        $this->load->view(TEMPLATE, $data);
    }
    
  
      
    
}
/* End of file messages.php */
/* Location: ./application/controllers/user/messages.php */

View compose.php
Code:
<div id="content">

    <h1>New message</h1>

    &lt;?php echo form_open(isset($row['uid']) ? 'user/messages/compose/'.$row['uid'] : 'user/messages/compose'); ?&gt;
    
        Receiver: &lt;?php echo form_input($receiver); ?&gt;
        
        <br />
        
        Subject: &lt;?php echo form_input($subject); ?&gt;
        
        <br />
        
        &lt;?php echo form_textarea($content, set_value('content')); ?&gt;
        
        &lt;?php echo form_submit(array('name' => 'submit', 'value' => 'Send message')); ?&gt;
    
    &lt;?php echo form_close(); ?&gt;

</div>

The problem is as soon as you visit user/messages/compose the validation script runs and generates error. I want it to run when you send the message (submit).
#2

[eluser]CroNiX[/eluser]
In your compose() method, check for $this->input->post() before setting the validation rules and running them. The way your code is now, it executes whether or not the form was posted. There's really no point in wasting cpu cycles setting up and executing validation if no form was posted to begin with.

Code:
if ($this->input->post()) //Check for $_POST
{
  //set validation rules

  //run validation...
}

$this->load->view(TEMPLATE, $data);
#3

[eluser]weboap[/eluser]
this part of your controller should be in method compose , you are just showing the form, if you insist to have it this way, you will have to add a }else{} to the test on receiver ($this->receiver > 0 )


Code:
public function compose(){

$data['title'] = 'New message';
        
        $data['view'] = 'user/messages/compose';
        
        $data['receiver'] = array(
                "type"  =>  "text",
                "name"  =>  "receiver",
                "id"    =>  "receiver",
                "value" =>  isset($data['row']['username']) ? $data['row']['username'] : set_value('receiver')
        );
        
        $data['subject'] = array(
                "type"  =>  "text",
                "name"  =>  "subject",
                "id"    =>  "subject",
                "value" =>  set_value('subject')
        );
        
        $data['content'] = array(
                "name"  =>  "content",
                "id"    =>  "content",
                "rows"  =>  10,
                "cols"  =>  10
        );
        
        $this->load->view(TEMPLATE, $data);
    }


then
Code:
public function process(){


// your validation process
//this function will be in your form action.


}
#4

[eluser]Carmichael[/eluser]
[quote author="CroNiX" date="1338485545"]In your compose() method, check for $this->input->post() before setting the validation rules and running them. The way your code is now, it executes whether or not the form was posted. There's really no point in wasting cpu cycles setting up and executing validation if no form was posted to begin with.

Code:
if ($this->input->post()) //Check for $_POST
{
  //set validation rules

  //run validation...
}

$this->load->view(TEMPLATE, $data);
[/quote]
Isset. Of course. CodeIgniter has made me so comfortable in my coding that I have forgot the menial php functions.
#5

[eluser]Carmichael[/eluser]
[quote author="weboap" date="1338485937"]this part of your controller should be in method compose , you are just showing the form, if you insist to have it this way, you will have to add a }else{} to the test on receiver ($this->receiver > 0 )


Code:
public function compose(){

$data['title'] = 'New message';
        
        $data['view'] = 'user/messages/compose';
        
        $data['receiver'] = array(
                "type"  =>  "text",
                "name"  =>  "receiver",
                "id"    =>  "receiver",
                "value" =>  isset($data['row']['username']) ? $data['row']['username'] : set_value('receiver')
        );
        
        $data['subject'] = array(
                "type"  =>  "text",
                "name"  =>  "subject",
                "id"    =>  "subject",
                "value" =>  set_value('subject')
        );
        
        $data['content'] = array(
                "name"  =>  "content",
                "id"    =>  "content",
                "rows"  =>  10,
                "cols"  =>  10
        );
        
        $this->load->view(TEMPLATE, $data);
    }


then
Code:
public function process(){


// your validation process
//this function will be in your form action.


}
[/quote]
The thing is that I will have more methods in the Messages controller. compose(), save(). delete() and forward(). Isn't better to have the input data in the same method rather than create a new method for each method?




Theme © iAndrew 2016 - Forum software by © MyBB