form.php 14.5 KB
Newer Older
Taylor Otwell committed
1
<?php namespace Laravel;
2 3 4 5

class Form {

	/**
Taylor Otwell committed
6 7
	 * All of the label names that have been created.
	 *
8 9
	 * @var array
	 */
10
	public static $labels = array();
11

12
	/**
13
	 * The registered custom macros.
14 15 16
	 *
	 * @var array
	 */
17
	public static $macros = array();
18

19 20 21 22
	/**
	 * Registers a custom macro.
	 *
	 * @param  string   $name
Sergii Grebeniuk committed
23
	 * @param  Closure  $macro
24 25
	 * @return void
	 */
26
	public static function macro($name, $macro)
27
	{
28
		static::$macros[$name] = $macro;
29
	}
30 31

	/**
32 33
	 * Open a HTML form.
	 *
34 35 36 37 38 39 40 41 42 43 44 45 46
	 * <code>
	 *		// Open a "POST" form to the current request URI
	 *		echo Form::open();
	 *
	 *		// Open a "POST" form to a given URI
	 *		echo Form::open('user/profile');
	 *
	 *		// Open a "PUT" form to a given URI
	 *		echo Form::open('user/profile', 'put');
	 *
	 *		// Open a form that has HTML attributes
	 *		echo Form::open('user/profile', 'post', array('class' => 'profile'));
	 * </code>
Taylor Otwell committed
47
	 *
48 49 50 51
	 * @param  string   $action
	 * @param  string   $method
	 * @param  array    $attributes
	 * @param  bool     $https
52
	 * @return string
53
	 */
54
	public static function open($action = null, $method = 'POST', $attributes = array(), $https = null)
55
	{
56 57
		$method = strtoupper($method);

58
		$attributes['method'] =  static::method($method);
59

60
		$attributes['action'] = static::action($action, $https);
61

62 63 64
		// If a character encoding has not been specified in the attributes, we will
		// use the default encoding as specified in the application configuration
		// file for the "accept-charset" attribute.
65 66
		if ( ! array_key_exists('accept-charset', $attributes))
		{
67
			$attributes['accept-charset'] = Config::get('application.encoding');
68 69
		}

Taylor Otwell committed
70 71
		$append = '';

72 73
		// Since PUT and DELETE methods are not actually supported by HTML forms,
		// we'll create a hidden input element that contains the request method
74
		// and set the actual request method variable to POST.
Taylor Otwell committed
75 76 77 78
		if ($method == 'PUT' or $method == 'DELETE')
		{
			$append = static::hidden(Request::spoofer, $method);
		}
79

80
		return '<form'.HTML::attributes($attributes).'>'.$append;
Taylor Otwell committed
81 82 83 84 85 86 87 88
	}

	/**
	 * Determine the appropriate request method to use for a form.
	 *
	 * @param  string  $method
	 * @return string
	 */
89
	protected static function method($method)
Taylor Otwell committed
90
	{
91
		return ($method !== 'GET') ? 'POST' : $method;
Taylor Otwell committed
92
	}
93

Taylor Otwell committed
94 95 96 97 98
	/**
	 * Determine the appropriate action parameter to use for a form.
	 *
	 * If no action is specified, the current request URI will be used.
	 *
99 100
	 * @param  string   $action
	 * @param  bool     $https
Taylor Otwell committed
101 102
	 * @return string
	 */
103
	protected static function action($action, $https)
Taylor Otwell committed
104
	{
105 106 107
		$uri = (is_null($action)) ? URI::current() : $action;

		return HTML::entities(URL::to($uri, $https));
108 109 110
	}

	/**
Taylor Otwell committed
111
	 * Open a HTML form with a HTTPS action URI.
112 113 114 115 116 117
	 *
	 * @param  string  $action
	 * @param  string  $method
	 * @param  array   $attributes
	 * @return string
	 */
118
	public static function open_secure($action = null, $method = 'POST', $attributes = array())
119
	{
120
		return static::open($action, $method, $attributes, true);
121 122 123
	}

