Welcome Guest, Not a member yet? Register   Sign In
Default value for empty fields
#1

[eluser]rayed[/eluser]
Hi,

Is there a helper function that allows to set default value if the field is empty, something like:
Code:
<?= default($title, "No title") ?>

So it will display $title if it is available, or display "No title", that way I don't have to worry about unset or empty variables.

I am used to use Smarty default modifier that do basically the same thing, and it was very handle.

Rayed
#2

[eluser]xwero[/eluser]
i use a modified input->post for this
Code:
function post($index = '', $default= FALSE, $xss_clean = FALSE) // added $default
    {        
        if ( ! isset($_POST[$index]))
        {
            return $default;
        }

        if ($xss_clean === TRUE)
        {
            if (is_array($_POST[$index]))
            {
                foreach($_POST[$index] as $key => $val)
                {                    
                    $_POST[$index][$key] = $this->xss_clean($val);
                }
            }
            else
            {
                return $this->xss_clean($_POST[$index]);
            }
        }

        return $_POST[$index];
    }
usage
Code:
$this->input->post('fieldname','default')
You could write a wrapper function for less typing.
#3

[eluser]rayed[/eluser]
Thanks xwero for your reply, but I was looking for a helper function to use it in views with variables passed from the controller
.
Currently I am using a custom helper with this function:
Code:
function ifempty($var, $default='') {
if (isset($var) and trim($var)!='') return $var;
   else return $default;
}

I thought I would find something in the core CodeIgniter to replace my helper function.
#4

[eluser]xwero[/eluser]
You could use the validation library for this as an alternative; validation default values but it seems unlogical to me to set default values for fields with validation if you are not going to validate.
#5

[eluser]rayed[/eluser]
I guess I will stick with my simple helper Smile
Thank you
#6

[eluser]xwero[/eluser]
That is ok too Smile




Theme © iAndrew 2016 - Forum software by © MyBB