Subversion-Projekte lars-tiefland.ci

Revision

Revision 1257 | Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
68 lars 1
<?php
2
/**
3
 * CodeIgniter
4
 *
5
 * An open source application development framework for PHP
6
 *
7
 * This content is released under the MIT License (MIT)
8
 *
9
 * Copyright (c) 2014 - 2016, British Columbia Institute of Technology
10
 *
11
 * Permission is hereby granted, free of charge, to any person obtaining a copy
12
 * of this software and associated documentation files (the "Software"), to deal
13
 * in the Software without restriction, including without limitation the rights
14
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
 * copies of the Software, and to permit persons to whom the Software is
16
 * furnished to do so, subject to the following conditions:
17
 *
18
 * The above copyright notice and this permission notice shall be included in
19
 * all copies or substantial portions of the Software.
20
 *
21
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
 * THE SOFTWARE.
28
 *
29
 * @package	CodeIgniter
30
 * @author	EllisLab Dev Team
31
 * @copyright	Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
32
 * @copyright	Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/)
33
 * @license	http://opensource.org/licenses/MIT	MIT License
34
 * @link	https://codeigniter.com
35
 * @since	Version 1.0.0
36
 * @filesource
37
 */
38
defined('BASEPATH') OR exit('No direct script access allowed');
39
 
40
/**
41
 * CodeIgniter Form Helpers
42
 *
43
 * @package		CodeIgniter
44
 * @subpackage	Helpers
45
 * @category	Helpers
46
 * @author		EllisLab Dev Team
47
 * @link		https://codeigniter.com/user_guide/helpers/form_helper.html
48
 */
49
 
50
// ------------------------------------------------------------------------
51
 
52
if ( ! function_exists('form_open'))
53
{
54
	/**
55
	 * Form Declaration
56
	 *
57
	 * Creates the opening portion of the form.
58
	 *
59
	 * @param	string	the URI segments of the form destination
60
	 * @param	array	a key/value pair of attributes
61
	 * @param	array	a key/value pair hidden data
62
	 * @return	string
63
	 */
64
	function form_open($action = '', $attributes = array(), $hidden = array())
65
	{
66
		$CI =& get_instance();
67
 
68
		// If no action is provided then set to the current url
69
		if ( ! $action)
70
		{
71
			$action = $CI->config->site_url($CI->uri->uri_string());
72
		}
73
		// If an action is not a full URL then turn it into one
74
		elseif (strpos($action, '://') === FALSE)
75
		{
76
			$action = $CI->config->site_url($action);
77
		}
78
 
79
		$attributes = _attributes_to_string($attributes);
80
 
81
		if (stripos($attributes, 'method=') === FALSE)
82
		{
83
			$attributes .= ' method="post"';
84
		}
85
 
86
		if (stripos($attributes, 'accept-charset=') === FALSE)
87
		{
88
			$attributes .= ' accept-charset="'.strtolower(config_item('charset')).'"';
89
		}
90
 
91
		$form = '<form action="'.$action.'"'.$attributes.">\n";
92
 
93
		// Add CSRF field if enabled, but leave it out for GET requests and requests to external websites
94
		if ($CI->config->item('csrf_protection') === TRUE && strpos($action, $CI->config->base_url()) !== FALSE && ! stripos($form, 'method="get"'))
95
		{
96
			$hidden[$CI->security->get_csrf_token_name()] = $CI->security->get_csrf_hash();
97
		}
98
 
99
		if (is_array($hidden))
100
		{
101
			foreach ($hidden as $name => $value)
102
			{
103
				$form .= '<input type="hidden" name="'.$name.'" value="'.html_escape($value).'" style="display:none;" />'."\n";
104
			}
105
		}
106
 
107
		return $form;
108
	}
109
}
110
 
111
// ------------------------------------------------------------------------
112
 
