CodeIgniter Forums
Function to generate this code (with variables) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Function to generate this code (with variables) (/showthread.php?tid=11693)

Pages: 1 2


Function to generate this code (with variables) - El Forum - 09-19-2008

[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!


Function to generate this code (with variables) - El Forum - 09-19-2008

[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;
}



Function to generate this code (with variables) - El Forum - 09-19-2008

[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?


Function to generate this code (with variables) - El Forum - 09-19-2008

[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();



Function to generate this code (with variables) - El Forum - 09-19-2008

[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?


Function to generate this code (with variables) - El Forum - 09-19-2008

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


Function to generate this code (with variables) - El Forum - 09-19-2008

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


Function to generate this code (with variables) - El Forum - 09-19-2008

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


Function to generate this code (with variables) - El Forum - 09-19-2008

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


Function to generate this code (with variables) - El Forum - 09-19-2008

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

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