Welcome Guest, Not a member yet? Register   Sign In
How to check for / insert optional fields?
#1

[eluser]wdm*[/eluser]
At the moment my code looks like this:

Code:
# Assign values for saving to the db
$data = array(
    'table_of_contents' => $_POST['table_of_contents'],
    'length' => $_POST['length']
);

# Check for fields that may not be set
if ( isset($_POST['lossless_copy']) )
{
   $data = array(
       'lossless_copy' => $_POST['lossless_copy']
    );
}

// etc.

This would lead to endless if statements though... Even with the ternary syntax it's still messy. Is there a better way?
#2

[eluser]elvix[/eluser]
not sure if you care about blank values or not, but if you use CI's post object, which you should anyway, since that's where the XSS clean is applied, then it will simply return blank for any unset values.

instead of $_POST['length'] use $this->input->post('length')
#3

[eluser]wdm*[/eluser]
This doesn't seem to work for all data types.

Postgres doesn't let me save a '' (blank) value for a 'date' column type, for example.

Is this a Postgres bug?

[EDIT] On second thoughts, it seems more like a CI bug. Shouldn't $this->input->post return FALSE, or NULL, rather than an empty string?
#4

[eluser]wdm*[/eluser]
*bump*
#5

[eluser]drewbee[/eluser]
No. It should not return an empty string. That is what was passed from the Form ยป 'an empty string'. Values coming from forms are always strings, even when they are numbers, so false or null are not valid options regarding this.

Your saying postgre can't save an empty string. Seems absolutely rediculous to me (remind me never to use that system);

Anyways, I did some reading and it appears you can insert a null value, but not empty. So, I would do something like this. I little bit cleaner, but still a mess. This will simply check all of your post values and convert every empty string to null. May be a good idea to just have this auto run through the form validation extension.

Code:
// Set to null if empty
foreach ($_POST AS $key => $value)
{
    if (empty($value))
    {
        $_POST[$key] = null;
    }
}

$data = array(
    'table_of_contents' => $this->input->post('table_of_contents'),
    'length' => $this->input->post('length'),
    'lossless_copy' => $this->input->post('lossless_copy'));

);

See how that works for you.




Theme © iAndrew 2016 - Forum software by © MyBB