	/**
124 125 126 127 128
	 * Open a HTML form that accepts file uploads.
	 *
	 * @param  string  $action
	 * @param  string  $method
	 * @param  array   $attributes
129
	 * @param  bool    $https
130
	 * @return string
131
	 */
132
	public static function open_for_files($action = null, $method = 'POST', $attributes = array(), $https = null)
133 134 135
	{
		$attributes['enctype'] = 'multipart/form-data';

136
		return static::open($action, $method, $attributes, $https);
137 138 139
	}

	/**
Taylor Otwell committed
140
	 * Open a HTML form that accepts file uploads with a HTTPS action URI.
141 142 143 144 145
	 *
	 * @param  string  $action
	 * @param  string  $method
	 * @param  array   $attributes
	 * @return string
146
	 */
147
	public static function open_secure_for_files($action = null, $method = 'POST', $attributes = array())
148
	{
149
		return static::open_for_files($action, $method, $attributes, true);
150 151 152
	}

	/**
Taylor Otwell committed
153 154 155 156
	 * Close a HTML form.
	 *
	 * @return string
	 */
157
	public static function close()
Taylor Otwell committed
158 159 160 161 162
	{
		return '</form>';
	}

	/**
163 164 165 166
	 * Generate a hidden field containing the current CSRF token.
	 *
	 * @return string
	 */
167
	public static function token()
168
	{
169
		return static::input('hidden', Session::csrf_token, Session::token());
170 171 172 173
	}

	/**
	 * Create a HTML label element.
Taylor Otwell committed
174
	 *
175 176 177 178 179
	 * <code>
	 *		// Create a label for the "email" input element
	 *		echo Form::label('email', 'E-Mail Address');
	 * </code>
	 *
180 181 182 183
	 * @param  string  $name
	 * @param  string  $value
	 * @param  array   $attributes
	 * @return string
184
	 */
185
	public static function label($name, $value, $attributes = array(), $escape_html = true)
186
	{
187
		static::$labels[] = $name;
Taylor Otwell committed
188

Taylor Otwell committed
189 190
		$attributes = HTML::attributes($attributes);

191 192 193
		if ($escape_html) {
			$value = HTML::entities($value);
		}
Taylor Otwell committed
194

195
		return '<label for="'.$name.'"'.$attributes.'>'.$value.'</label>';
196 197 198
	}

	/**
Taylor Otwell committed
199
	 * Create a HTML input element.
200
	 *
201 202 203 204 205 206 207 208
	 * <code>
	 *		// Create a "text" input element named "email"
	 *		echo Form::input('text', 'email');
	 *
	 *		// Create an input element with a specified default value
	 *		echo Form::input('text', 'email', 'example@gmail.com');
	 * </code>
	 *
Phill Sparks committed
209
	 * @param  string  $type
210
	 * @param  string  $name
Taylor Otwell committed
211
	 * @param  mixed   $value
212 213
	 * @param  array   $attributes
	 * @return string
214
	 */
215
	public static function input($type, $name, $value = null, $attributes = array())
216
	{
217 218
		$name = (isset($attributes['name'])) ? $attributes['name'] : $name;

219
		$id = static::id($name, $attributes);
220

221 222
		$attributes = array_merge($attributes, compact('type', 'name', 'value', 'id'));

223
		return '<input'.HTML::attributes($attributes).'>';
224 225 226
	}

	/**
227 228 229 230 231 232 233
	 * Create a HTML text input element.
	 *
	 * @param  string  $name
	 * @param  string  $value
	 * @param  array   $attributes
	 * @return string
	 */
234
	public static function text($name, $value = null, $attributes = array())
235
	{
236
		return static::input('text', $name, $value, $attributes);
237 238 239
	}

