Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Extra data parameter for hidden input type. #2102

Closed
wants to merge 5 commits into from

4 participants

@arekpietrzyk

Extra data parameter for hidden input type.

Example use: ID parameter for field

@WDC

Out of curiosity, why would someone not just pass that kind of parameter in the array?

It seems CodeIgniter is split between accepting an array or string for functions in the form_helper.

@arekpietrzyk

Because in line looks prettier rather than passing an array in most cases in html template, e.g.:

<?php echo form_hidden('sole_id', $data['sole_id'], 'id="sole_id"'); ?>

@WDC

I'm definitely not arguing that fact, but something similar was discussed in 2044 and it seems the maintainers are not interested in that kind of functionality.

@arekpietrzyk

I think this is personal preference. From my point of view, form_hidden was last one from all form elements who didn't allow extra attributes, and I needed it, so I prepared patch.

If in the future someone decide to discard extra attributes from all form_ functions I'll use arrays, but for now I think it should be updated, because it is small update and for developers like me it will help a lot.

@WDC
@narfbg
Owner

I personally like the array-only approach.

@TheDigitalOrchard

I like the array-only approach, since it's more in line with "composing HTML from PHP". The actual composition of HTML should be handled internally by the helper method or there'd be little use for the method to begin with.

@arekpietrzyk

I wonder why you all try to put all parameters in arrays. Even in that simple use case like calling inputs via Form_helper.

Array utilizes more memory, each time when it's interpreted. And only for your better feeling when coding (probably once in project life). So IMO inline way is more optimal for server enviroment, not array way - in cases like above.

@narfbg
Owner

It just makes more sense for (all) the HTML helpers - every now and then, somebody wants to add a feature, which involves a new parameter. Then you end up with many more parameters than originally planned and becomes inconvenient to both support AND use such a function.

@arekpietrzyk

IMO in-line mode should left even in future for base use (name, value, extra) or (name, value, selected_value, extra) and for advanced use with any special functionality in future array mode.

If you want extra from helper you must remember that will cost resources - to be honest, for now I don't think there will be any extra functionality because they are just html inputs ;)

I don't blame array way in global, for me it just have small sense and cost a lot in this case.

About this pull, it just give us same functionality for hidden type, like in all other types.

@narfbg narfbg closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
9 system/helpers/form_helper.php
@@ -128,10 +128,11 @@ function form_open_multipart($action = '', $attributes = array(), $hidden = arra
*
* @param mixed $name Field name
* @param string $value Field value
+ * @param string $extra Extra data
* @param bool $recursing
* @return string
*/
- function form_hidden($name, $value = '', $recursing = FALSE)
+ function form_hidden($name, $value = '', $extra = '', $recursing = FALSE)
{
static $form;
@@ -144,21 +145,21 @@ function form_hidden($name, $value = '', $recursing = FALSE)
{
foreach ($name as $key => $val)
{
- form_hidden($key, $val, TRUE);
+ form_hidden($key, $val, $extra, TRUE);
}
return $form;
}
if ( ! is_array($value))
{
- $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value)."\" />\n";
+ $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'"'.$extra." />\n";
}
else
{
foreach ($value as $k => $v)
{
$k = is_int($k) ? '' : $k;
- form_hidden($name.'['.$k.']', $v, TRUE);
+ form_hidden($name.'['.$k.']', $v, $extra, TRUE);
}
}
View
1  user_guide_src/source/changelog.rst
@@ -85,6 +85,7 @@ Release Date: Not Released
- :doc:`Form Helper <helpers/form_helper>` changes include:
- :php:func:`form_dropdown()` will now also take an array for unity with other form helpers.
- :php:func:`form_prep()`'s second argument now only accepts a boolean value, which determines whether the value is escaped for a <textarea> or a regular <input> element.
+ - :php:func:`form_hidden()` added an optional paramater to allow pass extra attributes.
- :doc:`Security Helper <helpers/security_helper>` changes include:
- :php:func:`do_hash()` now uses PHP's native ``hash()`` function (supporting more algorithms) and is deprecated.
- :php:func:`strip_image_tags()` is now an alias for the same method in the :doc:`Security Library <libraries/security>`.
View
7 user_guide_src/source/helpers/form_helper.rst
@@ -92,6 +92,7 @@ form_hidden()
:param string $name: Field name
:param string $value: Field value
+ :param string $extra: Extra attributes to be added to the tag *as is*
:returns: string
Lets you generate hidden input fields. You can either submit a
@@ -153,6 +154,12 @@ If you want to create hidden input fields with extra attributes::
<input type="hidden" name="email" value="john@example.com" id="hiddenemail" class="hiddenemail" />
*/
+If you would like your form to contain some additional data, like
+ID, you can pass it as a string in the third parameter::
+
+ $extra = 'id="hidden_field"';
+ echo form_hidden('username', 'johndoe', $extra);
+
form_input()
============
Something went wrong with that request. Please try again.