Can I set default value for POST/GET data if it's empty/false, something like
$this->input->post("varname", "value-if-falsy")
?
So I don't have to code like
$a = $this->input->post("varname") ?
$this->input->post("varname") :
"value-if-falsy"
Just found out not very long ago that I can also use ?:
, eg.
$name = $this->input->post('name') ?: 'defaultvalue';
You have to override the default behavior.
In application/core
create MY_Input.php
class MY_Input extends CI_Input
{
function post($index = NULL, $xss_clean = FALSE, $default_value = NULL)
{
// Check if a field has been provided
if ($index === NULL AND ! empty($_POST))
{
$post = array();
// Loop through the full _POST array and return it
foreach (array_keys($_POST) as $key)
{
$post[$key] = $this->_fetch_from_array($_POST, $key, $xss_clean);
}
return $post;
}
$ret_val = $this->_fetch_from_array($_POST, $index, $xss_clean);
if(!$ret_val)
$ret_val = $default_value;
return $ret_val;
}
}
And then in your controller :
$this->input->post("varname", "", "value-if-falsy")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With