CodeIgniter Forums

Full Version: $this->input->post('something'); trim the value etc.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

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.

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!

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

    function MY_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 '';
                return trim($post_result);

        return $post_result;
// END Input class

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