	/**
240 241 242 243 244
	 * Create a HTML password input element.
	 *
	 * @param  string  $name
	 * @param  array   $attributes
	 * @return string
245
	 */
246
	public static function password($name, $attributes = array())
247
	{
248
		return static::input('password', $name, null, $attributes);
249 250 251
	}

	/**
252 253 254 255 256 257 258
	 * Create a HTML hidden input element.
	 *
	 * @param  string  $name
	 * @param  string  $value
	 * @param  array   $attributes
	 * @return string
	 */
259
	public static function hidden($name, $value = null, $attributes = array())
260
	{
261
		return static::input('hidden', $name, $value, $attributes);
262 263 264
	}

	/**
265 266 267 268 269 270
	 * Create a HTML search input element.
	 *
	 * @param  string  $name
	 * @param  string  $value
	 * @param  array   $attributes
	 * @return string
271
	 */
272
	public static function search($name, $value = null, $attributes = array())
273
	{
274
		return static::input('search', $name, $value, $attributes);
275 276 277
	}

	/**
278
	 * Create a HTML email input element.
279 280 281 282 283
	 *
	 * @param  string  $name
	 * @param  string  $value
	 * @param  array   $attributes
	 * @return string
284
	 */
285
	public static function email($name, $value = null, $attributes = array())
286
	{
287
		return static::input('email', $name, $value, $attributes);
288 289 290
	}

	/**
291
	 * Create a HTML telephone input element.
292 293 294 295 296
	 *
	 * @param  string  $name
	 * @param  string  $value
	 * @param  array   $attributes
	 * @return string
297
	 */
298
	public static function telephone($name, $value = null, $attributes = array())
299
	{
300
		return static::input('tel', $name, $value, $attributes);
301 302 303
	}

	/**
304
	 * Create a HTML URL input element.
305 306 307 308 309
	 *
	 * @param  string  $name
	 * @param  string  $value
	 * @param  array   $attributes
	 * @return string
310
	 */
311
	public static function url($name, $value = null, $attributes = array())
312
	{
313
		return static::input('url', $name, $value, $attributes);
314 315 316
	}

	/**
Taylor Otwell committed
317
	 * Create a HTML number input element.
318 319 320 321 322
	 *
	 * @param  string  $name
	 * @param  string  $value
	 * @param  array   $attributes
	 * @return string
323
	 */
324
	public static function number($name, $value = null, $attributes = array())
325
	{
326
		return static::input('number', $name, $value, $attributes);
327
	}
328

329 330 331 332 333 334 335
	/**
	 * Create a HTML date input element.
	 *
	 * @param  string  $name
	 * @param  string  $value
	 * @param  array   $attributes
	 * @return string
336
	 */
337 338 339 340
	public static function date($name, $value = null, $attributes = array())
	{
		return static::input('date', $name, $value, $attributes);
	}
341 342

	/**
343 344 345 346 347
	 * Create a HTML file input element.
	 *
	 * @param  string  $name
	 * @param  array   $attributes
	 * @return string
348
	 */
349
	public static function file($name, $attributes = array())
350
	{
351
		return static::input('file', $name, null, $attributes);
352 353 354
	}

	/**
Taylor Otwell committed
355
	 * Create a HTML textarea element.
356
	 *
Taylor Otwell committed
357
	 * @param  string  $name
358
	 * @param  string  $value
359 360 361
	 * @param  array   $attributes
	 * @return string
	 */
362
	public static function textarea($name, $value = '', $attributes = array())
363
	{
Taylor Otwell committed
364 365 366
		$attributes['name'] = $name;

		$attributes['id'] = static::id($name, $attributes);
367

368
		if ( ! isset($attributes['rows'])) $attributes['rows'] = 10;
369

370
		if ( ! isset($attributes['cols'])) $attributes['cols'] = 50;
Taylor Otwell committed
371

372
		return '<textarea'.HTML::attributes($attributes).'>'.HTML::entities($value).'</textarea>';
373 374 375
	}

