CodeIgniter Forums
Can I convert empty strings to NULL with a Codeigniter validation rule? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forum-11.html)
+--- Thread: Can I convert empty strings to NULL with a Codeigniter validation rule? (/thread-72905.html)



Can I convert empty strings to NULL with a Codeigniter validation rule? - b126 - 02-27-2019

I would like to convert all my POST values from '' to NULL automatically with a Codeigniter validation rule. Is it possible?
Here is my helper, which is working fine with text and numbers :

PHP Code:
function empty2null($value)
{
 
   return $value === '' null $value;



Can I define it as a validation rule (prepping?) somewhere? How to do this?
Afterwards I would like to send it as a proper NULL value to Doctrine.

I tried to implement it as a classic rules, but this custom function is not triggered when the POST value is empty.

Thank you


RE: Can I convert empty strings to NULL with a Codeigniter validation rule? - php_rocs - 02-27-2019

@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;


RE: Can I convert empty strings to NULL with a Codeigniter validation rule? - b126 - 02-27-2019

(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.


RE: Can I convert empty strings to NULL with a Codeigniter validation rule? - php_rocs - 02-27-2019

@b126,

You could take the forms data array and determine if it has any '' values and if so then change it either NULL or 0.


RE: Can I convert empty strings to NULL with a Codeigniter validation rule? - b126 - 02-27-2019

At the end I think that I will override the post() method of the CI_Input core class instead.

Quicker and easier than everything else.
Just one line of code to add.


RE: Can I convert empty strings to NULL with a Codeigniter validation rule? - dave friend - 02-28-2019

That sounds like a good approach. I'd like to see the implementation.


RE: Can I convert empty strings to NULL with a Codeigniter validation rule? - b126 - 02-28-2019

(02-28-2019, 12:25 PM)dave friend Wrote: That sounds like a good approach. I'd like to see the implementation.

I did it and it’s working fine, with strings, and also with numeric values. 
Here is my /application/core/MY_Input.php

PHP Code:
class MY_Input extends CI_Input {
 
   public function post($index NULL$xss_clean NULL)
 
   {
 
       $value$this->_fetch_from_array($_POST$index$xss_clean);
 
       return $value === '' null $value;
 
       //return isset($value) ? $value : (isset($default_fail) ? $default_fail : NULL);
 
   }




RE: Can I convert empty strings to NULL with a Codeigniter validation rule? - dave friend - 02-28-2019

(02-28-2019, 12:45 PM)b126 Wrote: I did it and it’s working fine, with strings, and also with numeric values. 
Here is my /application/core/MY_Input.php

PHP Code:
class MY_Input extends CI_Input {
 
   public function post($index NULL$xss_clean NULL)
 
   {
 
       $value$this->_fetch_from_array($_POST$index$xss_clean);
 
       return $value === '' null $value;
 
       //return isset($value) ? $value : (isset($default_fail) ? $default_fail : NULL);
 
   }


One thing to be aware of is your code introduces a bug when you ask CI_Input::post() to return the entire $_POST array. i.e.
PHP Code:
$data $this->input->post(); 

That might not be something you ever do, but future code maintenance or changes might run up against this. It's an easy thing to forget and a hard thing to point out to someone else coming to the project.

I think you have to either override _fetch_from_array() - which is not as trivial as what you've done - or,  you could do the following

PHP Code:
public function post($index NULL$xss_clean NULL)
{
 
   $data $this->_fetch_from_array($_POST$index$xss_clean);
 
   if(is_array($data))
 
   {
 
       $output = array();
 
       foreach($data as $key => $value)
 
       {
 
           $output[$key] = $value === '' NULL $value;
 
       }
 
       return $output;
 
   }
 
   return $data === '' null $data;


Have you considered that you might want to trim the input in case somebody enters a string with empty spaces?