Welcome Guest, Not a member yet? Register   Sign In
Unintuitive first parameter in set_value() function of the form helper

I generally use set_value() this way:

<?=form_input('some_field', set_value('some_field', 'first_load_value'))?>

Why isn't it sufficient to just say set_value('first_load_value') and be done with it? Shouldn't CodeIgniter be able to detect which field I'm in? So much unnecessary typing would be saved, forms are boring things to deal with as it is :-(

How should CI detect what field you are in?
It's a function call inside a function call..there's no way (99.9% sure at least) of telling what parameter was passed to the form_input function inside the set_value function.

And what if you are using the set_value function differently:
<input name="some_field" value="<?php echo set_value('some_field', 'first_load_value'); ?>" />

The only way I can think of right now that would reduce the amount of typing would be to use a custom wrapper function like this:
// place it in MY_form_helper.php in application/helpers

function form_input_set($name, $first_value)
  return form_input($name, set_value($name, $first_value));

and in your form you could would do this:
<?php echo form_input_set('some_field', 'first_load_value'); ?>

Yeah, I wrote a custom helper function that does the same thing you suggested, and I kept it in my master copy of the framework. It gets copied to every new project I start...

Well, since it's just one line, maybe CI authors could consider making another function in this helper that implements the required functionality? Just pass two arguments instead of having to type set_value()?

Theme © iAndrew 2016 - Forum software by © MyBB