Welcome Guest, Not a member yet? Register   Sign In
Can I convert empty strings to NULL with a Codeigniter validation rule?
#3

(This post was last modified: 02-27-2019, 02:24 PM by b126.)

(02-27-2019, 01:10 PM)php_rocs Wrote: @b126,

You should be able to use a PHP function such as isset() (scroll down page to See Also for other suggestions- http://php.net/manual/en/function.isset.php).

return isset($value)? null : $value;

Hi,

Thank you but it’s not a question of isset(), or empty() or ===‘’. 

It’s a question on how to trigger this conversion automatically while posting the form data or during the validation. 

Unfortunately, it seems that CI posts empty string if you do not put anything in your input field, like it is the case for optional fields for example. 

So when a user leave a blank field for an optional input like « distance », the input->post(‘distance’) value posted is ‘’. 

When you convert ‘’ to an integer in php, it becomes 0. 
You will then record 0 in your database instead of NULL.

The same is for empty strings. You will save ‘’ in your database instead of null.

In Laravel, you can use the ConvertEmptyStringsToNull middleware. 
In Symfony, I think you can use empty_data option.
So I wonder if there was an equivalent in Codeigniter.
Reply


Messages In This Thread
RE: Can I convert empty strings to NULL with a Codeigniter validation rule? - by b126 - 02-27-2019, 01:30 PM



Theme © iAndrew 2016 - Forum software by © MyBB