113
if ( ! function_exists('form_open_multipart'))
114
{
115
	/**
116
	 * Form Declaration - Multipart type
117
	 *
118
	 * Creates the opening portion of the form, but with "multipart/form-data".
119
	 *
120
	 * @param	string	the URI segments of the form destination
121
	 * @param	array	a key/value pair of attributes
122
	 * @param	array	a key/value pair hidden data
123
	 * @return	string
124
	 */
125
	function form_open_multipart($action = '', $attributes = array(), $hidden = array())
126
	{
127
		if (is_string($attributes))
128
		{
129
			$attributes .= ' enctype="multipart/form-data"';
130
		}
131
		else
132
		{
133
			$attributes['enctype'] = 'multipart/form-data';
134
		}
135
 
136
		return form_open($action, $attributes, $hidden);
137
	}
138
}
139
 
140
// ------------------------------------------------------------------------
141
 
142
if ( ! function_exists('form_hidden'))
143
{
144
	/**
145
	 * Hidden Input Field
146
	 *
147
	 * Generates hidden fields. You can pass a simple key/value string or
148
	 * an associative array with multiple values.
149
	 *
150
	 * @param	mixed	$name		Field name
151
	 * @param	string	$value		Field value
152
	 * @param	bool	$recursing
153
	 * @return	string
154
	 */
155
	function form_hidden($name, $value = '', $recursing = FALSE)
156
	{
157
		static $form;
158
 
159
		if ($recursing === FALSE)
160
		{
161
			$form = "\n";
162
		}
163
 
164
		if (is_array($name))
165
		{
166
			foreach ($name as $key => $val)
167
			{
168
				form_hidden($key, $val, TRUE);
169
			}
170
 
171
			return $form;
172
		}
173
 
174
		if ( ! is_array($value))
175
		{
176
			$form .= '<input type="hidden" name="'.$name.'" value="'.html_escape($value)."\" />\n";
177
		}
178
		else
179
		{
180
			foreach ($value as $k => $v)
181
			{
182
				$k = is_int($k) ? '' : $k;
183
				form_hidden($name.'['.$k.']', $v, TRUE);
184
			}
185
		}
186
 
187
		return $form;
188
	}
189
}
190
 
191
// ------------------------------------------------------------------------
192
 
193
if ( ! function_exists('form_input'))
194
{
195
	/**
196
	 * Text Input Field
197
	 *
198
	 * @param	mixed
199
	 * @param	string
200
	 * @param	mixed
201
	 * @return	string
202
	 */
203
	function form_input($data = '', $value = '', $extra = '')
204
	{
205
		$defaults = array(
206
			'type' => 'text',
207
			'name' => is_array($data) ? '' : $data,
208
			'value' => $value
209
		);
210
 
211
		return '<input '._parse_form_attributes($data, $defaults)._attributes_to_string($extra)." />\n";
212
	}
213
}
214
 
215
// ------------------------------------------------------------------------
216
 
217
if ( ! function_exists('form_password'))
218
{
219
	/**
220
	 * Password Field
221
	 *
222
	 * Identical to the input function but adds the "password" type
223
	 *
224
	 * @param	mixed
225
	 * @param	string
226
	 * @param	mixed
227
	 * @return	string
228
	 */
229
	function form_password($data = '', $value = '', $extra = '')
230
	{
231
		is_array($data) OR $data = array('name' => $data);
232
		$data['type'] = 'password';
233
		return form_input($data, $value, $extra);
234
	}
235
}
236
 
237
// ------------------------------------------------------------------------
238
 
239
if ( ! function_exists('form_upload'))
240
{
241
	/**
242
	 * Upload Field
243
	 *
244
	 * Identical to the input function but adds the "file" type
245
	 *
246
	 * @param	mixed
247
	 * @param	string
248
	 * @param	mixed
249
	 * @return	string
250
	 */
251
	function form_upload($data = '', $value = '', $extra = '')
252
	{
253
		$defaults = array('type' => 'file', 'name' => '');
254
		is_array($data) OR $data = array('name' => $data);
255
		$data['type'] = 'file';
256
 
257
		return '<input '._parse_form_attributes($data, $defaults)._attributes_to_string($extra)." />\n";
258
	}
259
}
260
 
261
// ------------------------------------------------------------------------
262
 