	/**
Taylor Otwell committed
376
	 * Create a HTML select element.
377
	 *
378 379 380 381 382 383 384 385
	 * <code>
	 *		// Create a HTML select element filled with options
	 *		echo Form::select('sizes', array('S' => 'Small', 'L' => 'Large'));
	 *
	 *		// Create a select element with a default selected value
	 *		echo Form::select('sizes', array('S' => 'Small', 'L' => 'Large'), 'L');
	 * </code>
	 *
386
	 * @param  string  $name
Taylor Otwell committed
387 388
	 * @param  array   $options
	 * @param  string  $selected
389 390
	 * @param  array   $attributes
	 * @return string
391
	 */
392
	public static function select($name, $options = array(), $selected = null, $attributes = array())
393
	{
394
		$attributes['id'] = static::id($name, $attributes);
395

396
		$attributes['name'] = $name;
Taylor Otwell committed
397 398 399 400 401

		$html = array();

		foreach ($options as $value => $display)
		{
402
			if (is_array($display))
403 404 405 406 407 408 409
			{
				$html[] = static::optgroup($display, $value, $selected);
			}
			else
			{
				$html[] = static::option($value, $display, $selected);
			}
Taylor Otwell committed
410 411
		}

412
		return '<select'.HTML::attributes($attributes).'>'.implode('', $html).'</select>';
413 414 415
	}

	/**
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
	 * Create a HTML select element optgroup.
	 *
	 * @param  array   $options
	 * @param  string  $label
	 * @param  string  $selected
	 * @return string
	 */
	protected static function optgroup($options, $label, $selected)
	{
		$html = array();

		foreach ($options as $value => $display)
		{
			$html[] = static::option($value, $display, $selected);
		}

432
		return '<optgroup label="'.HTML::entities($label).'">'.implode('', $html).'</optgroup>';
433 434 435
	}

	/**
436 437 438 439
	 * Create a HTML select element option.
	 *
	 * @param  string  $value
	 * @param  string  $display
Phill Sparks committed
440
	 * @param  string  $selected
441 442 443 444
	 * @return string
	 */
	protected static function option($value, $display, $selected)
	{
445 446 447 448 449 450
		if (is_array($selected))
		{
			$selected = (in_array($value, $selected)) ? 'selected' : null;
		}
		else
		{
451
			$selected = ((string) $value == (string) $selected) ? 'selected' : null;
452
		}
453 454 455 456 457 458 459

		$attributes = array('value' => HTML::entities($value), 'selected' => $selected);

		return '<option'.HTML::attributes($attributes).'>'.HTML::entities($display).'</option>';
	}

	/**
460 461
	 * Create a HTML checkbox input element.
	 *
462 463 464 465 466 467 468 469
	 * <code>
	 *		// Create a checkbox element
	 *		echo Form::checkbox('terms', 'yes');
	 *
	 *		// Create a checkbox that is selected by default
	 *		echo Form::checkbox('terms', 'yes', true);
	 * </code>
	 *
470 471 472 473 474 475
	 * @param  string  $name
	 * @param  string  $value
	 * @param  bool    $checked
	 * @param  array   $attributes
	 * @return string
	 */
476
	public static function checkbox($name, $value = 1, $checked = false, $attributes = array())
477
	{
478
		return static::checkable('checkbox', $name, $value, $checked, $attributes);
479 480 481 482 483
	}

	/**
	 * Create a HTML radio button input element.
	 *
484 485 486 487 488 489 490 491
	 * <code>
	 *		// Create a radio button element
	 *		echo Form::radio('drinks', 'Milk');
	 *
	 *		// Create a radio button that is selected by default
	 *		echo Form::radio('drinks', 'Milk', true);
	 * </code>
	 *
492 493 494 495 496 497
	 * @param  string  $name
	 * @param  string  $value
	 * @param  bool    $checked
	 * @param  array   $attributes
	 * @return string
	 */
498
	public static function radio($name, $value = null, $checked = false, $attributes = array())
499
	{
500 501
		if (is_null($value)) $value = $name;

502
		return static::checkable('radio', $name, $value, $checked, $attributes);
503 504 505 506 507 508 509 510 511 512 513 514
	}

