blob: fe6b6ce11a1c4e4f9a6334e896454bb6aca90d3b [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev8bf6bb62012-01-06 16:11:04 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
Andrey Andreevc5536aa2012-11-01 17:33:58 +020025 * @filesource
Derek Allard2067d1a2008-11-13 22:59:24 +000026 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * CodeIgniter Form Helpers
31 *
32 * @package CodeIgniter
33 * @subpackage Helpers
34 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @link http://codeigniter.com/user_guide/helpers/form_helper.html
37 */
38
39// ------------------------------------------------------------------------
40
Derek Allard2067d1a2008-11-13 22:59:24 +000041if ( ! function_exists('form_open'))
42{
Timothy Warren01b129a2012-04-27 11:36:50 -040043 /**
44 * Form Declaration
45 *
46 * Creates the opening portion of the form.
47 *
48 * @param string the URI segments of the form destination
49 * @param array a key/value pair of attributes
50 * @param array a key/value pair hidden data
51 * @return string
52 */
vlakoffea19bc42013-07-27 10:07:43 +020053 function form_open($action = '', $attributes = array(), $hidden = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000054 {
55 $CI =& get_instance();
56
Andrey Andreevea41c8a2014-02-26 18:31:02 +020057 // If no action is provided then set to the current url
58 if ( ! $action)
59 {
60 $action = $CI->config->site_url($CI->uri->uri_string());
61 }
vlakoffc4f9c622013-07-27 10:08:00 +020062 // If an action is not a full URL then turn it into one
Andrey Andreevea41c8a2014-02-26 18:31:02 +020063 elseif (strpos($action, '://') === FALSE)
vlakoffc4f9c622013-07-27 10:08:00 +020064 {
65 $action = $CI->config->site_url($action);
66 }
vlakoffc4f9c622013-07-27 10:08:00 +020067
vlakoffea19bc42013-07-27 10:07:43 +020068 $attributes = _attributes_to_string($attributes);
69
70 if (stripos($attributes, 'method=') === FALSE)
Andrey Andreev122ca9b2013-07-26 18:16:26 +030071 {
72 $attributes .= ' method="post"';
73 }
Derek Allard2067d1a2008-11-13 22:59:24 +000074
vlakoffea19bc42013-07-27 10:07:43 +020075 if (stripos($attributes, 'accept-charset=') === FALSE)
76 {
77 $attributes .= ' accept-charset="'.strtolower(config_item('charset')).'"';
78 }
79
vlakoffea19bc42013-07-27 10:07:43 +020080 $form = '<form action="'.$action.'"'.$attributes.">\n";
Barry Mienydd671972010-10-04 16:33:58 +020081
Andrey Andreev93a83c72012-03-26 21:24:02 +030082 // Add CSRF field if enabled, but leave it out for GET requests and requests to external websites
Andrey Andreevea41c8a2014-02-26 18:31:02 +020083 if ($CI->config->item('csrf_protection') === TRUE && strpos($action, $CI->config->base_url()) !== FALSE && ! stripos($form, 'method="get"'))
Derek Allard958543a2010-07-22 14:10:26 -040084 {
Greg Aker1f6f0ab2011-04-07 18:24:53 -050085 $hidden[$CI->security->get_csrf_token_name()] = $CI->security->get_csrf_hash();
Greg Aker28b425a2010-09-15 11:43:23 -050086 }
87
Andrey Andreev93b4e782014-03-04 17:48:21 +020088 if (is_array($hidden))
Greg Aker28b425a2010-09-15 11:43:23 -050089 {
Andrey Andreev93b4e782014-03-04 17:48:21 +020090 foreach ($hidden as $name => $value)
91 {
92 $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" style="display:none;" />'."\n";
93 }
Derek Allard958543a2010-07-22 14:10:26 -040094 }
95
Derek Allard2067d1a2008-11-13 22:59:24 +000096 return $form;
97 }
98}
99
100// ------------------------------------------------------------------------
101
Derek Allard2067d1a2008-11-13 22:59:24 +0000102if ( ! function_exists('form_open_multipart'))
103{
Timothy Warren01b129a2012-04-27 11:36:50 -0400104 /**
105 * Form Declaration - Multipart type
106 *
107 * Creates the opening portion of the form, but with "multipart/form-data".
108 *
109 * @param string the URI segments of the form destination
110 * @param array a key/value pair of attributes
111 * @param array a key/value pair hidden data
112 * @return string
113 */
Ben Edmunds98963b32011-08-20 14:17:16 -0500114 function form_open_multipart($action = '', $attributes = array(), $hidden = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 {
Derek Allard33d4b6a2010-01-17 07:23:00 +0000116 if (is_string($attributes))
117 {
118 $attributes .= ' enctype="multipart/form-data"';
119 }
120 else
121 {
122 $attributes['enctype'] = 'multipart/form-data';
123 }
124
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 return form_open($action, $attributes, $hidden);
126 }
127}
128
129// ------------------------------------------------------------------------
130
Derek Allard2067d1a2008-11-13 22:59:24 +0000131if ( ! function_exists('form_hidden'))
132{
Timothy Warren01b129a2012-04-27 11:36:50 -0400133 /**
134 * Hidden Input Field
135 *
136 * Generates hidden fields. You can pass a simple key/value string or
137 * an associative array with multiple values.
138 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200139 * @param mixed $name Field name
140 * @param string $value Field value
141 * @param bool $recursing
Timothy Warren01b129a2012-04-27 11:36:50 -0400142 * @return string
143 */
Robin Sowell57fe4102009-03-26 18:58:46 +0000144 function form_hidden($name, $value = '', $recursing = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000146 static $form;
147
148 if ($recursing === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000150 $form = "\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 }
152
Robin Sowell57fe4102009-03-26 18:58:46 +0000153 if (is_array($name))
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000155 foreach ($name as $key => $val)
156 {
157 form_hidden($key, $val, TRUE);
158 }
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200159
Robin Sowell57fe4102009-03-26 18:58:46 +0000160 return $form;
161 }
162
163 if ( ! is_array($value))
164 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200165 $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value)."\" />\n";
Robin Sowell57fe4102009-03-26 18:58:46 +0000166 }
167 else
168 {
169 foreach ($value as $k => $v)
170 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300171 $k = is_int($k) ? '' : $k;
Robin Sowell57fe4102009-03-26 18:58:46 +0000172 form_hidden($name.'['.$k.']', $v, TRUE);
173 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 }
175
176 return $form;
177 }
178}
179
180// ------------------------------------------------------------------------
181
Derek Allard2067d1a2008-11-13 22:59:24 +0000182if ( ! function_exists('form_input'))
183{
Timothy Warren01b129a2012-04-27 11:36:50 -0400184 /**
185 * Text Input Field
186 *
187 * @param mixed
188 * @param string
189 * @param string
190 * @return string
191 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 function form_input($data = '', $value = '', $extra = '')
193 {
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200194 $defaults = array(
195 'type' => 'text',
196 'name' => is_array($data) ? '' : $data,
197 'value' => $value
198 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000199
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200200 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 }
202}
203
204// ------------------------------------------------------------------------
205
Derek Allard2067d1a2008-11-13 22:59:24 +0000206if ( ! function_exists('form_password'))
207{
Timothy Warren01b129a2012-04-27 11:36:50 -0400208 /**
209 * Password Field
210 *
211 * Identical to the input function but adds the "password" type
212 *
213 * @param mixed
214 * @param string
215 * @param string
216 * @return string
217 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 function form_password($data = '', $value = '', $extra = '')
219 {
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200220 is_array($data) OR $data = array('name' => $data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 $data['type'] = 'password';
222 return form_input($data, $value, $extra);
223 }
224}
225
226// ------------------------------------------------------------------------
227
Derek Allard2067d1a2008-11-13 22:59:24 +0000228if ( ! function_exists('form_upload'))
229{
Timothy Warren01b129a2012-04-27 11:36:50 -0400230 /**
231 * Upload Field
232 *
233 * Identical to the input function but adds the "file" type
234 *
235 * @param mixed
236 * @param string
237 * @param string
238 * @return string
239 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 function form_upload($data = '', $value = '', $extra = '')
241 {
Bo-Yi Wu06ddcf02013-02-18 08:52:05 +0800242 $defaults = array('type' => 'file', 'name' => '');
Andrey Andreev99ba3a22013-02-15 22:42:22 +0200243 is_array($data) OR $data = array('name' => $data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 $data['type'] = 'file';
Andrey Andreev99ba3a22013-02-15 22:42:22 +0200245 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 }
247}
248
249// ------------------------------------------------------------------------
250
Derek Allard2067d1a2008-11-13 22:59:24 +0000251if ( ! function_exists('form_textarea'))
252{
Timothy Warren01b129a2012-04-27 11:36:50 -0400253 /**
254 * Textarea field
255 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200256 * @param mixed $data
257 * @param string $value
258 * @param string $extra
Timothy Warren01b129a2012-04-27 11:36:50 -0400259 * @return string
260 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 function form_textarea($data = '', $value = '', $extra = '')
262 {
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200263 $defaults = array(
264 'name' => is_array($data) ? '' : $data,
265 'cols' => '40',
266 'rows' => '10'
267 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000268
269 if ( ! is_array($data) OR ! isset($data['value']))
270 {
271 $val = $value;
272 }
273 else
274 {
Barry Mienydd671972010-10-04 16:33:58 +0200275 $val = $data['value'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 unset($data['value']); // textareas don't use the value attribute
277 }
Barry Mienydd671972010-10-04 16:33:58 +0200278
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200279 return '<textarea '._parse_form_attributes($data, $defaults).$extra.'>'.form_prep($val, TRUE)."</textarea>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 }
281}
282
283// ------------------------------------------------------------------------
284
Derek Jones68788d52010-03-05 10:11:31 -0600285if ( ! function_exists('form_multiselect'))
Derek Jones26399292009-04-08 16:14:17 +0000286{
Timothy Warren01b129a2012-04-27 11:36:50 -0400287 /**
288 * Multi-select menu
289 *
290 * @param string
291 * @param array
292 * @param mixed
293 * @param string
294 * @return string
295 */
Derek Jones26399292009-04-08 16:14:17 +0000296 function form_multiselect($name = '', $options = array(), $selected = array(), $extra = '')
297 {
298 if ( ! strpos($extra, 'multiple'))
299 {
300 $extra .= ' multiple="multiple"';
301 }
Barry Mienydd671972010-10-04 16:33:58 +0200302
Derek Jones26399292009-04-08 16:14:17 +0000303 return form_dropdown($name, $options, $selected, $extra);
304 }
305}
306
307// --------------------------------------------------------------------
308
Derek Allard2067d1a2008-11-13 22:59:24 +0000309if ( ! function_exists('form_dropdown'))
310{
Timothy Warren01b129a2012-04-27 11:36:50 -0400311 /**
312 * Drop-down Menu
313 *
Brennan Thompson40cd6002014-02-14 12:06:38 -0700314 * @param mixed $data
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200315 * @param mixed $options
316 * @param mixed $selected
317 * @param mixed $extra
Timothy Warren01b129a2012-04-27 11:36:50 -0400318 * @return string
319 */
Brennan Thompson40cd6002014-02-14 12:06:38 -0700320 function form_dropdown($data = '', $options = array(), $selected = array(), $extra = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 {
Brennan Thompson1d03ef42014-02-17 12:08:22 -0700322 $defaults = array('name' => is_array($data) ? '' : $data);
Brennan Thompson82c78a92014-02-17 10:02:19 -0700323
324 if (is_array($data) && isset($data['selected']))
Andrey Andreev93a83c72012-03-26 21:24:02 +0300325 {
Brennan Thompson40cd6002014-02-14 12:06:38 -0700326 $selected = $data['selected'];
327 unset($data['selected']); // selects don't have a selected attribute
Andrey Andreev93a83c72012-03-26 21:24:02 +0300328 }
329
Andrey Andreev582ebcb2012-10-27 00:52:15 +0300330 is_array($selected) OR $selected = array($selected);
Derek Allard2067d1a2008-11-13 22:59:24 +0000331
332 // If no selected state was submitted we will attempt to set it automatically
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200333 if (count($selected) === 0 && isset($_POST[$name]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200335 $selected = array($_POST[$name]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 }
Brennan Thompson82c78a92014-02-17 10:02:19 -0700337
Brennan Thompsonc8ba6632014-02-16 19:36:00 -0700338 if (is_array($data) && isset($data['options']))
Brennan Thompson40cd6002014-02-14 12:06:38 -0700339 {
340 $options = $data['options'];
341 unset($data['options']); // selects don't use an options attribute
342 }
Brennan Thompson21ef97e2014-02-16 11:01:03 -0700343
344 is_array($options) OR $options = array($options);
Brennan Thompson82c78a92014-02-17 10:02:19 -0700345
Brennan Thompson1d03ef42014-02-17 12:08:22 -0700346 $extra = _attributes_to_string($extra);
Brennan Thompson82c78a92014-02-17 10:02:19 -0700347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
Brennan Thompson82c78a92014-02-17 10:02:19 -0700349
Brennan Thompson1d03ef42014-02-17 12:08:22 -0700350 $form = '<select '.rtrim(_parse_form_attributes($data, $defaults)).$extra.$multiple.">\n";
Brennan Thompson82c78a92014-02-17 10:02:19 -0700351
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 foreach ($options as $key => $val)
353 {
354 $key = (string) $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000355
Andrey Andreev6b114ae2012-07-13 12:05:52 +0300356 if (is_array($val))
Derek Allard78a5fc92009-02-05 16:34:35 +0000357 {
Andrey Andreev6b114ae2012-07-13 12:05:52 +0300358 if (empty($val))
359 {
360 continue;
361 }
362
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200363 $form .= '<optgroup label="'.$key."\">\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000364
Derek Allard78a5fc92009-02-05 16:34:35 +0000365 foreach ($val as $optgroup_key => $optgroup_val)
366 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300367 $sel = in_array($optgroup_key, $selected) ? ' selected="selected"' : '';
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200368 $form .= '<option value="'.form_prep($optgroup_key).'"'.$sel.'>'
Brennan Thompson82c78a92014-02-17 10:02:19 -0700369 .(string) $optgroup_val."</option>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000370 }
371
Andrey Andreev93a83c72012-03-26 21:24:02 +0300372 $form .= "</optgroup>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000373 }
374 else
375 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200376 $form .= '<option value="'.form_prep($key).'"'
Brennan Thompson82c78a92014-02-17 10:02:19 -0700377 .(in_array($key, $selected) ? ' selected="selected"' : '').'>'
378 .(string) $val."</option>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000379 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 }
Brennan Thompson82c78a92014-02-17 10:02:19 -0700381
Andrey Andreev93a83c72012-03-26 21:24:02 +0300382 return $form."</select>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 }
384}
385
386// ------------------------------------------------------------------------
387
Derek Allard2067d1a2008-11-13 22:59:24 +0000388if ( ! function_exists('form_checkbox'))
389{
Timothy Warren01b129a2012-04-27 11:36:50 -0400390 /**
391 * Checkbox Field
392 *
393 * @param mixed
394 * @param string
395 * @param bool
396 * @param string
397 * @return string
398 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
400 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300401 $defaults = array('type' => 'checkbox', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000402
Andrey Andreev93a83c72012-03-26 21:24:02 +0300403 if (is_array($data) && array_key_exists('checked', $data))
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 {
405 $checked = $data['checked'];
406
Michiel Vugteveen910ff7a2012-06-06 20:03:14 +0200407 if ($checked == FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 {
409 unset($data['checked']);
410 }
411 else
412 {
413 $data['checked'] = 'checked';
414 }
415 }
416
Michiel Vugteveen910ff7a2012-06-06 20:03:14 +0200417 if ($checked == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 {
419 $defaults['checked'] = 'checked';
420 }
421 else
422 {
423 unset($defaults['checked']);
424 }
425
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200426 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 }
428}
429
430// ------------------------------------------------------------------------
431
Derek Allard2067d1a2008-11-13 22:59:24 +0000432if ( ! function_exists('form_radio'))
433{
Timothy Warren01b129a2012-04-27 11:36:50 -0400434 /**
435 * Radio Button
436 *
437 * @param mixed
438 * @param string
439 * @param bool
440 * @param string
441 * @return string
442 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
444 {
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200445 is_array($data) OR $data = array('name' => $data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 $data['type'] = 'radio';
447 return form_checkbox($data, $value, $checked, $extra);
448 }
449}
450
451// ------------------------------------------------------------------------
452
Derek Allard2067d1a2008-11-13 22:59:24 +0000453if ( ! function_exists('form_submit'))
Barry Mienydd671972010-10-04 16:33:58 +0200454{
Timothy Warren01b129a2012-04-27 11:36:50 -0400455 /**
456 * Submit Button
457 *
458 * @param mixed
459 * @param string
460 * @param string
461 * @return string
462 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 function form_submit($data = '', $value = '', $extra = '')
464 {
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200465 $defaults = array(
466 'type' => 'submit',
467 'name' => is_array($data) ? '' : $data,
468 'value' => $value
469 );
470
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200471 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 }
473}
474
475// ------------------------------------------------------------------------
476
Derek Allard2067d1a2008-11-13 22:59:24 +0000477if ( ! function_exists('form_reset'))
478{
Timothy Warren01b129a2012-04-27 11:36:50 -0400479 /**
480 * Reset Button
481 *
482 * @param mixed
483 * @param string
484 * @param string
485 * @return string
486 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 function form_reset($data = '', $value = '', $extra = '')
488 {
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200489 $defaults = array(
490 'type' => 'reset',
491 'name' => is_array($data) ? '' : $data,
492 'value' => $value
493 );
494
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200495 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 }
497}
498
499// ------------------------------------------------------------------------
500
Derek Allard2067d1a2008-11-13 22:59:24 +0000501if ( ! function_exists('form_button'))
502{
Timothy Warren01b129a2012-04-27 11:36:50 -0400503 /**
504 * Form Button
505 *
506 * @param mixed
507 * @param string
508 * @param string
509 * @return string
510 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 function form_button($data = '', $content = '', $extra = '')
512 {
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200513 $defaults = array(
514 'name' => is_array($data) ? '' : $data,
515 'type' => 'button'
516 );
517
Andrey Andreev93a83c72012-03-26 21:24:02 +0300518 if (is_array($data) && isset($data['content']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 {
520 $content = $data['content'];
521 unset($data['content']); // content is not an attribute
522 }
523
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200524 return '<button '._parse_form_attributes($data, $defaults).$extra.'>'.$content."</button>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 }
526}
527
528// ------------------------------------------------------------------------
529
Derek Allard2067d1a2008-11-13 22:59:24 +0000530if ( ! function_exists('form_label'))
531{
Timothy Warren01b129a2012-04-27 11:36:50 -0400532 /**
533 * Form Label Tag
534 *
535 * @param string The text to appear onscreen
536 * @param string The id the label applies to
537 * @param string Additional attributes
538 * @return string
539 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 function form_label($label_text = '', $id = '', $attributes = array())
541 {
542
543 $label = '<label';
544
Alex Bilbie773ccc32012-06-02 11:11:08 +0100545 if ($id !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300547 $label .= ' for="'.$id.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 }
549
Andrey Andreev93a83c72012-03-26 21:24:02 +0300550 if (is_array($attributes) && count($attributes) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 {
552 foreach ($attributes as $key => $val)
553 {
554 $label .= ' '.$key.'="'.$val.'"';
555 }
556 }
557
Andrey Andreev93a83c72012-03-26 21:24:02 +0300558 return $label.'>'.$label_text.'</label>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 }
560}
561
562// ------------------------------------------------------------------------
Timothy Warren01b129a2012-04-27 11:36:50 -0400563
Derek Allard2067d1a2008-11-13 22:59:24 +0000564if ( ! function_exists('form_fieldset'))
565{
Timothy Warren01b129a2012-04-27 11:36:50 -0400566 /**
567 * Fieldset Tag
568 *
569 * Used to produce <fieldset><legend>text</legend>. To close fieldset
570 * use form_fieldset_close()
571 *
572 * @param string The legend text
vlakoffea19bc42013-07-27 10:07:43 +0200573 * @param array Additional attributes
Timothy Warren01b129a2012-04-27 11:36:50 -0400574 * @return string
575 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 function form_fieldset($legend_text = '', $attributes = array())
577 {
vlakoffea19bc42013-07-27 10:07:43 +0200578 $fieldset = '<fieldset'._attributes_to_string($attributes).">\n";
Alex Bilbie773ccc32012-06-02 11:11:08 +0100579 if ($legend_text !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300581 return $fieldset.'<legend>'.$legend_text."</legend>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 }
583
584 return $fieldset;
585 }
586}
587
588// ------------------------------------------------------------------------
589
Derek Allard2067d1a2008-11-13 22:59:24 +0000590if ( ! function_exists('form_fieldset_close'))
591{
Timothy Warren01b129a2012-04-27 11:36:50 -0400592 /**
593 * Fieldset Close Tag
594 *
595 * @param string
596 * @return string
597 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 function form_fieldset_close($extra = '')
599 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300600 return '</fieldset>'.$extra;
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 }
602}
603
604// ------------------------------------------------------------------------
605
Derek Allard2067d1a2008-11-13 22:59:24 +0000606if ( ! function_exists('form_close'))
607{
Timothy Warren01b129a2012-04-27 11:36:50 -0400608 /**
609 * Form Close Tag
610 *
611 * @param string
612 * @return string
613 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 function form_close($extra = '')
615 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300616 return '</form>'.$extra;
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 }
618}
619
620// ------------------------------------------------------------------------
621
Derek Allard2067d1a2008-11-13 22:59:24 +0000622if ( ! function_exists('form_prep'))
623{
Timothy Warren01b129a2012-04-27 11:36:50 -0400624 /**
625 * Form Prep
626 *
627 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
628 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200629 * @param string|string[] $str Value to escape
630 * @param bool $is_textarea Whether we're escaping for a textarea element
631 * @return string|string[] Escaped values
Timothy Warren01b129a2012-04-27 11:36:50 -0400632 */
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200633 function form_prep($str = '', $is_textarea = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200635 if (is_array($str))
636 {
637 foreach (array_keys($str) as $key)
638 {
639 $str[$key] = form_prep($str[$key], $is_textarea);
640 }
641
642 return $str;
643 }
644
645 if ($is_textarea === TRUE)
646 {
647 return str_replace(array('<', '>'), array('&lt;', '&gt;'), stripslashes($str));
648 }
649
Andrey Andreev075f6fa2012-11-01 15:18:44 +0200650 return str_replace(array("'", '"'), array('&#39;', '&quot;'), stripslashes($str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 }
652}
653
654// ------------------------------------------------------------------------
655
Derek Allard2067d1a2008-11-13 22:59:24 +0000656if ( ! function_exists('set_value'))
657{
Timothy Warren01b129a2012-04-27 11:36:50 -0400658 /**
659 * Form Value
660 *
661 * Grabs a value from the POST array for the specified field so you can
662 * re-populate an input field or textarea. If Form Validation
663 * is active it retrieves the info from the validation class
664 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200665 * @param string $field Field name
666 * @param string $default Default value
667 * @param bool $is_textarea Whether the field is a textarea element
668 * @return string
Timothy Warren01b129a2012-04-27 11:36:50 -0400669 */
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200670 function set_value($field = '', $default = '', $is_textarea = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 {
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530672 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000673
nisheeth-barthwal47ea5a82013-03-26 18:57:28 +0530674 $value = (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
675 ? $CI->form_validation->set_value($field, $default)
676 : $CI->input->post($field, FALSE);
677
678 return form_prep($value === NULL ? $default : $value, $is_textarea);
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 }
680}
681
682// ------------------------------------------------------------------------
683
Derek Allard2067d1a2008-11-13 22:59:24 +0000684if ( ! function_exists('set_select'))
685{
Timothy Warren01b129a2012-04-27 11:36:50 -0400686 /**
687 * Set Select
688 *
689 * Let's you set the selected value of a <select> menu via data in the POST array.
690 * If Form Validation is active it retrieves the info from the validation class
691 *
692 * @param string
693 * @param string
694 * @param bool
695 * @return string
696 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 function set_select($field = '', $value = '', $default = FALSE)
698 {
Andrey Andreev67f6a5e2013-09-13 16:21:31 +0300699 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000700
Andrey Andreev67f6a5e2013-09-13 16:21:31 +0300701 if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 {
Andrey Andreev67f6a5e2013-09-13 16:21:31 +0300703 return $CI->form_validation->set_select($field, $value, $default);
704 }
705 elseif (($input = $CI->input->post($field, FALSE)) === NULL)
706 {
707 return ($default === TRUE) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 }
Andrey Andreeva587a932013-10-23 19:57:46 +0300709
710 $value = (string) $value;
711 if (is_array($input))
Andrey Andreevd3b7e242013-09-13 18:36:29 +0300712 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300713 // Note: in_array('', array(0)) returns TRUE, do not use it
714 foreach ($input as &$v)
715 {
716 if ($value === $v)
717 {
718 return ' selected="selected"';
719 }
720 }
721
722 return '';
Andrey Andreevd3b7e242013-09-13 18:36:29 +0300723 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000724
Andrey Andreevd3b7e242013-09-13 18:36:29 +0300725 return ($input === $value) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000726 }
727}
728
729// ------------------------------------------------------------------------
730
Derek Allard2067d1a2008-11-13 22:59:24 +0000731if ( ! function_exists('set_checkbox'))
732{
Timothy Warren01b129a2012-04-27 11:36:50 -0400733 /**
734 * Set Checkbox
735 *
736 * Let's you set the selected value of a checkbox via the value in the POST array.
737 * If Form Validation is active it retrieves the info from the validation class
738 *
739 * @param string
740 * @param string
741 * @param bool
742 * @return string
743 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000744 function set_checkbox($field = '', $value = '', $default = FALSE)
745 {
Andrey Andreevae50f552013-09-13 16:17:41 +0300746 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000747
Andrey Andreevae50f552013-09-13 16:17:41 +0300748 if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
Barry Mienydd671972010-10-04 16:33:58 +0200749 {
Andrey Andreevae50f552013-09-13 16:17:41 +0300750 return $CI->form_validation->set_checkbox($field, $value, $default);
751 }
752 elseif (($input = $CI->input->post($field, FALSE)) === NULL)
753 {
754 return ($default === TRUE) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000755 }
Andrey Andreeva587a932013-10-23 19:57:46 +0300756
757 $value = (string) $value;
758 if (is_array($input))
Andrey Andreeve8a23a52013-09-13 18:29:29 +0300759 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300760 // Note: in_array('', array(0)) returns TRUE, do not use it
761 foreach ($input as &$v)
762 {
763 if ($value === $v)
764 {
765 return ' checked="checked"';
766 }
767 }
768
769 return '';
Andrey Andreeve8a23a52013-09-13 18:29:29 +0300770 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000771
Andrey Andreevae50f552013-09-13 16:17:41 +0300772 return ($input === $value) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 }
774}
775
776// ------------------------------------------------------------------------
777
Derek Allard2067d1a2008-11-13 22:59:24 +0000778if ( ! function_exists('set_radio'))
779{
Timothy Warren01b129a2012-04-27 11:36:50 -0400780 /**
781 * Set Radio
782 *
783 * Let's you set the selected value of a radio field via info in the POST array.
784 * If Form Validation is active it retrieves the info from the validation class
785 *
Andrey Andreevae50f552013-09-13 16:17:41 +0300786 * @param string $field
787 * @param string $value
788 * @param bool $default
Timothy Warren01b129a2012-04-27 11:36:50 -0400789 * @return string
790 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000791 function set_radio($field = '', $value = '', $default = FALSE)
792 {
Andrey Andreevae50f552013-09-13 16:17:41 +0300793 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000794
Andrey Andreevae50f552013-09-13 16:17:41 +0300795 if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
Derek Allard2067d1a2008-11-13 22:59:24 +0000796 {
Andrey Andreevae50f552013-09-13 16:17:41 +0300797 return $CI->form_validation->set_radio($field, $value, $default);
798 }
799 elseif (($input = $CI->input->post($field, FALSE)) === NULL)
800 {
801 return ($default === TRUE) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 }
803
Andrey Andreeva587a932013-10-23 19:57:46 +0300804 return ($input === (string) $value) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 }
806}
807
808// ------------------------------------------------------------------------
809
Derek Allard2067d1a2008-11-13 22:59:24 +0000810if ( ! function_exists('form_error'))
811{
Timothy Warren01b129a2012-04-27 11:36:50 -0400812 /**
813 * Form Error
814 *
815 * Returns the error for a specific form field. This is a helper for the
816 * form validation class.
817 *
818 * @param string
819 * @param string
820 * @param string
821 * @return string
822 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 function form_error($field = '', $prefix = '', $suffix = '')
824 {
825 if (FALSE === ($OBJ =& _get_validation_object()))
826 {
827 return '';
828 }
829
830 return $OBJ->error($field, $prefix, $suffix);
831 }
832}
833
834// ------------------------------------------------------------------------
835
Derek Allard2067d1a2008-11-13 22:59:24 +0000836if ( ! function_exists('validation_errors'))
837{
Timothy Warren01b129a2012-04-27 11:36:50 -0400838 /**
839 * Validation Error String
840 *
841 * Returns all the errors associated with a form submission. This is a helper
842 * function for the form validation class.
843 *
844 * @param string
845 * @param string
846 * @return string
847 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 function validation_errors($prefix = '', $suffix = '')
849 {
850 if (FALSE === ($OBJ =& _get_validation_object()))
Greg Akerc83bea62011-04-23 12:12:57 -0500851 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000852 return '';
853 }
854
855 return $OBJ->error_string($prefix, $suffix);
856 }
857}
858
859// ------------------------------------------------------------------------
860
Derek Allard2067d1a2008-11-13 22:59:24 +0000861if ( ! function_exists('_parse_form_attributes'))
862{
Timothy Warren01b129a2012-04-27 11:36:50 -0400863 /**
864 * Parse the form attributes
865 *
866 * Helper function used by some of the form helpers
867 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200868 * @param array $attributes List of attributes
869 * @param array $default Default values
Timothy Warren01b129a2012-04-27 11:36:50 -0400870 * @return string
871 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 function _parse_form_attributes($attributes, $default)
873 {
874 if (is_array($attributes))
875 {
876 foreach ($default as $key => $val)
877 {
878 if (isset($attributes[$key]))
879 {
880 $default[$key] = $attributes[$key];
881 unset($attributes[$key]);
882 }
883 }
884
885 if (count($attributes) > 0)
886 {
887 $default = array_merge($default, $attributes);
888 }
889 }
890
891 $att = '';
Barry Mienydd671972010-10-04 16:33:58 +0200892
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 foreach ($default as $key => $val)
894 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100895 if ($key === 'value')
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200897 $val = form_prep($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 }
Andrey Andreev60b97142012-10-25 16:59:17 +0300899 elseif ($key === 'name' && ! strlen($default['name']))
900 {
901 continue;
902 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000903
Andrey Andreev93a83c72012-03-26 21:24:02 +0300904 $att .= $key.'="'.$val.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 }
906
907 return $att;
908 }
909}
910
911// ------------------------------------------------------------------------
912
Derek Allard2067d1a2008-11-13 22:59:24 +0000913if ( ! function_exists('_attributes_to_string'))
914{
Timothy Warren01b129a2012-04-27 11:36:50 -0400915 /**
916 * Attributes To String
917 *
918 * Helper function used by some of the form helpers
919 *
920 * @param mixed
Timothy Warren01b129a2012-04-27 11:36:50 -0400921 * @return string
922 */
vlakoffea19bc42013-07-27 10:07:43 +0200923 function _attributes_to_string($attributes)
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 {
vlakoffbb8b0892013-07-28 22:35:04 +0200925 if (empty($attributes))
926 {
927 return '';
928 }
929
vlakoffea19bc42013-07-27 10:07:43 +0200930 if (is_object($attributes))
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300932 $attributes = (array) $attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 }
934
vlakoffea19bc42013-07-27 10:07:43 +0200935 if (is_array($attributes))
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 {
Derek Allard3241d732009-09-17 12:17:45 +0000937 $atts = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000938
Derek Allard3241d732009-09-17 12:17:45 +0000939 foreach ($attributes as $key => $val)
940 {
941 $atts .= ' '.$key.'="'.$val.'"';
942 }
943
944 return $atts;
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 }
vlakoffea19bc42013-07-27 10:07:43 +0200946
vlakofff7464752013-07-28 22:23:21 +0200947 if (is_string($attributes))
948 {
Brennan Thompson7a772e52014-02-16 19:22:54 -0700949 return ' '.$attributes;
vlakofff7464752013-07-28 22:23:21 +0200950 }
951
vlakoffea19bc42013-07-27 10:07:43 +0200952 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 }
954}
955
956// ------------------------------------------------------------------------
957
Derek Allard2067d1a2008-11-13 22:59:24 +0000958if ( ! function_exists('_get_validation_object'))
959{
Timothy Warren01b129a2012-04-27 11:36:50 -0400960 /**
961 * Validation Object
962 *
963 * Determines what the form validation class was instantiated as, fetches
964 * the object and returns it.
965 *
966 * @return mixed
967 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 function &_get_validation_object()
969 {
970 $CI =& get_instance();
971
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500972 // We set this as a variable since we're returning by reference.
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 $return = FALSE;
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200974
Andrey Andreev519f87a2013-07-23 17:16:10 +0300975 if (FALSE !== ($object = $CI->load->is_loaded('Form_validation')))
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 {
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500977 if ( ! isset($CI->$object) OR ! is_object($CI->$object))
978 {
979 return $return;
980 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200981
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500982 return $CI->$object;
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200984
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500985 return $return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 }
987}
988
Derek Allard2067d1a2008-11-13 22:59:24 +0000989/* End of file form_helper.php */
Zachary Cardoza9f27a3e2013-03-23 21:59:20 -0700990/* Location: ./system/helpers/form_helper.php */