blob: 84a3e80cf73db669f9d44b9d49d0662448eba792 [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 {
Bo-Yi Wu06ddcf02013-02-18 08:52:05 +0800231 $defaults = array('type' => 'file', 'name' => '');
Andrey Andreev99ba3a22013-02-15 22:42:22 +0200232 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 Andreev7c4d1062012-11-01 15:14:34 +0200264 return '<textarea '._parse_form_attributes($data, $defaults).$extra.'>'.form_prep($val, TRUE)."</textarea>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 }
266}
267
268// ------------------------------------------------------------------------
269
Derek Jones68788d52010-03-05 10:11:31 -0600270if ( ! function_exists('form_multiselect'))
Derek Jones26399292009-04-08 16:14:17 +0000271{
Timothy Warren01b129a2012-04-27 11:36:50 -0400272 /**
273 * Multi-select menu
274 *
275 * @param string
276 * @param array
277 * @param mixed
278 * @param string
279 * @return string
280 */
Derek Jones26399292009-04-08 16:14:17 +0000281 function form_multiselect($name = '', $options = array(), $selected = array(), $extra = '')
282 {
283 if ( ! strpos($extra, 'multiple'))
284 {
285 $extra .= ' multiple="multiple"';
286 }
Barry Mienydd671972010-10-04 16:33:58 +0200287
Derek Jones26399292009-04-08 16:14:17 +0000288 return form_dropdown($name, $options, $selected, $extra);
289 }
290}
291
292// --------------------------------------------------------------------
293
Derek Allard2067d1a2008-11-13 22:59:24 +0000294if ( ! function_exists('form_dropdown'))
295{
Timothy Warren01b129a2012-04-27 11:36:50 -0400296 /**
297 * Drop-down Menu
298 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200299 * @param mixed $name
300 * @param mixed $options
301 * @param mixed $selected
302 * @param mixed $extra
Timothy Warren01b129a2012-04-27 11:36:50 -0400303 * @return string
304 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
306 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300307 // If name is really an array then we'll call the function again using the array
308 if (is_array($name) && isset($name['name']))
309 {
310 isset($name['options']) OR $name['options'] = array();
311 isset($name['selected']) OR $name['selected'] = array();
312 isset($name['extra']) OR $name['extra'] = array();
313
314 return form_dropdown($name['name'], $name['options'], $name['selected'], $name['extra']);
315 }
316
Andrey Andreev582ebcb2012-10-27 00:52:15 +0300317 is_array($selected) OR $selected = array($selected);
Derek Allard2067d1a2008-11-13 22:59:24 +0000318
319 // If no selected state was submitted we will attempt to set it automatically
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200320 if (count($selected) === 0 && isset($_POST[$name]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200322 $selected = array($_POST[$name]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 }
324
Michiel Vugteveen9640a032012-06-06 20:20:27 +0200325 if ($extra != '')
326 {
327 $extra = ' '.$extra;
328 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000329
330 $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
331
332 $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
Robin Sowell57fe4102009-03-26 18:58:46 +0000333
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 foreach ($options as $key => $val)
335 {
336 $key = (string) $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000337
Andrey Andreev6b114ae2012-07-13 12:05:52 +0300338 if (is_array($val))
Derek Allard78a5fc92009-02-05 16:34:35 +0000339 {
Andrey Andreev6b114ae2012-07-13 12:05:52 +0300340 if (empty($val))
341 {
342 continue;
343 }
344
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200345 $form .= '<optgroup label="'.$key."\">\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000346
Derek Allard78a5fc92009-02-05 16:34:35 +0000347 foreach ($val as $optgroup_key => $optgroup_val)
348 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300349 $sel = in_array($optgroup_key, $selected) ? ' selected="selected"' : '';
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200350 $form .= '<option value="'.form_prep($optgroup_key).'"'.$sel.'>'
Andrey Andreev582ebcb2012-10-27 00:52:15 +0300351 .(string) $optgroup_val."</option>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000352 }
353
Andrey Andreev93a83c72012-03-26 21:24:02 +0300354 $form .= "</optgroup>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000355 }
356 else
357 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200358 $form .= '<option value="'.form_prep($key).'"'
Andrey Andreev582ebcb2012-10-27 00:52:15 +0300359 .(in_array($key, $selected) ? ' selected="selected"' : '').'>'
360 .(string) $val."</option>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000361 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 }
363
Andrey Andreev93a83c72012-03-26 21:24:02 +0300364 return $form."</select>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 }
366}
367
368// ------------------------------------------------------------------------
369
Derek Allard2067d1a2008-11-13 22:59:24 +0000370if ( ! function_exists('form_checkbox'))
371{
Timothy Warren01b129a2012-04-27 11:36:50 -0400372 /**
373 * Checkbox Field
374 *
375 * @param mixed
376 * @param string
377 * @param bool
378 * @param string
379 * @return string
380 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
382 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300383 $defaults = array('type' => 'checkbox', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000384
Andrey Andreev93a83c72012-03-26 21:24:02 +0300385 if (is_array($data) && array_key_exists('checked', $data))
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 {
387 $checked = $data['checked'];
388
Michiel Vugteveen910ff7a2012-06-06 20:03:14 +0200389 if ($checked == FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 {
391 unset($data['checked']);
392 }
393 else
394 {
395 $data['checked'] = 'checked';
396 }
397 }
398
Michiel Vugteveen910ff7a2012-06-06 20:03:14 +0200399 if ($checked == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 {
401 $defaults['checked'] = 'checked';
402 }
403 else
404 {
405 unset($defaults['checked']);
406 }
407
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200408 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 }
410}
411
412// ------------------------------------------------------------------------
413
Derek Allard2067d1a2008-11-13 22:59:24 +0000414if ( ! function_exists('form_radio'))
415{
Timothy Warren01b129a2012-04-27 11:36:50 -0400416 /**
417 * Radio Button
418 *
419 * @param mixed
420 * @param string
421 * @param bool
422 * @param string
423 * @return string
424 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
426 {
427 if ( ! is_array($data))
Barry Mienydd671972010-10-04 16:33:58 +0200428 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 $data = array('name' => $data);
430 }
431
432 $data['type'] = 'radio';
433 return form_checkbox($data, $value, $checked, $extra);
434 }
435}
436
437// ------------------------------------------------------------------------
438
Derek Allard2067d1a2008-11-13 22:59:24 +0000439if ( ! function_exists('form_submit'))
Barry Mienydd671972010-10-04 16:33:58 +0200440{
Timothy Warren01b129a2012-04-27 11:36:50 -0400441 /**
442 * Submit Button
443 *
444 * @param mixed
445 * @param string
446 * @param string
447 * @return string
448 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 function form_submit($data = '', $value = '', $extra = '')
450 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300451 $defaults = array('type' => 'submit', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200452 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 }
454}
455
456// ------------------------------------------------------------------------
457
Derek Allard2067d1a2008-11-13 22:59:24 +0000458if ( ! function_exists('form_reset'))
459{
Timothy Warren01b129a2012-04-27 11:36:50 -0400460 /**
461 * Reset Button
462 *
463 * @param mixed
464 * @param string
465 * @param string
466 * @return string
467 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 function form_reset($data = '', $value = '', $extra = '')
469 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300470 $defaults = array('type' => 'reset', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
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_button'))
478{
Timothy Warren01b129a2012-04-27 11:36:50 -0400479 /**
480 * Form 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_button($data = '', $content = '', $extra = '')
488 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300489 $defaults = array('name' => ( ! is_array($data) ? $data : ''), 'type' => 'button');
490 if (is_array($data) && isset($data['content']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 {
492 $content = $data['content'];
493 unset($data['content']); // content is not an attribute
494 }
495
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200496 return '<button '._parse_form_attributes($data, $defaults).$extra.'>'.$content."</button>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 }
498}
499
500// ------------------------------------------------------------------------
501
Derek Allard2067d1a2008-11-13 22:59:24 +0000502if ( ! function_exists('form_label'))
503{
Timothy Warren01b129a2012-04-27 11:36:50 -0400504 /**
505 * Form Label Tag
506 *
507 * @param string The text to appear onscreen
508 * @param string The id the label applies to
509 * @param string Additional attributes
510 * @return string
511 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 function form_label($label_text = '', $id = '', $attributes = array())
513 {
514
515 $label = '<label';
516
Alex Bilbie773ccc32012-06-02 11:11:08 +0100517 if ($id !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300519 $label .= ' for="'.$id.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 }
521
Andrey Andreev93a83c72012-03-26 21:24:02 +0300522 if (is_array($attributes) && count($attributes) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 {
524 foreach ($attributes as $key => $val)
525 {
526 $label .= ' '.$key.'="'.$val.'"';
527 }
528 }
529
Andrey Andreev93a83c72012-03-26 21:24:02 +0300530 return $label.'>'.$label_text.'</label>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 }
532}
533
534// ------------------------------------------------------------------------
Timothy Warren01b129a2012-04-27 11:36:50 -0400535
Derek Allard2067d1a2008-11-13 22:59:24 +0000536if ( ! function_exists('form_fieldset'))
537{
Timothy Warren01b129a2012-04-27 11:36:50 -0400538 /**
539 * Fieldset Tag
540 *
541 * Used to produce <fieldset><legend>text</legend>. To close fieldset
542 * use form_fieldset_close()
543 *
544 * @param string The legend text
545 * @param string Additional attributes
546 * @return string
547 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 function form_fieldset($legend_text = '', $attributes = array())
549 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200550 $fieldset = '<fieldset'._attributes_to_string($attributes, FALSE).">\n";
Alex Bilbie773ccc32012-06-02 11:11:08 +0100551 if ($legend_text !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300553 return $fieldset.'<legend>'.$legend_text."</legend>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 }
555
556 return $fieldset;
557 }
558}
559
560// ------------------------------------------------------------------------
561
Derek Allard2067d1a2008-11-13 22:59:24 +0000562if ( ! function_exists('form_fieldset_close'))
563{
Timothy Warren01b129a2012-04-27 11:36:50 -0400564 /**
565 * Fieldset Close Tag
566 *
567 * @param string
568 * @return string
569 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 function form_fieldset_close($extra = '')
571 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300572 return '</fieldset>'.$extra;
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 }
574}
575
576// ------------------------------------------------------------------------
577
Derek Allard2067d1a2008-11-13 22:59:24 +0000578if ( ! function_exists('form_close'))
579{
Timothy Warren01b129a2012-04-27 11:36:50 -0400580 /**
581 * Form Close Tag
582 *
583 * @param string
584 * @return string
585 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 function form_close($extra = '')
587 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300588 return '</form>'.$extra;
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 }
590}
591
592// ------------------------------------------------------------------------
593
Derek Allard2067d1a2008-11-13 22:59:24 +0000594if ( ! function_exists('form_prep'))
595{
Timothy Warren01b129a2012-04-27 11:36:50 -0400596 /**
597 * Form Prep
598 *
599 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
600 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200601 * @param string|string[] $str Value to escape
602 * @param bool $is_textarea Whether we're escaping for a textarea element
603 * @return string|string[] Escaped values
Timothy Warren01b129a2012-04-27 11:36:50 -0400604 */
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200605 function form_prep($str = '', $is_textarea = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200607 if (is_array($str))
608 {
609 foreach (array_keys($str) as $key)
610 {
611 $str[$key] = form_prep($str[$key], $is_textarea);
612 }
613
614 return $str;
615 }
616
617 if ($is_textarea === TRUE)
618 {
619 return str_replace(array('<', '>'), array('&lt;', '&gt;'), stripslashes($str));
620 }
621
Andrey Andreev075f6fa2012-11-01 15:18:44 +0200622 return str_replace(array("'", '"'), array('&#39;', '&quot;'), stripslashes($str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 }
624}
625
626// ------------------------------------------------------------------------
627
Derek Allard2067d1a2008-11-13 22:59:24 +0000628if ( ! function_exists('set_value'))
629{
Timothy Warren01b129a2012-04-27 11:36:50 -0400630 /**
631 * Form Value
632 *
633 * Grabs a value from the POST array for the specified field so you can
634 * re-populate an input field or textarea. If Form Validation
635 * is active it retrieves the info from the validation class
636 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200637 * @param string $field Field name
638 * @param string $default Default value
639 * @param bool $is_textarea Whether the field is a textarea element
640 * @return string
Timothy Warren01b129a2012-04-27 11:36:50 -0400641 */
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200642 function set_value($field = '', $default = '', $is_textarea = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 {
644 if (FALSE === ($OBJ =& _get_validation_object()))
645 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200646 return isset($_POST[$field])
647 ? form_prep($_POST[$field], $is_textarea)
648 : form_prep($default, $is_textarea);
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 }
650
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200651 return form_prep($OBJ->set_value($field, $default), $is_textarea);
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 }
653}
654
655// ------------------------------------------------------------------------
656
Derek Allard2067d1a2008-11-13 22:59:24 +0000657if ( ! function_exists('set_select'))
658{
Timothy Warren01b129a2012-04-27 11:36:50 -0400659 /**
660 * Set Select
661 *
662 * Let's you set the selected value of a <select> menu via data in the POST array.
663 * If Form Validation is active it retrieves the info from the validation class
664 *
665 * @param string
666 * @param string
667 * @param bool
668 * @return string
669 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 function set_select($field = '', $value = '', $default = FALSE)
671 {
672 $OBJ =& _get_validation_object();
673
674 if ($OBJ === FALSE)
675 {
676 if ( ! isset($_POST[$field]))
677 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100678 if (count($_POST) === 0 && $default === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 {
680 return ' selected="selected"';
681 }
682 return '';
683 }
684
685 $field = $_POST[$field];
686
687 if (is_array($field))
688 {
689 if ( ! in_array($value, $field))
690 {
691 return '';
692 }
693 }
Michiel Vugteveena6f34232012-06-07 10:14:29 +0200694 elseif (($field == '' OR $value == '') OR $field !== $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300696 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 }
698
699 return ' selected="selected"';
700 }
701
702 return $OBJ->set_select($field, $value, $default);
703 }
704}
705
706// ------------------------------------------------------------------------
707
Derek Allard2067d1a2008-11-13 22:59:24 +0000708if ( ! function_exists('set_checkbox'))
709{
Timothy Warren01b129a2012-04-27 11:36:50 -0400710 /**
711 * Set Checkbox
712 *
713 * Let's you set the selected value of a checkbox via the value in the POST array.
714 * If Form Validation is active it retrieves the info from the validation class
715 *
716 * @param string
717 * @param string
718 * @param bool
719 * @return string
720 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 function set_checkbox($field = '', $value = '', $default = FALSE)
722 {
723 $OBJ =& _get_validation_object();
724
725 if ($OBJ === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200726 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 if ( ! isset($_POST[$field]))
728 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100729 if (count($_POST) === 0 && $default === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 {
731 return ' checked="checked"';
732 }
733 return '';
734 }
735
736 $field = $_POST[$field];
Barry Mienydd671972010-10-04 16:33:58 +0200737
Derek Allard2067d1a2008-11-13 22:59:24 +0000738 if (is_array($field))
739 {
740 if ( ! in_array($value, $field))
741 {
742 return '';
743 }
744 }
Michiel Vugteveena6f34232012-06-07 10:14:29 +0200745 elseif (($field == '' OR $value == '') OR $field !== $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300747 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 }
749
750 return ' checked="checked"';
751 }
752
753 return $OBJ->set_checkbox($field, $value, $default);
754 }
755}
756
757// ------------------------------------------------------------------------
758
Derek Allard2067d1a2008-11-13 22:59:24 +0000759if ( ! function_exists('set_radio'))
760{
Timothy Warren01b129a2012-04-27 11:36:50 -0400761 /**
762 * Set Radio
763 *
764 * Let's you set the selected value of a radio field via info in the POST array.
765 * If Form Validation is active it retrieves the info from the validation class
766 *
767 * @param string
768 * @param string
769 * @param bool
770 * @return string
771 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000772 function set_radio($field = '', $value = '', $default = FALSE)
773 {
774 $OBJ =& _get_validation_object();
775
776 if ($OBJ === FALSE)
777 {
778 if ( ! isset($_POST[$field]))
779 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100780 if (count($_POST) === 0 && $default === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000781 {
782 return ' checked="checked"';
783 }
784 return '';
785 }
786
787 $field = $_POST[$field];
Barry Mienydd671972010-10-04 16:33:58 +0200788
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 if (is_array($field))
790 {
791 if ( ! in_array($value, $field))
792 {
793 return '';
794 }
795 }
796 else
797 {
Michiel Vugteveena6f34232012-06-07 10:14:29 +0200798 if (($field == '' OR $value == '') OR $field !== $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 {
800 return '';
801 }
802 }
803
804 return ' checked="checked"';
805 }
806
807 return $OBJ->set_radio($field, $value, $default);
808 }
809}
810
811// ------------------------------------------------------------------------
812
Derek Allard2067d1a2008-11-13 22:59:24 +0000813if ( ! function_exists('form_error'))
814{
Timothy Warren01b129a2012-04-27 11:36:50 -0400815 /**
816 * Form Error
817 *
818 * Returns the error for a specific form field. This is a helper for the
819 * form validation class.
820 *
821 * @param string
822 * @param string
823 * @param string
824 * @return string
825 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 function form_error($field = '', $prefix = '', $suffix = '')
827 {
828 if (FALSE === ($OBJ =& _get_validation_object()))
829 {
830 return '';
831 }
832
833 return $OBJ->error($field, $prefix, $suffix);
834 }
835}
836
837// ------------------------------------------------------------------------
838
Derek Allard2067d1a2008-11-13 22:59:24 +0000839if ( ! function_exists('validation_errors'))
840{
Timothy Warren01b129a2012-04-27 11:36:50 -0400841 /**
842 * Validation Error String
843 *
844 * Returns all the errors associated with a form submission. This is a helper
845 * function for the form validation class.
846 *
847 * @param string
848 * @param string
849 * @return string
850 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 function validation_errors($prefix = '', $suffix = '')
852 {
853 if (FALSE === ($OBJ =& _get_validation_object()))
Greg Akerc83bea62011-04-23 12:12:57 -0500854 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 return '';
856 }
857
858 return $OBJ->error_string($prefix, $suffix);
859 }
860}
861
862// ------------------------------------------------------------------------
863
Derek Allard2067d1a2008-11-13 22:59:24 +0000864if ( ! function_exists('_parse_form_attributes'))
865{
Timothy Warren01b129a2012-04-27 11:36:50 -0400866 /**
867 * Parse the form attributes
868 *
869 * Helper function used by some of the form helpers
870 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200871 * @param array $attributes List of attributes
872 * @param array $default Default values
Timothy Warren01b129a2012-04-27 11:36:50 -0400873 * @return string
874 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 function _parse_form_attributes($attributes, $default)
876 {
877 if (is_array($attributes))
878 {
879 foreach ($default as $key => $val)
880 {
881 if (isset($attributes[$key]))
882 {
883 $default[$key] = $attributes[$key];
884 unset($attributes[$key]);
885 }
886 }
887
888 if (count($attributes) > 0)
889 {
890 $default = array_merge($default, $attributes);
891 }
892 }
893
894 $att = '';
Barry Mienydd671972010-10-04 16:33:58 +0200895
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 foreach ($default as $key => $val)
897 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100898 if ($key === 'value')
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200900 $val = form_prep($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 }
Andrey Andreev60b97142012-10-25 16:59:17 +0300902 elseif ($key === 'name' && ! strlen($default['name']))
903 {
904 continue;
905 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000906
Andrey Andreev93a83c72012-03-26 21:24:02 +0300907 $att .= $key.'="'.$val.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 }
909
910 return $att;
911 }
912}
913
914// ------------------------------------------------------------------------
915
Derek Allard2067d1a2008-11-13 22:59:24 +0000916if ( ! function_exists('_attributes_to_string'))
917{
Timothy Warren01b129a2012-04-27 11:36:50 -0400918 /**
919 * Attributes To String
920 *
921 * Helper function used by some of the form helpers
922 *
923 * @param mixed
924 * @param bool
925 * @return string
926 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 function _attributes_to_string($attributes, $formtag = FALSE)
928 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300929 if (is_string($attributes) && strlen($attributes) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100931 if ($formtag === TRUE && strpos($attributes, 'method=') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 {
933 $attributes .= ' method="post"';
934 }
935
Alex Bilbie773ccc32012-06-02 11:11:08 +0100936 if ($formtag === TRUE && strpos($attributes, 'accept-charset=') === FALSE)
Derek Allard3241d732009-09-17 12:17:45 +0000937 {
938 $attributes .= ' accept-charset="'.strtolower(config_item('charset')).'"';
939 }
940
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200941 return ' '.$attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 }
Barry Mienydd671972010-10-04 16:33:58 +0200943
Andrey Andreev93a83c72012-03-26 21:24:02 +0300944 if (is_object($attributes) && count($attributes) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300946 $attributes = (array) $attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 }
948
Andrey Andreev93a83c72012-03-26 21:24:02 +0300949 if (is_array($attributes) && ($formtag === TRUE OR count($attributes) > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 {
Derek Allard3241d732009-09-17 12:17:45 +0000951 $atts = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000952
Andrey Andreev93a83c72012-03-26 21:24:02 +0300953 if ( ! isset($attributes['method']) && $formtag === TRUE)
Derek Allard3241d732009-09-17 12:17:45 +0000954 {
955 $atts .= ' method="post"';
956 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000957
Andrey Andreev93a83c72012-03-26 21:24:02 +0300958 if ( ! isset($attributes['accept-charset']) && $formtag === TRUE)
Derek Allard3241d732009-09-17 12:17:45 +0000959 {
960 $atts .= ' accept-charset="'.strtolower(config_item('charset')).'"';
961 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000962
Derek Allard3241d732009-09-17 12:17:45 +0000963 foreach ($attributes as $key => $val)
964 {
965 $atts .= ' '.$key.'="'.$val.'"';
966 }
967
968 return $atts;
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 }
970 }
971}
972
973// ------------------------------------------------------------------------
974
Derek Allard2067d1a2008-11-13 22:59:24 +0000975if ( ! function_exists('_get_validation_object'))
976{
Timothy Warren01b129a2012-04-27 11:36:50 -0400977 /**
978 * Validation Object
979 *
980 * Determines what the form validation class was instantiated as, fetches
981 * the object and returns it.
982 *
983 * @return mixed
984 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 function &_get_validation_object()
986 {
987 $CI =& get_instance();
988
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500989 // We set this as a variable since we're returning by reference.
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 $return = FALSE;
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200991
Greg Akerc6d918a2011-04-22 10:17:32 -0500992 if (FALSE !== ($object = $CI->load->is_loaded('form_validation')))
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 {
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500994 if ( ! isset($CI->$object) OR ! is_object($CI->$object))
995 {
996 return $return;
997 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200998
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500999 return $CI->$object;
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001001
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001002 return $return;
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 }
1004}
1005
Derek Allard2067d1a2008-11-13 22:59:24 +00001006/* End of file form_helper.php */
Zach Cardozae15d1be2013-03-22 14:50:39 -07001007/* Location: ./system/helpers/form_helper.php */