Welcome Guest, Not a member yet? Register   Sign In
Function to generate this code (with variables)
#1

[eluser]bapobap[/eluser]
Hi there,

I'm making lots of form items and they are pretty much standardised, so I thought why not make a nice helper function to make it simpler but I don't know how to have the variable I need to use in the actual code. Example:
Code:
&lt;?php if ($this->validation->VAR_error): echo '<div class="error">'; else: echo '<div class="item">'; endif; ?&gt;
<p><label for="VAR">VAR_LABEL</label>&lt;?=$this->validation->VAR_error?&gt;</p>
<p>&lt;input type="VAR_TYPE" id="VAR" name="VAR" value="&lt;?=$this-&gt;validation->VAR?&gt;" class="text" /></p>
</div>

Somehow generate that, replacing the variables, using a function call like this:
Code:
&lt;?=nice_form_item('email', 'text', 'Email address')?&gt;

Is there any way to do this, or maybe even a better way?

Thanks!
#2

[eluser]Michael Wales[/eluser]
Code:
function nice_form_item($name, $type = 'text', $label = 'Input') {
  $field_error = $name . '_error';
  $str = '<p><label for="' . $name . '">' . $label . '</label>' . $this->input->validation->$field_error . '</p>';
  if ($type == 'text') {
    $str .= '<p>&lt;input type="text" name="' . $name . '" id="' . $name . '" value="' . $this-&gt;validation->$name . '" class="text" /></p>';
  } elseif ($type == 'textarea') {
    $str .= '<p>&lt;textarea name="' . $name . '" id="' . $name . '" class="textarea"&gt;' . $this->validation->$name . '&lt;/textarea&gt;';
  } elseif () {
    // Continue with checkboxes, radios, etc
  }
  return $str;
}
#3

[eluser]bapobap[/eluser]
Thanks Michael, where would this go, as I think if it's a helper, it can't access the CI object and this the validation stuff?
#4

[eluser]Pascal Kriete[/eluser]
You can get access to the CI object from the outside by using get_instance . Assuming of course, that a controller has been instantiated at this point (so don't try it in the error_404 template).
Code:
$CI =& get_instance();

// Now we can get the validation stuff :)
$CI->validation->whatever();
#5

[eluser]bapobap[/eluser]
Hi inparo,

How do I pass that to the helper? If I have the function call in my view, that function in the helper, how would I pass that?
#6

[eluser]Pascal Kriete[/eluser]
You don't pass it, you just call it in the helper. It's a function that is 'include'-ed along with the CI_Base object. It's in the global namespace so you can use it anywhere.

Check out system/helpers/date_helper.php for an example.
#7

[eluser]bapobap[/eluser]
I must be doing something wrong, I'm getting Message: Undefined property: CI_Input::$validation, I have validation on autoload but tried loading it after the get_instance, neither worked.
#8

[eluser]Pascal Kriete[/eluser]
Can you reduce the error to a small(ish) snippet of code and post that?
#9

[eluser]bapobap[/eluser]
I shall try!

Controller
Code:
function test()
{
    $rules['email']                 = 'trim|required|valid_email';
    $fields['email']            = 'Email address';
    
    $this->validation->set_rules($rules);
    $this->validation->set_fields($fields);
    
    echo nice_form_item('email', 'text', 'Email address');
}

Helper (autoloaded, along with validation)
Code:
function nice_form_item($name, $type = 'text', $label = 'Input') {
    $CI =& get_instance();

  $field_error = $name . '_error';
  $str = '<p><label for="' . $name . '">' . $label . '</label>' . $CI->input->validation->$field_error . '</p>';
  if ($type == 'text') {
    $str .= '<p>&lt;input type="text" name="' . $name . '" id="' . $name . '" value="' . $CI-&gt;validation->$name . '" class="text" /></p>';
  } elseif ($type == 'textarea') {
    $str .= '<p>&lt;textarea name="' . $name . '" id="' . $name . '" class="textarea"&gt;' . $CI->validation->$name . '&lt;/textarea&gt;';
  }
  return $str;
}

If I have cut this down to make it stupid sorry, I basically had a big registration method in the controller, the view, then this in the helper.
#10

[eluser]Pascal Kriete[/eluser]
Code:
$CI->input->validation->$field_error
Notice anything input->wrong? :-)

[Edit: You can blame it on Ssgt Wales Wink ]




Theme © iAndrew 2016 - Forum software by © MyBB