Welcome Guest, Not a member yet? Register   Sign In
Prevent null values on input post
#1

Just extends Input Core


PHP Code:
<?php

class My_Input extends CI_Input
{
    function 
__construct()
    {
        
parent::__construct();
    }

    
/**
     * Fetch an item from the POST array
     *
     * @param    mixed    $index        Index for item to be fetched from $_POST
     * @param    bool    $xss_clean    Whether to apply XSS filtering
     * @return    mixed
     */
    
public function post($index NULL$xss_clean NULL)
    {
         
$value $this->_fetch_from_array($_POST$index$xss_clean);
                 if(
is_null($value)){ return ''; }
        return 
$value;
    }

Reply
#2

Moved your post here, as it appears to be a suggestion rather than a bug or support request.
Reply
#3

(09-29-2017, 10:10 AM)jonathanq Wrote: Just extends Input Core


PHP Code:
<?php

class My_Input extends CI_Input
{
 function 
__construct()
 {
 
   parent::__construct();
 }

 
/**
 * Fetch an item from the POST array
 *
 * @param mixed $index Index for item to be fetched from $_POST
 * @param bool $xss_clean Whether to apply XSS filtering
 * @return mixed
 */
 
public function post($index NULL$xss_clean NULL)
 {
 
$value $this->_fetch_from_array($_POST$index$xss_clean);
 
                if(is_null($value)){ return ''; }
 return 
$value;
 }


Why?

If you need an empty string use (string) before the methode.

$data = (string) $this->input->post('not_exist');
Reply
#4

But what if an empty string is not wanted but NULL is?

If we want to make everyone happy then maybe something like this


PHP Code:
/**
 * Fetch an item from the POST array
 *
 * @param mixed $index Index for item to be fetched from $_POST
 * @param bool $xss_clean Whether to apply XSS filtering
 * @param bool $default_fail The value to return if $_POST[$index] is not set or is NULL
 * @return mixed
 */
 
public function post($index NULL$xss_clean NULL$default_fail NULL)
 {
 
    $value $this->_fetch_from_array($_POST$index$xss_clean);
 
    return isset($value) ? $value : (isset($default_fail) ? $default_fail NULL); 
 } 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB