blob: 200da8ac5889ac098389b351d2782e2b4319ec9c [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
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, 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 */
Derek Allard2067d1a2008-11-13 22:59:24 +000053 function form_open($action = '', $attributes = '', $hidden = array())
54 {
55 $CI =& get_instance();
56
Alex Bilbie773ccc32012-06-02 11:11:08 +010057 if ($attributes === '')
Derek Allard2067d1a2008-11-13 22:59:24 +000058 {
Derek Allard3241d732009-09-17 12:17:45 +000059 $attributes = 'method="post"';
Derek Allard2067d1a2008-11-13 22:59:24 +000060 }
61
Phil Sturgeon9d0e6172011-04-02 12:43:55 +010062 // If an action is not a full URL then turn it into one
Phil Sturgeon133beaf2011-03-10 16:38:32 +000063 if ($action && strpos($action, '://') === FALSE)
64 {
Phil Sturgeon52e73182011-03-11 10:17:01 +000065 $action = $CI->config->site_url($action);
Phil Sturgeon133beaf2011-03-10 16:38:32 +000066 }
Andrey Andreev3c32a112012-06-16 18:05:34 +030067 elseif ( ! $action)
68 {
69 // If no action is provided then set to the current url
70 $action = $CI->config->site_url($CI->uri->uri_string());
71 }
Phil Sturgeon9d0e6172011-04-02 12:43:55 +010072
Andrey Andreev8bf6bb62012-01-06 16:11:04 +020073 $form = '<form action="'.$action.'"'._attributes_to_string($attributes, TRUE).">\n";
Barry Mienydd671972010-10-04 16:33:58 +020074
Andrey Andreev93a83c72012-03-26 21:24:02 +030075 // Add CSRF field if enabled, but leave it out for GET requests and requests to external websites
76 if ($CI->config->item('csrf_protection') === TRUE && ! (strpos($action, $CI->config->base_url()) === FALSE OR strpos($form, 'method="get"')))
Derek Allard958543a2010-07-22 14:10:26 -040077 {
Greg Aker1f6f0ab2011-04-07 18:24:53 -050078 $hidden[$CI->security->get_csrf_token_name()] = $CI->security->get_csrf_hash();
Greg Aker28b425a2010-09-15 11:43:23 -050079 }
80
Andrey Andreev93a83c72012-03-26 21:24:02 +030081 if (is_array($hidden) && count($hidden) > 0)
Greg Aker28b425a2010-09-15 11:43:23 -050082 {
Andrey Andreev3c32a112012-06-16 18:05:34 +030083 $form .= '<div style="display:none;">'.form_hidden($hidden).'</div>';
Derek Allard958543a2010-07-22 14:10:26 -040084 }
85
Derek Allard2067d1a2008-11-13 22:59:24 +000086 return $form;
87 }
88}
89
90// ------------------------------------------------------------------------
91
Derek Allard2067d1a2008-11-13 22:59:24 +000092if ( ! function_exists('form_open_multipart'))
93{
Timothy Warren01b129a2012-04-27 11:36:50 -040094 /**
95 * Form Declaration - Multipart type
96 *
97 * Creates the opening portion of the form, but with "multipart/form-data".
98 *
99 * @param string the URI segments of the form destination
100 * @param array a key/value pair of attributes
101 * @param array a key/value pair hidden data
102 * @return string
103 */
Ben Edmunds98963b32011-08-20 14:17:16 -0500104 function form_open_multipart($action = '', $attributes = array(), $hidden = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 {
Derek Allard33d4b6a2010-01-17 07:23:00 +0000106 if (is_string($attributes))
107 {
108 $attributes .= ' enctype="multipart/form-data"';
109 }
110 else
111 {
112 $attributes['enctype'] = 'multipart/form-data';
113 }
114
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 return form_open($action, $attributes, $hidden);
116 }
117}
118
119// ------------------------------------------------------------------------
120
Derek Allard2067d1a2008-11-13 22:59:24 +0000121if ( ! function_exists('form_hidden'))
122{
Timothy Warren01b129a2012-04-27 11:36:50 -0400123 /**
124 * Hidden Input Field
125 *
126 * Generates hidden fields. You can pass a simple key/value string or
127 * an associative array with multiple values.
128 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200129 * @param mixed $name Field name
130 * @param string $value Field value
131 * @param bool $recursing
Timothy Warren01b129a2012-04-27 11:36:50 -0400132 * @return string
133 */
Robin Sowell57fe4102009-03-26 18:58:46 +0000134 function form_hidden($name, $value = '', $recursing = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000136 static $form;
137
138 if ($recursing === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000140 $form = "\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 }
142
Robin Sowell57fe4102009-03-26 18:58:46 +0000143 if (is_array($name))
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000145 foreach ($name as $key => $val)
146 {
147 form_hidden($key, $val, TRUE);
148 }
149 return $form;
150 }
151
152 if ( ! is_array($value))
153 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200154 $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value)."\" />\n";
Robin Sowell57fe4102009-03-26 18:58:46 +0000155 }
156 else
157 {
158 foreach ($value as $k => $v)
159 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300160 $k = is_int($k) ? '' : $k;
Robin Sowell57fe4102009-03-26 18:58:46 +0000161 form_hidden($name.'['.$k.']', $v, TRUE);
162 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 }
164
165 return $form;
166 }
167}
168
169// ------------------------------------------------------------------------
170
Derek Allard2067d1a2008-11-13 22:59:24 +0000171if ( ! function_exists('form_input'))
172{
Timothy Warren01b129a2012-04-27 11:36:50 -0400173 /**
174 * Text Input Field
175 *
176 * @param mixed
177 * @param string
178 * @param string
179 * @return string
180 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 function form_input($data = '', $value = '', $extra = '')
182 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300183 $defaults = array('type' => 'text', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000184
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200185 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 }
187}
188
189// ------------------------------------------------------------------------
190
Derek Allard2067d1a2008-11-13 22:59:24 +0000191if ( ! function_exists('form_password'))
192{
Timothy Warren01b129a2012-04-27 11:36:50 -0400193 /**
194 * Password Field
195 *
196 * Identical to the input function but adds the "password" type
197 *
198 * @param mixed
199 * @param string
200 * @param string
201 * @return string
202 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 function form_password($data = '', $value = '', $extra = '')
204 {
205 if ( ! is_array($data))
206 {
207 $data = array('name' => $data);
208 }
209
210 $data['type'] = 'password';
211 return form_input($data, $value, $extra);
212 }
213}
214
215// ------------------------------------------------------------------------
216
Derek Allard2067d1a2008-11-13 22:59:24 +0000217if ( ! function_exists('form_upload'))
218{
Timothy Warren01b129a2012-04-27 11:36:50 -0400219 /**
220 * Upload Field
221 *
222 * Identical to the input function but adds the "file" type
223 *
224 * @param mixed
225 * @param string
226 * @param string
227 * @return string
228 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 function form_upload($data = '', $value = '', $extra = '')
230 {
Andrey Andreev99ba3a22013-02-15 22:42:22 +0200231 $default = array('type' => 'file', 'name' => '');
232 is_array($data) OR $data = array('name' => $data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 $data['type'] = 'file';
Andrey Andreev99ba3a22013-02-15 22:42:22 +0200234 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 }
236}
237
238// ------------------------------------------------------------------------
239
Derek Allard2067d1a2008-11-13 22:59:24 +0000240if ( ! function_exists('form_textarea'))
241{
Timothy Warren01b129a2012-04-27 11:36:50 -0400242 /**
243 * Textarea field
244 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200245 * @param mixed $data
246 * @param string $value
247 * @param string $extra
Timothy Warren01b129a2012-04-27 11:36:50 -0400248 * @return string
249 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 function form_textarea($data = '', $value = '', $extra = '')
251 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300252 $defaults = array('name' => ( ! is_array($data) ? $data : ''), 'cols' => '40', 'rows' => '10');
Derek Allard2067d1a2008-11-13 22:59:24 +0000253
254 if ( ! is_array($data) OR ! isset($data['value']))
255 {
256 $val = $value;
257 }
258 else
259 {
Barry Mienydd671972010-10-04 16:33:58 +0200260 $val = $data['value'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 unset($data['value']); // textareas don't use the value attribute
262 }
Barry Mienydd671972010-10-04 16:33:58 +0200263
Andrey Andreev93a83c72012-03-26 21:24:02 +0300264 $name = is_array($data) ? $data['name'] : $data;
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200265 return '<textarea '._parse_form_attributes($data, $defaults).$extra.'>'.form_prep($val, TRUE)."</textarea>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 }
267}
268
269// ------------------------------------------------------------------------
270
Derek Jones68788d52010-03-05 10:11:31 -0600271if ( ! function_exists('form_multiselect'))
Derek Jones26399292009-04-08 16:14:17 +0000272{
Timothy Warren01b129a2012-04-27 11:36:50 -0400273 /**
274 * Multi-select menu
275 *
276 * @param string
277 * @param array
278 * @param mixed
279 * @param string
280 * @return string
281 */
Derek Jones26399292009-04-08 16:14:17 +0000282 function form_multiselect($name = '', $options = array(), $selected = array(), $extra = '')
283 {
284 if ( ! strpos($extra, 'multiple'))
285 {
286 $extra .= ' multiple="multiple"';
287 }
Barry Mienydd671972010-10-04 16:33:58 +0200288
Derek Jones26399292009-04-08 16:14:17 +0000289 return form_dropdown($name, $options, $selected, $extra);
290 }
291}
292
293// --------------------------------------------------------------------
294
Derek Allard2067d1a2008-11-13 22:59:24 +0000295if ( ! function_exists('form_dropdown'))
296{
Timothy Warren01b129a2012-04-27 11:36:50 -0400297 /**
298 * Drop-down Menu
299 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200300 * @param mixed $name
301 * @param mixed $options
302 * @param mixed $selected
303 * @param mixed $extra
Timothy Warren01b129a2012-04-27 11:36:50 -0400304 * @return string
305 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
307 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300308 // If name is really an array then we'll call the function again using the array
309 if (is_array($name) && isset($name['name']))
310 {
311 isset($name['options']) OR $name['options'] = array();
312 isset($name['selected']) OR $name['selected'] = array();
313 isset($name['extra']) OR $name['extra'] = array();
314
315 return form_dropdown($name['name'], $name['options'], $name['selected'], $name['extra']);
316 }
317
Andrey Andreev582ebcb2012-10-27 00:52:15 +0300318 is_array($selected) OR $selected = array($selected);
Derek Allard2067d1a2008-11-13 22:59:24 +0000319
320 // If no selected state was submitted we will attempt to set it automatically
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200321 if (count($selected) === 0 && isset($_POST[$name]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200323 $selected = array($_POST[$name]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 }
325
Michiel Vugteveen9640a032012-06-06 20:20:27 +0200326 if ($extra != '')
327 {
328 $extra = ' '.$extra;
329 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000330
331 $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
332
333 $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
Robin Sowell57fe4102009-03-26 18:58:46 +0000334
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 foreach ($options as $key => $val)
336 {
337 $key = (string) $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000338
Andrey Andreev6b114ae2012-07-13 12:05:52 +0300339 if (is_array($val))
Derek Allard78a5fc92009-02-05 16:34:35 +0000340 {
Andrey Andreev6b114ae2012-07-13 12:05:52 +0300341 if (empty($val))
342 {
343 continue;
344 }
345
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200346 $form .= '<optgroup label="'.$key."\">\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000347
Derek Allard78a5fc92009-02-05 16:34:35 +0000348 foreach ($val as $optgroup_key => $optgroup_val)
349 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300350 $sel = in_array($optgroup_key, $selected) ? ' selected="selected"' : '';
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200351 $form .= '<option value="'.form_prep($optgroup_key).'"'.$sel.'>'
Andrey Andreev582ebcb2012-10-27 00:52:15 +0300352 .(string) $optgroup_val."</option>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000353 }
354
Andrey Andreev93a83c72012-03-26 21:24:02 +0300355 $form .= "</optgroup>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000356 }
357 else
358 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200359 $form .= '<option value="'.form_prep($key).'"'
Andrey Andreev582ebcb2012-10-27 00:52:15 +0300360 .(in_array($key, $selected) ? ' selected="selected"' : '').'>'
361 .(string) $val."</option>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000362 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 }
364
Andrey Andreev93a83c72012-03-26 21:24:02 +0300365 return $form."</select>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 }
367}
368
369// ------------------------------------------------------------------------
370
Derek Allard2067d1a2008-11-13 22:59:24 +0000371if ( ! function_exists('form_checkbox'))
372{
Timothy Warren01b129a2012-04-27 11:36:50 -0400373 /**
374 * Checkbox Field
375 *
376 * @param mixed
377 * @param string
378 * @param bool
379 * @param string
380 * @return string
381 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
383 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300384 $defaults = array('type' => 'checkbox', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000385
Andrey Andreev93a83c72012-03-26 21:24:02 +0300386 if (is_array($data) && array_key_exists('checked', $data))
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 {
388 $checked = $data['checked'];
389
Michiel Vugteveen910ff7a2012-06-06 20:03:14 +0200390 if ($checked == FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 {
392 unset($data['checked']);
393 }
394 else
395 {
396 $data['checked'] = 'checked';
397 }
398 }
399
Michiel Vugteveen910ff7a2012-06-06 20:03:14 +0200400 if ($checked == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 {
402 $defaults['checked'] = 'checked';
403 }
404 else
405 {
406 unset($defaults['checked']);
407 }
408
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200409 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 }
411}
412
413// ------------------------------------------------------------------------
414
Derek Allard2067d1a2008-11-13 22:59:24 +0000415if ( ! function_exists('form_radio'))
416{
Timothy Warren01b129a2012-04-27 11:36:50 -0400417 /**
418 * Radio Button
419 *
420 * @param mixed
421 * @param string
422 * @param bool
423 * @param string
424 * @return string
425 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
427 {
428 if ( ! is_array($data))
Barry Mienydd671972010-10-04 16:33:58 +0200429 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 $data = array('name' => $data);
431 }
432
433 $data['type'] = 'radio';
434 return form_checkbox($data, $value, $checked, $extra);
435 }
436}
437
438// ------------------------------------------------------------------------
439
Derek Allard2067d1a2008-11-13 22:59:24 +0000440if ( ! function_exists('form_submit'))
Barry Mienydd671972010-10-04 16:33:58 +0200441{
Timothy Warren01b129a2012-04-27 11:36:50 -0400442 /**
443 * Submit Button
444 *
445 * @param mixed
446 * @param string
447 * @param string
448 * @return string
449 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 function form_submit($data = '', $value = '', $extra = '')
451 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300452 $defaults = array('type' => 'submit', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200453 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 }
455}
456
457// ------------------------------------------------------------------------
458
Derek Allard2067d1a2008-11-13 22:59:24 +0000459if ( ! function_exists('form_reset'))
460{
Timothy Warren01b129a2012-04-27 11:36:50 -0400461 /**
462 * Reset Button
463 *
464 * @param mixed
465 * @param string
466 * @param string
467 * @return string
468 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 function form_reset($data = '', $value = '', $extra = '')
470 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300471 $defaults = array('type' => 'reset', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200472 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 }
474}
475
476// ------------------------------------------------------------------------
477
Derek Allard2067d1a2008-11-13 22:59:24 +0000478if ( ! function_exists('form_button'))
479{
Timothy Warren01b129a2012-04-27 11:36:50 -0400480 /**
481 * Form Button
482 *
483 * @param mixed
484 * @param string
485 * @param string
486 * @return string
487 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 function form_button($data = '', $content = '', $extra = '')
489 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300490 $defaults = array('name' => ( ! is_array($data) ? $data : ''), 'type' => 'button');
491 if (is_array($data) && isset($data['content']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 {
493 $content = $data['content'];
494 unset($data['content']); // content is not an attribute
495 }
496
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200497 return '<button '._parse_form_attributes($data, $defaults).$extra.'>'.$content."</button>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 }
499}
500
501// ------------------------------------------------------------------------
502
Derek Allard2067d1a2008-11-13 22:59:24 +0000503if ( ! function_exists('form_label'))
504{
Timothy Warren01b129a2012-04-27 11:36:50 -0400505 /**
506 * Form Label Tag
507 *
508 * @param string The text to appear onscreen
509 * @param string The id the label applies to
510 * @param string Additional attributes
511 * @return string
512 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 function form_label($label_text = '', $id = '', $attributes = array())
514 {
515
516 $label = '<label';
517
Alex Bilbie773ccc32012-06-02 11:11:08 +0100518 if ($id !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300520 $label .= ' for="'.$id.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 }
522
Andrey Andreev93a83c72012-03-26 21:24:02 +0300523 if (is_array($attributes) && count($attributes) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 {
525 foreach ($attributes as $key => $val)
526 {
527 $label .= ' '.$key.'="'.$val.'"';
528 }
529 }
530
Andrey Andreev93a83c72012-03-26 21:24:02 +0300531 return $label.'>'.$label_text.'</label>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 }
533}
534
535// ------------------------------------------------------------------------
Timothy Warren01b129a2012-04-27 11:36:50 -0400536
Derek Allard2067d1a2008-11-13 22:59:24 +0000537if ( ! function_exists('form_fieldset'))
538{
Timothy Warren01b129a2012-04-27 11:36:50 -0400539 /**
540 * Fieldset Tag
541 *
542 * Used to produce <fieldset><legend>text</legend>. To close fieldset
543 * use form_fieldset_close()
544 *
545 * @param string The legend text
546 * @param string Additional attributes
547 * @return string
548 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 function form_fieldset($legend_text = '', $attributes = array())
550 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200551 $fieldset = '<fieldset'._attributes_to_string($attributes, FALSE).">\n";
Alex Bilbie773ccc32012-06-02 11:11:08 +0100552 if ($legend_text !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300554 return $fieldset.'<legend>'.$legend_text."</legend>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 }
556
557 return $fieldset;
558 }
559}
560
561// ------------------------------------------------------------------------
562
Derek Allard2067d1a2008-11-13 22:59:24 +0000563if ( ! function_exists('form_fieldset_close'))
564{
Timothy Warren01b129a2012-04-27 11:36:50 -0400565 /**
566 * Fieldset Close Tag
567 *
568 * @param string
569 * @return string
570 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 function form_fieldset_close($extra = '')
572 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300573 return '</fieldset>'.$extra;
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 }
575}
576
577// ------------------------------------------------------------------------
578
Derek Allard2067d1a2008-11-13 22:59:24 +0000579if ( ! function_exists('form_close'))
580{
Timothy Warren01b129a2012-04-27 11:36:50 -0400581 /**
582 * Form Close Tag
583 *
584 * @param string
585 * @return string
586 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 function form_close($extra = '')
588 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300589 return '</form>'.$extra;
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 }
591}
592
593// ------------------------------------------------------------------------
594
Derek Allard2067d1a2008-11-13 22:59:24 +0000595if ( ! function_exists('form_prep'))
596{
Timothy Warren01b129a2012-04-27 11:36:50 -0400597 /**
598 * Form Prep
599 *
600 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
601 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200602 * @param string|string[] $str Value to escape
603 * @param bool $is_textarea Whether we're escaping for a textarea element
604 * @return string|string[] Escaped values
Timothy Warren01b129a2012-04-27 11:36:50 -0400605 */
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200606 function form_prep($str = '', $is_textarea = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200608 if (is_array($str))
609 {
610 foreach (array_keys($str) as $key)
611 {
612 $str[$key] = form_prep($str[$key], $is_textarea);
613 }
614
615 return $str;
616 }
617
618 if ($is_textarea === TRUE)
619 {
620 return str_replace(array('<', '>'), array('&lt;', '&gt;'), stripslashes($str));
621 }
622
Andrey Andreev075f6fa2012-11-01 15:18:44 +0200623 return str_replace(array("'", '"'), array('&#39;', '&quot;'), stripslashes($str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 }
625}
626
627// ------------------------------------------------------------------------
628
Derek Allard2067d1a2008-11-13 22:59:24 +0000629if ( ! function_exists('set_value'))
630{
Timothy Warren01b129a2012-04-27 11:36:50 -0400631 /**
632 * Form Value
633 *
634 * Grabs a value from the POST array for the specified field so you can
635 * re-populate an input field or textarea. If Form Validation
636 * is active it retrieves the info from the validation class
637 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200638 * @param string $field Field name
639 * @param string $default Default value
640 * @param bool $is_textarea Whether the field is a textarea element
641 * @return string
Timothy Warren01b129a2012-04-27 11:36:50 -0400642 */
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200643 function set_value($field = '', $default = '', $is_textarea = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 {
nisheeth-barthwal5e227992013-02-21 13:09:29 +0530645 if (FALSE !== ($OBJ =& _get_validation_object()) && $OBJ->has_rule($field))
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 {
nisheeth-barthwal5e227992013-02-21 13:09:29 +0530647 return form_prep($OBJ->set_value($field, $default), $is_textarea);
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 }
649
nisheeth-barthwal5e227992013-02-21 13:09:29 +0530650 // We couldn't find the $field in validator, so try in $_POST array
651 $index = $field;
652 $container = $_POST;
653
654 // Test if the $field is an array name, and try to obtain the final index
655 if (preg_match_all('/\[(.*?)\]/', $field, $matches))
656 {
657 sscanf($field, '%[^[][', $index);
658 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
659 {
660 if (isset($container[$index]) && $matches[1][$i] !== '')
661 {
662 $container = $container[$index];
663 $index = $matches[1][$i];
664 }
665 else
666 {
667 $container = array();
668 break;
669 }
670 }
671 }
672
673 return isset($container[$index])
674 ? form_prep($container[$index], $is_textarea)
675 : form_prep($default, $is_textarea);
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 }
677}
678
679// ------------------------------------------------------------------------
680
Derek Allard2067d1a2008-11-13 22:59:24 +0000681if ( ! function_exists('set_select'))
682{
Timothy Warren01b129a2012-04-27 11:36:50 -0400683 /**
684 * Set Select
685 *
686 * Let's you set the selected value of a <select> menu via data in the POST array.
687 * If Form Validation is active it retrieves the info from the validation class
688 *
689 * @param string
690 * @param string
691 * @param bool
692 * @return string
693 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 function set_select($field = '', $value = '', $default = FALSE)
695 {
696 $OBJ =& _get_validation_object();
697
698 if ($OBJ === FALSE)
699 {
700 if ( ! isset($_POST[$field]))
701 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100702 if (count($_POST) === 0 && $default === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 {
704 return ' selected="selected"';
705 }
706 return '';
707 }
708
709 $field = $_POST[$field];
710
711 if (is_array($field))
712 {
713 if ( ! in_array($value, $field))
714 {
715 return '';
716 }
717 }
Michiel Vugteveena6f34232012-06-07 10:14:29 +0200718 elseif (($field == '' OR $value == '') OR $field !== $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000719 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300720 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 }
722
723 return ' selected="selected"';
724 }
725
726 return $OBJ->set_select($field, $value, $default);
727 }
728}
729
730// ------------------------------------------------------------------------
731
Derek Allard2067d1a2008-11-13 22:59:24 +0000732if ( ! function_exists('set_checkbox'))
733{
Timothy Warren01b129a2012-04-27 11:36:50 -0400734 /**
735 * Set Checkbox
736 *
737 * Let's you set the selected value of a checkbox via the value in the POST array.
738 * If Form Validation is active it retrieves the info from the validation class
739 *
740 * @param string
741 * @param string
742 * @param bool
743 * @return string
744 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000745 function set_checkbox($field = '', $value = '', $default = FALSE)
746 {
747 $OBJ =& _get_validation_object();
748
749 if ($OBJ === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200750 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000751 if ( ! isset($_POST[$field]))
752 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100753 if (count($_POST) === 0 && $default === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 {
755 return ' checked="checked"';
756 }
757 return '';
758 }
759
760 $field = $_POST[$field];
Barry Mienydd671972010-10-04 16:33:58 +0200761
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 if (is_array($field))
763 {
764 if ( ! in_array($value, $field))
765 {
766 return '';
767 }
768 }
Michiel Vugteveena6f34232012-06-07 10:14:29 +0200769 elseif (($field == '' OR $value == '') OR $field !== $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000770 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300771 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000772 }
773
774 return ' checked="checked"';
775 }
776
777 return $OBJ->set_checkbox($field, $value, $default);
778 }
779}
780
781// ------------------------------------------------------------------------
782
Derek Allard2067d1a2008-11-13 22:59:24 +0000783if ( ! function_exists('set_radio'))
784{
Timothy Warren01b129a2012-04-27 11:36:50 -0400785 /**
786 * Set Radio
787 *
788 * Let's you set the selected value of a radio field via info in the POST array.
789 * If Form Validation is active it retrieves the info from the validation class
790 *
791 * @param string
792 * @param string
793 * @param bool
794 * @return string
795 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000796 function set_radio($field = '', $value = '', $default = FALSE)
797 {
798 $OBJ =& _get_validation_object();
799
800 if ($OBJ === FALSE)
801 {
802 if ( ! isset($_POST[$field]))
803 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100804 if (count($_POST) === 0 && $default === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 {
806 return ' checked="checked"';
807 }
808 return '';
809 }
810
811 $field = $_POST[$field];
Barry Mienydd671972010-10-04 16:33:58 +0200812
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 if (is_array($field))
814 {
815 if ( ! in_array($value, $field))
816 {
817 return '';
818 }
819 }
820 else
821 {
Michiel Vugteveena6f34232012-06-07 10:14:29 +0200822 if (($field == '' OR $value == '') OR $field !== $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 {
824 return '';
825 }
826 }
827
828 return ' checked="checked"';
829 }
830
831 return $OBJ->set_radio($field, $value, $default);
832 }
833}
834
835// ------------------------------------------------------------------------
836
Derek Allard2067d1a2008-11-13 22:59:24 +0000837if ( ! function_exists('form_error'))
838{
Timothy Warren01b129a2012-04-27 11:36:50 -0400839 /**
840 * Form Error
841 *
842 * Returns the error for a specific form field. This is a helper for the
843 * form validation class.
844 *
845 * @param string
846 * @param string
847 * @param string
848 * @return string
849 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 function form_error($field = '', $prefix = '', $suffix = '')
851 {
852 if (FALSE === ($OBJ =& _get_validation_object()))
853 {
854 return '';
855 }
856
857 return $OBJ->error($field, $prefix, $suffix);
858 }
859}
860
861// ------------------------------------------------------------------------
862
Derek Allard2067d1a2008-11-13 22:59:24 +0000863if ( ! function_exists('validation_errors'))
864{
Timothy Warren01b129a2012-04-27 11:36:50 -0400865 /**
866 * Validation Error String
867 *
868 * Returns all the errors associated with a form submission. This is a helper
869 * function for the form validation class.
870 *
871 * @param string
872 * @param string
873 * @return string
874 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 function validation_errors($prefix = '', $suffix = '')
876 {
877 if (FALSE === ($OBJ =& _get_validation_object()))
Greg Akerc83bea62011-04-23 12:12:57 -0500878 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 return '';
880 }
881
882 return $OBJ->error_string($prefix, $suffix);
883 }
884}
885
886// ------------------------------------------------------------------------
887
Derek Allard2067d1a2008-11-13 22:59:24 +0000888if ( ! function_exists('_parse_form_attributes'))
889{
Timothy Warren01b129a2012-04-27 11:36:50 -0400890 /**
891 * Parse the form attributes
892 *
893 * Helper function used by some of the form helpers
894 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200895 * @param array $attributes List of attributes
896 * @param array $default Default values
Timothy Warren01b129a2012-04-27 11:36:50 -0400897 * @return string
898 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 function _parse_form_attributes($attributes, $default)
900 {
901 if (is_array($attributes))
902 {
903 foreach ($default as $key => $val)
904 {
905 if (isset($attributes[$key]))
906 {
907 $default[$key] = $attributes[$key];
908 unset($attributes[$key]);
909 }
910 }
911
912 if (count($attributes) > 0)
913 {
914 $default = array_merge($default, $attributes);
915 }
916 }
917
918 $att = '';
Barry Mienydd671972010-10-04 16:33:58 +0200919
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 foreach ($default as $key => $val)
921 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100922 if ($key === 'value')
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200924 $val = form_prep($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 }
Andrey Andreev60b97142012-10-25 16:59:17 +0300926 elseif ($key === 'name' && ! strlen($default['name']))
927 {
928 continue;
929 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000930
Andrey Andreev93a83c72012-03-26 21:24:02 +0300931 $att .= $key.'="'.$val.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 }
933
934 return $att;
935 }
936}
937
938// ------------------------------------------------------------------------
939
Derek Allard2067d1a2008-11-13 22:59:24 +0000940if ( ! function_exists('_attributes_to_string'))
941{
Timothy Warren01b129a2012-04-27 11:36:50 -0400942 /**
943 * Attributes To String
944 *
945 * Helper function used by some of the form helpers
946 *
947 * @param mixed
948 * @param bool
949 * @return string
950 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 function _attributes_to_string($attributes, $formtag = FALSE)
952 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300953 if (is_string($attributes) && strlen($attributes) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100955 if ($formtag === TRUE && strpos($attributes, 'method=') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 {
957 $attributes .= ' method="post"';
958 }
959
Alex Bilbie773ccc32012-06-02 11:11:08 +0100960 if ($formtag === TRUE && strpos($attributes, 'accept-charset=') === FALSE)
Derek Allard3241d732009-09-17 12:17:45 +0000961 {
962 $attributes .= ' accept-charset="'.strtolower(config_item('charset')).'"';
963 }
964
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200965 return ' '.$attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +0000966 }
Barry Mienydd671972010-10-04 16:33:58 +0200967
Andrey Andreev93a83c72012-03-26 21:24:02 +0300968 if (is_object($attributes) && count($attributes) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300970 $attributes = (array) $attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 }
972
Andrey Andreev93a83c72012-03-26 21:24:02 +0300973 if (is_array($attributes) && ($formtag === TRUE OR count($attributes) > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 {
Derek Allard3241d732009-09-17 12:17:45 +0000975 $atts = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000976
Andrey Andreev93a83c72012-03-26 21:24:02 +0300977 if ( ! isset($attributes['method']) && $formtag === TRUE)
Derek Allard3241d732009-09-17 12:17:45 +0000978 {
979 $atts .= ' method="post"';
980 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000981
Andrey Andreev93a83c72012-03-26 21:24:02 +0300982 if ( ! isset($attributes['accept-charset']) && $formtag === TRUE)
Derek Allard3241d732009-09-17 12:17:45 +0000983 {
984 $atts .= ' accept-charset="'.strtolower(config_item('charset')).'"';
985 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000986
Derek Allard3241d732009-09-17 12:17:45 +0000987 foreach ($attributes as $key => $val)
988 {
989 $atts .= ' '.$key.'="'.$val.'"';
990 }
991
992 return $atts;
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 }
994 }
995}
996
997// ------------------------------------------------------------------------
998
Derek Allard2067d1a2008-11-13 22:59:24 +0000999if ( ! function_exists('_get_validation_object'))
1000{
Timothy Warren01b129a2012-04-27 11:36:50 -04001001 /**
1002 * Validation Object
1003 *
1004 * Determines what the form validation class was instantiated as, fetches
1005 * the object and returns it.
1006 *
1007 * @return mixed
1008 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 function &_get_validation_object()
1010 {
1011 $CI =& get_instance();
1012
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001013 // We set this as a variable since we're returning by reference.
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 $return = FALSE;
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001015
Greg Akerc6d918a2011-04-22 10:17:32 -05001016 if (FALSE !== ($object = $CI->load->is_loaded('form_validation')))
Derek Allard2067d1a2008-11-13 22:59:24 +00001017 {
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001018 if ( ! isset($CI->$object) OR ! is_object($CI->$object))
1019 {
1020 return $return;
1021 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001022
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001023 return $CI->$object;
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001025
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001026 return $return;
Derek Allard2067d1a2008-11-13 22:59:24 +00001027 }
1028}
1029
Derek Allard2067d1a2008-11-13 22:59:24 +00001030/* End of file form_helper.php */
Andrey Andreev93a83c72012-03-26 21:24:02 +03001031/* Location: ./system/helpers/form_helper.php */