263
if ( ! function_exists('form_textarea'))
264
{
265
	/**
266
	 * Textarea field
267
	 *
268
	 * @param	mixed	$data
269
	 * @param	string	$value
270
	 * @param	mixed	$extra
271
	 * @return	string
272
	 */
273
	function form_textarea($data = '', $value = '', $extra = '')
274
	{
275
		$defaults = array(
276
			'name' => is_array($data) ? '' : $data,
277
			'cols' => '40',
278
			'rows' => '10'
279
		);
280
 
281
		if ( ! is_array($data) OR ! isset($data['value']))
282
		{
283
			$val = $value;
284
		}
285
		else
286
		{
287
			$val = $data['value'];
288
			unset($data['value']); // textareas don't use the value attribute
289
		}
290
 
291
		return '<textarea '._parse_form_attributes($data, $defaults)._attributes_to_string($extra).'>'
292
			.html_escape($val)
293
			."</textarea>\n";
294
	}
295
}
296
 
297
// ------------------------------------------------------------------------
298
 
299
if ( ! function_exists('form_multiselect'))
300
{
301
	/**
302
	 * Multi-select menu
303
	 *
304
	 * @param	string
305
	 * @param	array
306
	 * @param	mixed
307
	 * @param	mixed
308
	 * @return	string
309
	 */
310
	function form_multiselect($name = '', $options = array(), $selected = array(), $extra = '')
311
	{
312
		$extra = _attributes_to_string($extra);
313
		if (stripos($extra, 'multiple') === FALSE)
314
		{
315
			$extra .= ' multiple="multiple"';
316
		}
317
 
318
		return form_dropdown($name, $options, $selected, $extra);
319
	}
320
}
321
 
322
// --------------------------------------------------------------------
323
 