	/**
	 * Create a checkable input element.
	 *
	 * @param  string  $type
	 * @param  string  $name
	 * @param  string  $value
	 * @param  bool    $checked
	 * @param  array   $attributes
	 * @return string
	 */
515
	protected static function checkable($type, $name, $value, $checked, $attributes)
516
	{
517 518 519
		if ($checked) $attributes['checked'] = 'checked';

		$attributes['id'] = static::id($name, $attributes);
520

521
		return static::input($type, $name, $value, $attributes);
522 523 524
	}

	/**
Taylor Otwell committed
525
	 * Create a HTML submit input element.
526 527 528 529 530
	 *
	 * @param  string  $value
	 * @param  array   $attributes
	 * @return string
	 */
531
	public static function submit($value = null, $attributes = array())
532
	{
533
		return static::input('submit', null, $value, $attributes);
534 535 536
	}

	/**
Taylor Otwell committed
537
	 * Create a HTML reset input element.
538
	 *
Taylor Otwell committed
539
	 * @param  string  $value
540 541
	 * @param  array   $attributes
	 * @return string
Taylor Otwell committed
542
	 */
543
	public static function reset($value = null, $attributes = array())
544
	{
545
		return static::input('reset', null, $value, $attributes);
Taylor Otwell committed
546
	}
547

Taylor Otwell committed
548 549 550
	/**
	 * Create a HTML image input element.
	 *
551 552 553 554 555
	 * <code>
	 *		// Create an image input element
	 *		echo Form::image('img/submit.png');
	 * </code>
	 *
Taylor Otwell committed
556
	 * @param  string  $url
Phill Sparks committed
557
	 * @param  string  $name
Taylor Otwell committed
558 559 560
	 * @param  array   $attributes
	 * @return string
	 */
561
	public static function image($url, $name = null, $attributes = array())
Taylor Otwell committed
562
	{
563
		$attributes['src'] = URL::to_asset($url);
564

565
		return static::input('image', $name, null, $attributes);
566 567 568
	}

	/**
Taylor Otwell committed
569
	 * Create a HTML button element.
570
	 *
Taylor Otwell committed
571
	 * @param  string  $value
572 573
	 * @param  array   $attributes
	 * @return string
Taylor Otwell committed
574
	 */
575
	public static function button($value = null, $attributes = array())
576
	{
577
		return '<button'.HTML::attributes($attributes).'>'.HTML::entities($value).'</button>';
578 579
	}

580 581 582 583 584 585 586
	/**
	 * Determine the ID attribute for a form element.
	 *
	 * @param  string  $name
	 * @param  array   $attributes
	 * @return mixed
	 */
587
	protected static function id($name, $attributes)
588
	{
589 590
		// If an ID has been explicitly specified in the attributes, we will
		// use that ID. Otherwise, we will look for an ID in the array of
591
		// label names so labels and their elements have the same ID.
592 593 594 595
		if (array_key_exists('id', $attributes))
		{
			return $attributes['id'];
		}
596

597 598 599 600
		if (in_array($name, static::$labels))
		{
			return $name;
		}
601 602
	}

603 604 605 606 607 608 609 610 611
	/**
	 * Dynamically handle calls to custom macros.
	 *
	 * @param  string  $method
	 * @param  array   $parameters
	 * @return mixed
	 */
	public static function __callStatic($method, $parameters)
	{
612 613 614 615 616 617
		if (isset(static::$macros[$method]))
		{
			return call_user_func_array(static::$macros[$method], $parameters);
		}

		throw new \Exception("Method [$method] does not exist.");
618 619
	}

620
}