CodeIgniter Forums
blank set_value() return default value - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: blank set_value() return default value (/showthread.php?tid=1689)



blank set_value() return default value - Reirei - 04-01-2015

Code:
set_value('user[name]', $user->name);

On use this code im my input, the input value is set to default ($user->name) when loaded.
By replacing the default value with a blank value and send the form, the input is displayed with the default value with the form error 'required'.

I want the value, even blank, is setted after sending the form.
I'm using codeigniter 3, if i remember, this did not occur in 2.

Thanks.


RE: blank set_value() return default value - mnolte - 04-04-2015

(04-01-2015, 04:57 PM)Reirei Wrote:
Code:
set_value('user[name]', $user->name);

On use this code im my input, the input value is set to default ($user->name) when loaded.
By replacing the default value with a blank value and send the form, the input is displayed with the default value with the form error 'required'.

I want the value, even blank, is setted after sending the form.
I'm using codeigniter 3, if i remember, this did not occur in 2.

Thanks.

I'm having the same problem in v3.0.1-dev.


RE: blank set_value() return default value - mnolte - 04-04-2015

I'm having the same problem in 3.0.1-dev.


RE: blank set_value() return default value - Reirei - 04-09-2015

Help?!


RE: blank set_value() return default value - silentium - 04-10-2015

If the user submit the field empty, it's empty and if empty, the helper sets the default value.

Only way around would be to have a value in it.


RE: blank set_value() return default value - Reirei - 04-12-2015

(04-10-2015, 10:05 AM)silentium Wrote: If the user submit the field empty, it's empty and if empty, the helper sets the default value.

Only way around would be to have a value in it.

I did a test in the ci 2.1 and works perfectly.

The correct is: If $_POST is empty, set values $default, else set values $_POST.


RE: blank set_value() return default value - silentium - 04-12-2015

After digging through the core of CI 2.2 and CI 3, there are one small difference in the code that seems to be creating this issue.

In CI 3, in system/libraries/Form_validation.php line 941 the set_value() method check if the field exists and have any postdata
PHP Code:
if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
{
 
   return $default;


The same code in CI 2.2 only check if the field exists, but not if it has any content. Line 732.
PHP Code:
if ( ! isset($this->_field_data[$field]))
{
 
   return $default;


When form validation is executed with run(), it does not set the postdata for a field if it is empty. CI 3 system/libraries/Form_validation.php line 466
PHP Code:
elseif (isset($validation_array[$field]) && $validation_array[$field] !== '')
{
 
   $this->_field_data[$field]['postdata'] = $validation_array[$field];


This is the same in both CI 2.2 and CI 3. So due to that CI 3 check if the field has any data, which it don't have if submitted empty, it returns the default field value.

Sum up
Both CI 2.2 and CI 3 sets the postdata in the same way when using the form validation on form submission. But a change in CI 3, namely the adding of checking if a field have any postdata, returns the fields default value if that field was submitted empty.


RE: blank set_value() return default value - Reirei - 04-13-2015

(04-12-2015, 10:59 AM)silentium Wrote: After digging through the core of CI 2.2 and CI 3, there are one small difference in the code that seems to be creating this issue.

In CI 3, in system/libraries/Form_validation.php line 941 the set_value() method check if the field exists and have any postdata



PHP Code:
if ( ! isset($this->_field_data[$field], $this->_field_data[$field]['postdata']))
{
 
   return $default;


The same code in CI 2.2 only check if the field exists, but not if it has any content. Line 732.



PHP Code:
if ( ! isset($this->_field_data[$field]))
{
 
   return $default;


When form validation is executed with run(), it does not set the postdata for a field if it is empty. CI 3 system/libraries/Form_validation.php line 466



PHP Code:
elseif (isset($validation_array[$field]) && $validation_array[$field] !== '')
{
 
   $this->_field_data[$field]['postdata'] = $validation_array[$field];


This is the same in both CI 2.2 and CI 3. So due to that CI 3 check if the field has any data, which it don't have if submitted empty, it returns the default field value.

Sum up
Both CI 2.2 and CI 3 sets the postdata in the same way when using the form validation on form submission. But a change in CI 3, namely the adding of checking if a field have any postdata, returns the fields default value if that field was submitted empty.

This can be considered as an issue?


RE: blank set_value() return default value - silentium - 04-13-2015

I would think so yes. At least I see it as a issue.

Report it on Github https://github.com/bcit-ci/CodeIgniter/issues to get a discussion about it to see what the developers of CI thinks.


RE: blank set_value() return default value - sparky672 - 05-01-2015

I setup a test page because the fix did not work for me.  I have discovered that if the "name" of the field is part of an indexed array, the problem will continue to occur.  I posted a MCVE and a live demo link at the GitHub thread...

https://github.com/bcit-ci/CodeIgniter/issues/3816#issuecomment-97716993