324
if ( ! function_exists('form_dropdown'))
325
{
326
	/**
327
	 * Drop-down Menu
328
	 *
329
	 * @param	mixed	$data
330
	 * @param	mixed	$options
331
	 * @param	mixed	$selected
332
	 * @param	mixed	$extra
333
	 * @return	string
334
	 */
335
	function form_dropdown($data = '', $options = array(), $selected = array(), $extra = '')
336
	{
337
		$defaults = array();
338
 
339
		if (is_array($data))
340
		{
341
			if (isset($data['selected']))
342
			{
343
				$selected = $data['selected'];
344
				unset($data['selected']); // select tags don't have a selected attribute
345
			}
346
 
347
			if (isset($data['options']))
348
			{
349
				$options = $data['options'];
350
				unset($data['options']); // select tags don't use an options attribute
351
			}
352
		}
353
		else
354
		{
355
			$defaults = array('name' => $data);
356
		}
357
 
358
		is_array($selected) OR $selected = array($selected);
359
		is_array($options) OR $options = array($options);
360
 
361
		// If no selected state was submitted we will attempt to set it automatically
362
		if (empty($selected))
363
		{
364
			if (is_array($data))
365
			{
366
				if (isset($data['name'], $_POST[$data['name']]))
367
				{
368
					$selected = array($_POST[$data['name']]);
369
				}
370
			}
371
			elseif (isset($_POST[$data]))
372
			{
373
				$selected = array($_POST[$data]);
374
			}
375
		}
376
 
377
		$extra = _attributes_to_string($extra);
378
 
379
		$multiple = (count($selected) > 1 && stripos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
380
 
381
		$form = '<select '.rtrim(_parse_form_attributes($data, $defaults)).$extra.$multiple.">\n";
382
 
383
		foreach ($options as $key => $val)
384
		{
385
			$key = (string) $key;
386
 
387
			if (is_array($val))
388
			{
389
				if (empty($val))
390
				{
391
					continue;
392
				}
393
 
394
				$form .= '<optgroup label="'.$key."\">\n";
395
 
396
				foreach ($val as $optgroup_key => $optgroup_val)
397
				{
398
					$sel = in_array($optgroup_key, $selected) ? ' selected="selected"' : '';
399
					$form .= '<option value="'.html_escape($optgroup_key).'"'.$sel.'>'
400
						.(string) $optgroup_val."</option>\n";
401
				}
402
 
403
				$form .= "</optgroup>\n";
404
			}
405
			else
406
			{
407
				$form .= '<option value="'.html_escape($key).'"'
408
					.(in_array($key, $selected) ? ' selected="selected"' : '').'>'
409
					.(string) $val."</option>\n";
410
			}
411
		}
412
 
413
		return $form."</select>\n";
414
	}
415
}
416
 
417
// ------------------------------------------------------------------------
418
 
419
if ( ! function_exists('form_checkbox'))
420
{
421
	/**
422
	 * Checkbox Field
423
	 *
424
	 * @param	mixed
425
	 * @param	string
426
	 * @param	bool
427
	 * @param	mixed
428
	 * @return	string
429
	 */
430
	function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
431
	{
432
		$defaults = array('type' => 'checkbox', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
433
 
434
		if (is_array($data) && array_key_exists('checked', $data))
435
		{
436
			$checked = $data['checked'];
437
 
438
			if ($checked == FALSE)
439
			{
440
				unset($data['checked']);
441
			}
442
			else
443
			{
444
				$data['checked'] = 'checked';
445
			}
446
		}
447
 
448
		if ($checked == TRUE)
449
		{
450
			$defaults['checked'] = 'checked';
451
		}
452
		else
453
		{
454
			unset($defaults['checked']);
455
		}
456
 
457
		return '<input '._parse_form_attributes($data, $defaults)._attributes_to_string($extra)." />\n";
458
	}
459
}
460
 
461
// ------------------------------------------------------------------------
462
 
463
if ( ! function_exists('form_radio'))
464
{
465
	/**
466
	 * Radio Button
467
	 *
468
	 * @param	mixed
469
	 * @param	string
470
	 * @param	bool
471
	 * @param	mixed
472
	 * @return	string
473
	 */
474
	function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
475
	{
476
		is_array($data) OR $data = array('name' => $data);
477
		$data['type'] = 'radio';
478
 
479
		return form_checkbox($data, $value, $checked, $extra);
480
	}
481
}
482
 
483
// ------------------------------------------------------------------------
484
 
485
if ( ! function_exists('form_submit'))
486
{
487
	/**
488
	 * Submit Button
489
	 *
490
	 * @param	mixed
491
	 * @param	string
492
	 * @param	mixed
493
	 * @return	string
494
	 */
495
	function form_submit($data = '', $value = '', $extra = '')
496
	{
497
		$defaults = array(
498
			'type' => 'submit',
499
			'name' => is_array($data) ? '' : $data,
500
			'value' => $value
501
		);
502
 
503
		return '<input '._parse_form_attributes($data, $defaults)._attributes_to_string($extra)." />\n";
504
	}
505
}
506
 
507
// ------------------------------------------------------------------------
508
 
509
if ( ! function_exists('form_reset'))
510
{
511
	/**
512
	 * Reset Button
513
	 *
514
	 * @param	mixed
515
	 * @param	string
516
	 * @param	mixed
517
	 * @return	string
518
	 */
519
	function form_reset($data = '', $value = '', $extra = '')
520
	{
521
		$defaults = array(
522
			'type' => 'reset',
523
			'name' => is_array($data) ? '' : $data,
524
			'value' => $value
525
		);
526
 
527
		return '<input '._parse_form_attributes($data, $defaults)._attributes_to_string($extra)." />\n";
528
	}
529
}
530
 
531
// ------------------------------------------------------------------------
532
 
533
if ( ! function_exists('form_button'))
534
{
535
	/**
536
	 * Form Button
537
	 *
538
	 * @param	mixed
539
	 * @param	string
540
	 * @param	mixed
541
	 * @return	string
542
	 */
543
	function form_button($data = '', $content = '', $extra = '')
544
	{
545
		$defaults = array(
546
			'name' => is_array($data) ? '' : $data,
547
			'type' => 'button'
548
		);
549
 
550
		if (is_array($data) && isset($data['content']))
551
		{
552
			$content = $data['content'];
553
			unset($data['content']); // content is not an attribute
554
		}
555
 
556
		return '<button '._parse_form_attributes($data, $defaults)._attributes_to_string($extra).'>'
557
			.$content
558
			."</button>\n";
559
	}
560
}
561
 
562
// ------------------------------------------------------------------------
563
 
564
if ( ! function_exists('form_label'))
565
{
566
	/**
567
	 * Form Label Tag
568
	 *
569
	 * @param	string	The text to appear onscreen
570
	 * @param	string	The id the label applies to
571
	 * @param	array	Additional attributes
572
	 * @return	string
573
	 */
574
	function form_label($label_text = '', $id = '', $attributes = array())
575
	{
576
 
577
		$label = '<label';
578
 
579
		if ($id !== '')
580
		{
581
			$label .= ' for="'.$id.'"';
582
		}
583
 
584
		if (is_array($attributes) && count($attributes) > 0)
585
		{
586
			foreach ($attributes as $key => $val)
587
			{
588
				$label .= ' '.$key.'="'.$val.'"';
589
			}
590
		}
591
 
592
		return $label.'>'.$label_text.'</label>';
593
	}
594
}
595
 
596
// ------------------------------------------------------------------------
597
 
598
if ( ! function_exists('form_fieldset'))
599
{
600
	/**
601
	 * Fieldset Tag
602
	 *
603
	 * Used to produce <fieldset><legend>text</legend>.  To close fieldset
604
	 * use form_fieldset_close()
605
	 *
606
	 * @param	string	The legend text
607
	 * @param	array	Additional attributes
608
	 * @return	string
609
	 */
610
	function form_fieldset($legend_text = '', $attributes = array())
611
	{
612
		$fieldset = '<fieldset'._attributes_to_string($attributes).">\n";
613
		if ($legend_text !== '')
614
		{
615
			return $fieldset.'<legend>'.$legend_text."</legend>\n";
616
		}
617
 
618
		return $fieldset;
619
	}
620
}
621
 
622
// ------------------------------------------------------------------------
623
 
624
if ( ! function_exists('form_fieldset_close'))
625
{
626
	/**
627
	 * Fieldset Close Tag
628
	 *
629
	 * @param	string
630
	 * @return	string
631
	 */
632
	function form_fieldset_close($extra = '')
633
	{
634
		return '</fieldset>'.$extra;
635
	}
636
}
637
 
638
// ------------------------------------------------------------------------
639
 
640
if ( ! function_exists('form_close'))
641
{
642
	/**
643
	 * Form Close Tag
644
	 *
645
	 * @param	string
646
	 * @return	string
647
	 */
648
	function form_close($extra = '')
649
	{
650
		return '</form>'.$extra;
651
	}
652
}
653
 
654
// ------------------------------------------------------------------------
655
 
656
if ( ! function_exists('form_prep'))
657
{
658
	/**
659
	 * Form Prep
660
	 *
661
	 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
662
	 *
663
	 * @deprecated	3.0.0	An alias for html_escape()
664
	 * @param	string|string[]	$str		Value to escape
665
	 * @return	string|string[]	Escaped values
666
	 */
667
	function form_prep($str)
668
	{
669
		return html_escape($str, TRUE);
670
	}
671
}
672
 
673
// ------------------------------------------------------------------------
674
 
675
if ( ! function_exists('set_value'))
676
{
677
	/**
678
	 * Form Value
679
	 *
680
	 * Grabs a value from the POST array for the specified field so you can
681
	 * re-populate an input field or textarea. If Form Validation
682
	 * is active it retrieves the info from the validation class
683
	 *
684
	 * @param	string	$field		Field name
685
	 * @param	string	$default	Default value
686
	 * @param	bool	$html_escape	Whether to escape HTML special characters or not
687
	 * @return	string
688
	 */
689
	function set_value($field, $default = '', $html_escape = TRUE)
690
	{
691
		$CI =& get_instance();
692
 
693
		$value = (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
694
			? $CI->form_validation->set_value($field, $default)
695
			: $CI->input->post($field, FALSE);
696
 
697
		isset($value) OR $value = $default;
698
		return ($html_escape) ? html_escape($value) : $value;
699
	}
700
}
701
 
702
// ------------------------------------------------------------------------
703
 
704
if ( ! function_exists('set_select'))
705
{
706
	/**
707
	 * Set Select
708
	 *
709
	 * Let's you set the selected value of a <select> menu via data in the POST array.
710
	 * If Form Validation is active it retrieves the info from the validation class
711
	 *
712
	 * @param	string
713
	 * @param	string
714
	 * @param	bool
715
	 * @return	string
716
	 */
717
	function set_select($field, $value = '', $default = FALSE)
718
	{
719
		$CI =& get_instance();
720
 
721
		if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
722
		{
723
			return $CI->form_validation->set_select($field, $value, $default);
724
		}
725
		elseif (($input = $CI->input->post($field, FALSE)) === NULL)
726
		{
727
			return ($default === TRUE) ? ' selected="selected"' : '';
728
		}
729
 
730
		$value = (string) $value;
731
		if (is_array($input))
732
		{
733
			// Note: in_array('', array(0)) returns TRUE, do not use it
734
			foreach ($input as &$v)
735
			{
736
				if ($value === $v)
737
				{
738
					return ' selected="selected"';
739
				}
740
			}
741
 
742
			return '';
743
		}
744
 
745
		return ($input === $value) ? ' selected="selected"' : '';
746
	}
747
}
748
 
749
// ------------------------------------------------------------------------
750
 
751
if ( ! function_exists('set_checkbox'))
752
{
753
	/**
754
	 * Set Checkbox
755
	 *
756
	 * Let's you set the selected value of a checkbox via the value in the POST array.
757
	 * If Form Validation is active it retrieves the info from the validation class
758
	 *
759
	 * @param	string
760
	 * @param	string
761
	 * @param	bool
762
	 * @return	string
763
	 */
764
	function set_checkbox($field, $value = '', $default = FALSE)
765
	{
766
		$CI =& get_instance();
767
 
768
		if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
769
		{
770
			return $CI->form_validation->set_checkbox($field, $value, $default);
771
		}
772
 
773
		// Form inputs are always strings ...
774
		$value = (string) $value;
775
		$input = $CI->input->post($field, FALSE);
776
 
777
		if (is_array($input))
778
		{
779
			// Note: in_array('', array(0)) returns TRUE, do not use it
780
			foreach ($input as &$v)
781
			{
782
				if ($value === $v)
783
				{
784
					return ' checked="checked"';
785
				}
786
			}
787
 
788
			return '';
789
		}
790
 
791
		// Unchecked checkbox and radio inputs are not even submitted by browsers ...
792
		if ($CI->input->method() === 'post')
793
		{
794
			return ($input === $value) ? ' checked="checked"' : '';
795
		}
796
 
797
		return ($default === TRUE) ? ' checked="checked"' : '';
798
	}
799
}
800
 
801
// ------------------------------------------------------------------------
802
 
803
if ( ! function_exists('set_radio'))
804
{
805
	/**
806
	 * Set Radio
807
	 *
808
	 * Let's you set the selected value of a radio field via info in the POST array.
809
	 * If Form Validation is active it retrieves the info from the validation class
810
	 *
811
	 * @param	string	$field
812
	 * @param	string	$value
813
	 * @param	bool	$default
814
	 * @return	string
815
	 */
816
	function set_radio($field, $value = '', $default = FALSE)
817
	{
818
		$CI =& get_instance();
819
 
820
		if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
821
		{
822
			return $CI->form_validation->set_radio($field, $value, $default);
823
		}
824
 
825
		// Form inputs are always strings ...
826
		$value = (string) $value;
827
		$input = $CI->input->post($field, FALSE);
828
 
829
		if (is_array($input))
830
		{
831
			// Note: in_array('', array(0)) returns TRUE, do not use it
832
			foreach ($input as &$v)
833
			{
834
				if ($value === $v)
835
				{
836
					return ' checked="checked"';
837
				}
838
			}
839
 
840
			return '';
841
		}
842
 
843
		// Unchecked checkbox and radio inputs are not even submitted by browsers ...
844
		if ($CI->input->method() === 'post')
845
		{
846
			return ($input === $value) ? ' checked="checked"' : '';
847
		}
848
 
849
		return ($default === TRUE) ? ' checked="checked"' : '';
850
	}
851
}
852
 
853
// ------------------------------------------------------------------------
854
 
855
if ( ! function_exists('form_error'))
856
{
857
	/**
858
	 * Form Error
859
	 *
860
	 * Returns the error for a specific form field. This is a helper for the
861
	 * form validation class.
862
	 *
863
	 * @param	string
864
	 * @param	string
865
	 * @param	string
866
	 * @return	string
867
	 */
868
	function form_error($field = '', $prefix = '', $suffix = '')
869
	{
870
		if (FALSE === ($OBJ =& _get_validation_object()))
871
		{
872
			return '';
873
		}
874
 
875
		return $OBJ->error($field, $prefix, $suffix);
876
	}
877
}
878
 
879
// ------------------------------------------------------------------------
880
 
881
if ( ! function_exists('validation_errors'))
882
{
883
	/**
884
	 * Validation Error String
885
	 *
886
	 * Returns all the errors associated with a form submission. This is a helper
887
	 * function for the form validation class.
888
	 *
889
	 * @param	string
890
	 * @param	string
891
	 * @return	string
892
	 */
893
	function validation_errors($prefix = '', $suffix = '')
894
	{
895
		if (FALSE === ($OBJ =& _get_validation_object()))
896
		{
897
			return '';
898
		}
899
 
900
		return $OBJ->error_string($prefix, $suffix);
901
	}
902
}
903
 
904
// ------------------------------------------------------------------------
905
 
906
if ( ! function_exists('_parse_form_attributes'))
907
{
908
	/**
909
	 * Parse the form attributes
910
	 *
911
	 * Helper function used by some of the form helpers
912
	 *
913
	 * @param	array	$attributes	List of attributes
914
	 * @param	array	$default	Default values
915
	 * @return	string
916
	 */
917
	function _parse_form_attributes($attributes, $default)
918
	{
919
		if (is_array($attributes))
920
		{
921
			foreach ($default as $key => $val)
922
			{
923
				if (isset($attributes[$key]))
924
				{
925
					$default[$key] = $attributes[$key];
926
					unset($attributes[$key]);
927
				}
928
			}
929
 
930
			if (count($attributes) > 0)
931
			{
932
				$default = array_merge($default, $attributes);
933
			}
934
		}
935
 
936
		$att = '';
937
 
938
		foreach ($default as $key => $val)
939
		{
940
			if ($key === 'value')
941
			{
942
				$val = html_escape($val);
943
			}
944
			elseif ($key === 'name' && ! strlen($default['name']))
945
			{
946
				continue;
947
			}
948
 
949
			$att .= $key.'="'.$val.'" ';
950
		}
951
 
952
		return $att;
953
	}
954
}
955
 
956
// ------------------------------------------------------------------------
957
 
958
if ( ! function_exists('_attributes_to_string'))
959
{
960
	/**
961
	 * Attributes To String
962
	 *
963
	 * Helper function used by some of the form helpers
964
	 *
965
	 * @param	mixed
966
	 * @return	string
967
	 */
968
	function _attributes_to_string($attributes)
969
	{
970
		if (empty($attributes))
971
		{
972
			return '';
973
		}
974
 
975
		if (is_object($attributes))
976
		{
977
			$attributes = (array) $attributes;
978
		}
979
 
980
		if (is_array($attributes))
981
		{
982
			$atts = '';
983
 
984
			foreach ($attributes as $key => $val)
985
			{
986
				$atts .= ' '.$key.'="'.$val.'"';
987
			}
988
 
989
			return $atts;
990
		}
991
 
992
		if (is_string($attributes))
993
		{
994
			return ' '.$attributes;
995
		}
996
 
997
		return FALSE;
998
	}
999
}
1000
 
1001
// ------------------------------------------------------------------------
1002
 
1003
if ( ! function_exists('_get_validation_object'))
1004
{
1005
	/**
1006
	 * Validation Object
1007
	 *
1008
	 * Determines what the form validation class was instantiated as, fetches
1009
	 * the object and returns it.
1010
	 *
1011
	 * @return	mixed
1012
	 */
1013
	function &_get_validation_object()
1014
	{
1015
		$CI =& get_instance();
1016
 
1017
		// We set this as a variable since we're returning by reference.
1018
		$return = FALSE;
1019
 
1020
		if (FALSE !== ($object = $CI->load->is_loaded('Form_validation')))
1021
		{
1022
			if ( ! isset($CI->$object) OR ! is_object($CI->$object))
1023
			{
1024
				return $return;
1025
			}
1026
 
1027
			return $CI->$object;
1028
		}
1029
 
1030
		return $return;
1031
	}
1032
}