• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting form helper to write input name AND id (extend form helper)

#1
[eluser]runemedia[/eluser]
I want to write my form fields to have a name attribute as well as an id attribute. If I use the helper as indicated by the userguide, it just writes out the name attribute with the default value of the field. Now, I understand that I can use an array to specify all the field attributes I wish to use (that's well explained in the userguide). However, this seems like something that should be automated and it seems extending the form helper is the way to do that.

However, I'm not certain how I might start to do this. Any guidance to point me in the right direction? Thanks.

#2
[eluser]Jaketoolson[/eluser]
The capability to do so is built in by default, however, you're correct, I don't believe an id is set to the name by default. What if you have a check boxes and the name given is an array (e.g. name="selections[]" ). I don't want the id to then be "selections[]"...

As taken directly from the CI user guide's form_input() section:
Code:
$data = array(
              'name'        => 'username',
              'id'          => 'username',
              'value'       => 'johndoe',
              'maxlength'   => '100',
              'size'        => '50',
              'style'       => 'width:50%',
            );

echo form_input($data);

// Would produce:

<input type="text" name="username" id="username" value="johndoe" maxlength="100" size="50" style="width:50%" />

#3
[eluser]runemedia[/eluser]
Thanks Jaketoolson. Your point about the check boxes is valid, but there's a separate form helper for those already form_checkbox(), so presumably that won't be an issue. I'm aware of the array option (and have used it), but it still seems like something that can be automated with less typing. The majority of my text inputs have name, id and value attributes, so I thought I could extend the current form_input() to write out those for me without going to the extra typing of setting the array for every single form_input().

I see in the user guide that there is a way to extend a helper, and I believe this is what I should be trying to do. I'm just not smart enough to get that started. The listed example is not something I can seem to follow to write my own code.

#4
[eluser]Jaketoolson[/eluser]
To extend a class, library, or helper, you create a file with the prefix 'MY_' and the suffix whatever the name of the file you are going to be "extending". In this case it would be the form_helper.php found in 'system/helpers' folder.

If you wish to add functions to this helper, within the new 'MY_form_helper.php' file you created and placed in the application/helpers folder, simple create them and thats it. This file will then be loaded automatically after the form_helper.php is loaded. If you wish to overwrite existing functions within that file, just recreate them in this file.

Code:
function form_input($data = '', $value = '', $extra = '')
{
    $defaults = array('type' => 'text', 'name' => NULL, 'id' => NULL, 'value' => $value);

    if ( ! (is_array($data))
    {
        $defaults['name'] = $data;
        $defaults['id'] = $data;
    }

    return "<input "._parse_form_attributes($data, $defaults).$extra." />";
}

This sets NAME and ID attributes to NULL by default, then if $data is not an array, it sets both NAME and ID attributes to $data (or the same thing).

#5
[eluser]runemedia[/eluser]
Thanks so much for not just pointing me in the right direction, but writing the code for me! There was one small error on line #5 of your code (missing an extra closing parentheses).

Plus, by examining the form_helper.php code, I see I can use the $extra variable to add a class if I need without resorting to an array.

Again, I really appreciate your help.

#6
[eluser]Jaketoolson[/eluser]
No problem... you mentioned what I forgot - adding a 'class' (instead of 'style') attribute. I seem to do this to a lot of forms and since I'm already passing the 'class' attribute, I figure whats another line of code to pass the 'id' attribute.

So shoot, perhaps taking the time to extend the form_helper file and recreating the function(s) might take more time than it would if you spend a few extra seconds and pass the additional attributes for each form Smile


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.