Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Update system/helpers/form_helper.php #1870

Closed
wants to merge 3 commits into from

4 participants

@JFox-sk

Example use: 

// PHP
form_hidden('status_id', '0', FALSE, 'id="status_id_for_update"');

...

// JavaScript
$('#event').change(function() {
  $('#status_id_for_update').val('1');
});
@JFox-sk JFox-sk Update system/helpers/form_helper.php
Example use: 
// PHP
form_hidden('status_id', '0', FALSE, 'id="status_id_for_update"');
...
// JavaScript
$('#event').change(function() {
  $('#status_id_for_update').val('1');
});
130c5f4
system/helpers/form_helper.php
((6 lines not shown))
}
return $form;
}
if ( ! is_array($value))
{
- $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value, $name)."\" />\n";
+ $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value, $name)."\" '.$extra.' />\n";

Check this concatenation, it is wrong...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@JFox-sk

@Dentxinho : Yes, you are right. Thank you.

@JFox-sk

Update:

if ( ! function_exists('form_hidden'))
{
    /**
     * Hidden Input Field
     *
     * Generates hidden fields. You can pass a simple key/value string or
     * an associative array with multiple values.
     *
     * @param   mixed
     * @param   string
     * @param   bool
     * @param   string
     * @return  string
     */
    function form_hidden($name, $value = '', $recursing = FALSE, $extra = '')
    {
        static $form;

        if ($recursing === FALSE)
        {
            $form = "\n";
        }

        if (is_array($name))
        {
            foreach ($name as $key => $val)
            {
                form_hidden($key, $val, TRUE, $extra);
            }
            return $form;
        }

        if ( ! is_array($value))
        {
            $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value, $name)."\" ".$extra." />\n";
        }
        else
        {
            foreach ($value as $k => $v)
            {
                $k = is_int($k) ? '' : $k;
                form_hidden($name.'['.$k.']', $v, TRUE, $extra);
            }
        }

        return $form;
    }
}
@Dentxinho

No, man :P

$form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value, $name).'" '.$extra.' />\n';
@Dentxinho

Damn those escaping =x

@JFox-sk
$form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value, $name).'" '.$extra." />\n";
@Dentxinho

Whatever ;)

@JFox-sk

:-) or better:

if ( ! function_exists('form_hidden'))
{
    /**
     * Hidden Input Field
     *
     * Generates hidden fields. You can pass a simple key/value string or
     * an associative array with multiple values.
     *
     * @param   mixed
     * @param   string
     * @param   bool
     * @param   string
     * @return  string
     */
    function form_hidden($name, $value = '', $recursing = FALSE, $extra = '')
    {
        static $form;

        if(strlen($extra))
            $extra = ' '.$extra;

        if ($recursing === FALSE)
        {
            $form = "\n";
        }

        if (is_array($name))
        {
            foreach ($name as $key => $val)
            {
                form_hidden($key, $val, TRUE, $extra);
            }
            return $form;
        }

        if ( ! is_array($value))
        {
            $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value, $name).'"'.$extra." />\n";
        }
        else
        {
            foreach ($value as $k => $v)
            {
                $k = is_int($k) ? '' : $k;
                form_hidden($name.'['.$k.']', $v, TRUE, $extra);
            }
        }

        return $form;
    }
}
@Dentxinho

@JFox-sk

You have to let a space between that " and the $extra content. So your last post above is wrong, and the former is right:

$form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value, $name).'" '.$extra." />\n";
@JFox-sk

@Dentxinho Above, these two lines:

        if(strlen($extra))
            $extra = ' '.$extra;
@JFox-sk

Whatever space only when it is to be :-)

@narfbg
Owner

So why don't you commit instead of pasting the updated function?

@cryode

I'd argue that this is unnecessary. In my experience, a hidden input is rarely targeted for manipulation, so it doesn't need any additional attributes. There's also a note in the dev user guide about how to create one with extra attributes using form_input().

If this was to be integrated, I'd suggest:

  • Moving the $recursive = FALSE parameter to be last, so it does not need to be entered by users if they wish to add additional attributes. If it is only used internally, there's no reason any users need to see it.
  • $extra parameter should accept either a string or an array.

In the end, form helpers as a whole could use some standardization on how they handle all their attributes.

system/helpers/form_helper.php
((6 lines not shown))
{
static $form;
+ if(strlen($extra))
@narfbg Owner
narfbg added a note

Needs to be:

if (strlen($extra)) // strlen() really isn't useful here though
{
    $extra = ' '.$extra;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@narfbg
Owner

... agreed on $extra accepting arrays, much like other form helper functions accept $attributes.

Also, a changelog entry must be added.

@narfbg
Owner
@narfbg narfbg closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 10, 2012
  1. @JFox-sk

    Update system/helpers/form_helper.php

    JFox-sk authored
    Example use: 
    // PHP
    form_hidden('status_id', '0', FALSE, 'id="status_id_for_update"');
    ...
    // JavaScript
    $('#event').change(function() {
      $('#status_id_for_update').val('1');
    });
Commits on Oct 13, 2012
  1. @JFox-sk
Commits on Oct 15, 2012
  1. @JFox-sk
This page is out of date. Refresh to see the latest.
View
20 system/helpers/form_helper.php
@@ -129,10 +129,22 @@ function form_open_multipart($action = '', $attributes = array(), $hidden = arra
* @param bool
* @return string
*/
- function form_hidden($name, $value = '', $recursing = FALSE)
+ function form_hidden($name, $value = '', $extra = '', $recursing = FALSE)
{
static $form;
+ if (is_array($extra))
+ {
+ $extra_string = '';
+ foreach ($extra as $key => $val)
+ {
+ $extra_string .= ' '.$key.'="'.$val.'"';
+ }
+ $extra = $extra_string;
+ }
+ elseif($extra != '')
+ $extra = ' '.$extra;
+
if ($recursing === FALSE)
{
$form = "\n";
@@ -142,21 +154,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, $name)."\" />\n";
+ $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value, $name).'"'.$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
@@ -82,6 +82,7 @@ Release Date: Not Released
- Added an optional paramater to ``delete_files()`` to enable it to skip deleting files such as .htaccess and index.html.
- ``read_file()`` is now a deprecated alias of ``file_get_contents()``.
- :doc:`Security Helper <helpers/security_helper>` function ``strip_image_tags()`` is now an alias for the same method in the :doc:`Security Library <libraries/security>`.
+ - :doc:`Form Helper <helpers/form_helper>` function ``form_hidden()`` added an optional parameter to add attributes.
- Database
View
17 user_guide_src/source/helpers/form_helper.rst
@@ -148,6 +148,23 @@ If you want to create hidden input fields with extra attributes
<input type="hidden" name="email" value="john@example.com" id="hiddenemail" class="hiddenemail" />
*/
+or:
+
+::
+
+ $data = array(
+ 'id'          => 'hiddenemail',
+ 'class'       => 'hiddenemail'
+ );
+
+ echo form_hidden('email', 'john@example.com', $data);
+
+ /*
+ Would produce:
+
+ <input type="hidden" name="email" value="john@example.com" id="hiddenemail" class="hiddenemail" />
+ */
+
form_input()
============
Something went wrong with that request. Please try again.