CodeIgniter Forums
$this->input->post('something'); trim the value etc. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: $this->input->post('something'); trim the value etc. (/thread-37916.html)



$this->input->post('something'); trim the value etc. - El Forum - 01-24-2011

[eluser]searain[/eluser]
We would use $this->input->post('something'); a lot. And in 99% cases, I would like the value we get to be trimmed, and return '' of it does not exist (instead of return false as default.

CIdeveloper has suggested Extending the Core Input Class.

http://ellislab.com/forums/viewthread/178827/

I was thinking modifying his codes a little bit. Like the following, so in my application, whenever I call $this->input->post('something'),

it will

1) return '' instead of false if it doesn't exist.
2) return trimmed value.

if i call $this->input->post('something', true); then it will act like the ci default $this->input->post('something');

I am debating if I should extend the core class input. Other developers may get confused if they don't read the document.

But how could I make an easy call that $this->input->post('something') get trimmed and return '' if it doesn't exist?

I was thinking about extend the core input class, but instead override post function, i would write a mypost() function to do the same.

But it seems cideveloper's approach (override post method) is more elegant.

What is your advices? Thanks!

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Input extends CI_Input {

    function MY_Input(){
        parent::CI_Input();
    }

    function post($index = '', $xss_clean = FALSE, $ci_default = FALSE)
    {
        $post_result = $this->_fetch_from_array($_POST, $index, $xss_clean);
        if(!$ci_default) {
            if(!$post_result) {
                return '';
            }
            else
            {
                return trim($post_result);
            }

        }
        return $post_result;
    }
}
// END Input class

/* End of file Input.php */
/* Location: ./application/libraries/MY_Input.php */