Welcome Guest, Not a member yet? Register   Sign In
Do YOU use CodeIgniter's form helpers?
#1

[eluser]Calvin Froedge[/eluser]
The form helpers seem very strange to me. Namely, I'm not sure what the point of using PHP to generate simple inputs is. Also, is there a way to get the form helpers to prepopulate database values to their fields ONLY when no data is present in the post array? Right now I'm using an array to generate inputs:

$coupon_name = array(
'name' => 'coupon_name',
'id' => 'coupon_name',
'class'=> 'text',
'value' => set_value('coupon_name'),
'maxlength' => 30,
'size' => 30,
);

And then...

echo form_label($this->lang->line('coupons_form-add-coupon-label-coupon-name'), 'coupon_name');
echo form_input($coupon_name);

Why is this better than (this code not tested yet...just example)...

<label for="coupon_name">&lt;?php echo $this->lang->line('coupons_form-add-coupon-label-coupon-name');?&gt;
&lt;input type=text" class="text" name="coupon_name" value="
&lt;?php if (isset($form_values-&gt;coupon_name) && set_value('coupon_name') == ''){ echo $form_values->coupon_name; }else{ set_value('coupon_name')} ?&gt; />

Maybe there is some benefit of form helpers that I'm missing?
#2

[eluser]JasonS[/eluser]
I don't use the form helper. I don't find that it 'assists working with forms', it only obsfucates what is really going on.
#3

[eluser]Calvin Froedge[/eluser]
Yea, totally agree. I would MUCH rather work with plain HTML & PHP = ). Still awaiting some CI White Knight to shed light on this if there is in fact light to be shed. Maybe I've missed some crucial benefit?
#4

[eluser]davzie[/eluser]
I don't use the form helpers at all with the exception of form_open_multipart because I always forget the HTML for it.

I validate with MooTools Formcheck, put my PHP validation rules in a config file and run the tests against that. I haven't found a simple way of populating the fields again if there is a validation error though.

Something that needs to be simplified indeed.
#5

[eluser]Calvin Froedge[/eluser]
Quote:I validate with MooTools Formcheck, put my PHP validation rules in a config file and run the tests against that. I haven’t found a simple way of populating the fields again if there is a validation error though.

Use set_value, it's built into the form validation class:

For adding new entities:

Code:
&lt;input type="text" class="text" name="coupon_type_name" value="&lt;?php echo set_value('coupon_type_name');?&gt;" /&gt;

For editing existing:

Code:
&lt;input type="text" class="text" name="coupon_name" value="&lt;?php echo (set_value('coupon_name') == $coupon_info-&gt;coupon_name OR set_value('coupon_name') == '') ? $coupon_info->coupon_name : set_value('coupon_name'); ?&gt;" />

Note that you'll need to use different values than the ones I've got in there (for coupon codes) = ) Let me know if you need further explanation.
#6

[eluser]Jaketoolson[/eluser]
[quote author="Calvin Froedge" date="1297897134"]Maybe I've missed some crucial benefit?[/quote]

I believe this is why they are called 'helper' files and not core requirements in order for CI to run. At first, I didn't use any helper files, but as I progressed on converting my existing site over to CI , I found that I wanted to be consistent and use CI whenever possible. This includes simple tasks like creating anchors, lists, line-breaks (!!), and forms. Maybe overtime your opinion will change, maybe not.

I'm still not keen on using the active records class and chaining any SQL queries using CI as I prefer to write my queries out. This allows me to copy and paste said queries back into MySQL tools for error checking etc;

Also, didn't you just answer your own question?! Specifically you were hesitating with regards to form helpers, then you answered someones question by referring them to the set_value function defined in the form_helpers file.
#7

[eluser]KarlBallard[/eluser]
I admit, I use form_open() and form_close()..

form_open()
- Just because it also adds the CSRF field so it saves me doing it.

form_close()
- Just because I use form_open(), I don't like a closing HTML tag with no HTML opening tag.
#8

[eluser]cahva[/eluser]
@Calvin Froedge:
Populating the value with set_value is easier than your example. You did'nt take advantage of the second parameter, which populates it with that if theres no posted value:

Code:
&lt;input type="text" name="coupon_name" value="&lt;?php echo set_value('coupon_name',$coupon_info-&gt;coupon_name) ?&gt;">

If its new, just initialize it in the controller with empty values. And for existing record, fetch the result from model. The same form view will work for both of them.
#9

[eluser]Calvin Froedge[/eluser]
@Jaketoolson set_value is defined in the form_validation class. The form helper only initializes set_value if it doesn't already exist.

@cahva Aha! Thanks = ) That's much simpler.
#10

[eluser]Jaketoolson[/eluser]
Guess I need to use set_value more Wink




Theme © iAndrew 2016 - Forum software by © MyBB