Welcome Guest, Not a member yet? Register   Sign In
why use form helpers over standard HTML?
#1

[eluser]Brian Dickson[/eluser]
I'm learning CI and PHP5/OOP. I've seen examples in tutorials where form helpers and classes are used to, for example create a text field or checkboxes. I'd like to know what the benefits are of using such helpers and classes, thus introducing a further step before the browser renders the HTML?

FYI: I did ask this question in another community and didn't get the insight to help me understand.
#2

[eluser]mi6crazyheart[/eluser]
From my little experience with CI... i feel form helpers and classes are help u a lot to easily integrate u'r form with existing CI code base
#3

[eluser]n0xie[/eluser]
Honestly, I don't like to use form helpers inside views. The only helper I do use is the form_dropdown, because this saves a lot of 'keystrokes'. Most of the other helpers take about the same amount of keystrokes for an helper or plain HTML, and I prefer 'plain' HTML because I try to keep the amount of PHP code to a minimum inside views. The whole mixing of PHP and HTML looks 'ugly' to me.

I guess this is not what you'd want to hear but imho, there is nothing wrong in using just HTML in views. (obviously I do use helpers for some tasks. The best way I would describe helpers is to have some specific output that you need formatted by PHP that you abstract away into 1 function to avoid 'PHP clutter' inside a view).
#4

[eluser]jimmie32[/eluser]
I actually think that it sort-of breaks the separation between "Code" and "Design"; if you have to use PHP in Views, then it actually breaks the reason why views were created in the first place.

There are only a few times when I use helpers within views:
1. Saves keystrokes: Lots of <option>s to type. It's not a problem anymore with text expanders, though.
2. Saves keystrokes + file size: Lots of data which can be changed (eg. dropdown in forms), or it's just too much data and you're either lazy to type all the <option>s, or want flexibility, or want to save file size.

Of course, there's more in the world of helpers apart from the Form and HTML one, but myself, I don't use it frequently. Good ol' HTML rules.
#5

[eluser]praptin[/eluser]
I also don't like using form helper.

I think it's same with ASP.NET.
Code:
<asp:textbox runat="server" id="Data" />
<asp:button runat="server" id="buttonPost" Text="Click" PostBackUrl="target.aspx" />
#6

[eluser]pickupman[/eluser]
One of the advantages of the form helper, I find easier to do form validation, prep input in the controller, and then pass the whole thing off to a simple: form_input($field_name);
Code:
//In controller
$this->form_validation->set_rules('field_name', 'field name', 'required|trim');
$data['field_name'] = array('type' => 'text',
                            'name' => 'field_name',
                            'class' => 'some_class',
                            'size'  => '25',
                            'value' => set_value('field_name','') //May the default value here would be from DB
                            );

//In view
&lt;?php echo form_input($field_name);?&gt;

I guess some could argue the semantics of having some the style elements in the controller, but it all depends on the scope of your work.
#7

[eluser]WanWizard[/eluser]
Problem with your solution is that you move certain elements that should be part of the view (like size, class) to your controller logic.
It should be up to the designer how to present the data, not to the programmer responsible for the logic.
#8

[eluser]Phil Sturgeon[/eluser]
Here is an article I wrote explaining the use of form helpers (and helpers in general) as it's not always obvious from the start.

Why CodeIgniter HTML helpers rock
#9

[eluser]Narkboy[/eluser]
Brian - if you're learning PHP & CI and are relatively new to coding and frameworks in particular, then you'll discover that there are many ways of doing anything. Some save time typing out code, some save time in server resources, some make changes later on easier, some have no distinct advantage, some are personal preferences.

As a rule, anything that saves you time, makes your development simpler, or makes future changes easier is worthwhile. Very rarely will you be looking for server performance gains as small as the form helper. Find what you like, then stick with it until you run into a situation that means it needs changing!

/B
#10

[eluser]pickupman[/eluser]
[quote author="WanWizard" date="1277411672"]Problem with your solution is that you move certain elements that should be part of the view (like size, class) to your controller logic.
It should be up to the designer how to present the data, not to the programmer responsible for the logic.[/quote]

I just threw those in for fun. Some might argue that the helpers limit the ability to add attributes to an element. Class & size would probably not be best in the controller, and if you construct your markup correctly you will have pretty good specificity for css selectors. This will be one of those nice features about CSS3 using pseudo selectors like in jQuery/webkit/gecko.

@phil
good post with examples.




Theme © iAndrew 2016 - Forum software by © MyBB