CodeIgniter Forums
set_value with get - 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: set_value with get (/showthread.php?tid=65039)



set_value with get - Hoxfoximper - 04-22-2016

Since Codeigniter 3.xx or before the set_value method has changed.

I have this in MY_form_helper

function set_value($field = '', $default = ''){
if (FALSE === ($OBJ =& _get_validation_object()))
{
if (isset($_POST[$field]))
{
return form_prep($_POST[$field], $field);
}
if(isset($_GET[field]))
{
return form_prep($_GET[$field], $field);
}
return $default;
}
return form_prep($OBJ->set_value($field, $default), $field);
}


In Codeigniter 3.xx it looks like this

function set_value($field, $default = '', $html_escape = TRUE)
{
$CI =& get_instance();

$value = (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
? $CI->form_validation->set_value($field, $default)
: $CI->input->post($field, FALSE);

isset($value) OR $value = $default;
return ($html_escape) ? html_escape($value) : $value;
}

can anybody help me rewrite it so it fixes my the get problem for my search input? and does the same as my prev code?

THX Big Grin ALOT


RE: set_value with get - Hoxfoximper - 04-22-2016

Anybody?


RE: set_value with get - mwhitney - 04-27-2016

I would recommend passing your GET data to the form validation library with the set_data() method (or not using GET with form data). See https://codeigniter.com/user_guide/libraries/form_validation.html#validating-an-array-other-than-post

However, I believe the following might be the CI3 equivalent of your function:

PHP Code:
function set_value($field ''$default '')
{
 
   $CI =& get_instance();

 
   $value = (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
 
       $CI->form_validation->set_value($field$default)
 
       $CI->input->post($fieldFALSE);

 
   isset($value) OR $value = ($CI->input->get($field) === null $default $CI->input->get($field));
 
   return ($html_escape) ? html_escape($value) : $value;