form.php 12.9 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
	protected static $labels = array();
11 12

	/**
13 14
	 * Open a HTML form.
	 *
15 16 17 18 19 20 21 22 23 24 25 26 27
	 * <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
28
	 *
29 30 31 32
	 * @param  string   $action
	 * @param  string   $method
	 * @param  array    $attributes
	 * @param  bool     $https
33
	 * @return string
34
	 */
35
	public static function open($action = null, $method = 'POST', $attributes = array(), $https = false)
36
	{
37 38
		$method = strtoupper($method);

39
		$attributes['method'] =  static::method($method);
40 41
		
		$attributes['action'] = static::action($action, $https);
42

43 44 45
		// 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.
46 47
		if ( ! array_key_exists('accept-charset', $attributes))
		{
48
			$attributes['accept-charset'] = Config::get('application.encoding');
49 50
		}

Taylor Otwell committed
51 52
		$append = '';

53 54 55 56
		// Since PUT and DELETE methods are not actually supported by HTML forms,
		// we'll create a hidden input element that contains the request method
		// and set the actual request method to POST. Laravel will look for the
		// hidden element to determine the request method.
Taylor Otwell committed
57 58 59 60
		if ($method == 'PUT' or $method == 'DELETE')
		{
			$append = static::hidden(Request::spoofer, $method);
		}
61

62
		return '<form'.HTML::attributes($attributes).'>'.$append.PHP_EOL;
Taylor Otwell committed
63 64 65 66 67 68 69 70
	}

	/**
	 * Determine the appropriate request method to use for a form.
	 *
	 * @param  string  $method
	 * @return string
	 */
71
	protected static function method($method)
Taylor Otwell committed
72
	{
73
		return ($method !== 'GET') ? 'POST' : $method;
Taylor Otwell committed
74
	}
75

Taylor Otwell committed
76 77 78 79 80
	/**
	 * Determine the appropriate action parameter to use for a form.
	 *
	 * If no action is specified, the current request URI will be used.
	 *
81 82
	 * @param  string   $action
	 * @param  bool     $https
Taylor Otwell committed
83 84
	 * @return string
	 */
85
	protected static function action($action, $https)
Taylor Otwell committed
86
	{
87 88 89
		$uri = (is_null($action)) ? URI::current() : $action;

		return HTML::entities(URL::to($uri, $https));
90 91 92
	}

	/**
Taylor Otwell committed
93
	 * Open a HTML form with a HTTPS action URI.
94 95 96 97 98 99
	 *
	 * @param  string  $action
	 * @param  string  $method
	 * @param  array   $attributes
	 * @return string
	 */
100
	public static function open_secure($action = null, $method = 'POST', $attributes = array())
101
	{
102
		return static::open($action, $method, $attributes, true);
103 104 105
	}

	/**
106 107 108 109 110
	 * Open a HTML form that accepts file uploads.
	 *
	 * @param  string  $action
	 * @param  string  $method
	 * @param  array   $attributes
111
	 * @param  bool    $https
112 113
	 * @return string
	 */	
114
	public static function open_for_files($action = null, $method = 'POST', $attributes = array(), $https = false)
115 116 117
	{
		$attributes['enctype'] = 'multipart/form-data';

118
		return static::open($action, $method, $attributes, $https);
119 120 121
	}

	/**
Taylor Otwell committed
122
	 * Open a HTML form that accepts file uploads with a HTTPS action URI.
123 124 125 126 127 128
	 *
	 * @param  string  $action
	 * @param  string  $method
	 * @param  array   $attributes
	 * @return string
	 */	
129
	public static function open_secure_for_files($action = null, $method = 'POST', $attributes = array())
130
	{
131
		return static::open_for_files($action, $method, $attributes, true);
132 133 134
	}

	/**
Taylor Otwell committed
135 136 137 138
	 * Close a HTML form.
	 *
	 * @return string
	 */
139
	public static function close()
Taylor Otwell committed
140 141 142 143 144
	{
		return '</form>';
	}

	/**
145 146 147 148
	 * Generate a hidden field containing the current CSRF token.
	 *
	 * @return string
	 */
149
	public static function token()
150
	{
151
		return static::input('hidden', Session::csrf_token, Session::token());
152 153 154 155
	}

	/**
	 * Create a HTML label element.
Taylor Otwell committed
156
	 *
157 158 159 160 161
	 * <code>
	 *		// Create a label for the "email" input element
	 *		echo Form::label('email', 'E-Mail Address');
	 * </code>
	 *
162 163 164 165 166
	 * @param  string  $name
	 * @param  string  $value
	 * @param  array   $attributes
	 * @return string
	 */		
167
	public static function label($name, $value, $attributes = array())
168
	{
169
		static::$labels[] = $name;
Taylor Otwell committed
170

Taylor Otwell committed
171 172 173 174 175
		$attributes = HTML::attributes($attributes);

		$value = HTML::entities($value);

		return '<label for="'.$name.'"'.$attributes.'>'.$value.'</label>'.PHP_EOL;
176 177 178
	}

	/**
Taylor Otwell committed
179
	 * Create a HTML input element.
180
	 *
181 182 183 184 185 186 187 188
	 * <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>
	 *
189
	 * @param  string  $name
Taylor Otwell committed
190
	 * @param  mixed   $value
191 192 193
	 * @param  array   $attributes
	 * @return string
	 */		
194
	public static function input($type, $name, $value = null, $attributes = array())
195
	{
196 197
		$name = (isset($attributes['name'])) ? $attributes['name'] : $name;

198
		$id = static::id($name, $attributes);
199

200 201 202
		$attributes = array_merge($attributes, compact('type', 'name', 'value', 'id'));

		return '<input'.HTML::attributes($attributes).'>'.PHP_EOL;
203 204 205
	}

	/**
206 207 208 209 210 211 212
	 * Create a HTML text input element.
	 *
	 * @param  string  $name
	 * @param  string  $value
	 * @param  array   $attributes
	 * @return string
	 */
213
	public static function text($name, $value = null, $attributes = array())
214
	{
215
		return static::input('text', $name, $value, $attributes);
216 217 218
	}

	/**
219 220 221 222 223
	 * Create a HTML password input element.
	 *
	 * @param  string  $name
	 * @param  array   $attributes
	 * @return string
Taylor Otwell committed
224
	 */		
225
	public static function password($name, $attributes = array())
226
	{
227
		return static::input('password', $name, null, $attributes);
228 229 230
	}

	/**
231 232 233 234 235 236 237
	 * Create a HTML hidden input element.
	 *
	 * @param  string  $name
	 * @param  string  $value
	 * @param  array   $attributes
	 * @return string
	 */
238
	public static function hidden($name, $value = null, $attributes = array())
239
	{
240
		return static::input('hidden', $name, $value, $attributes);
241 242 243
	}

	/**
244 245 246 247 248 249
	 * Create a HTML search input element.
	 *
	 * @param  string  $name
	 * @param  string  $value
	 * @param  array   $attributes
	 * @return string
Taylor Otwell committed
250
	 */		
251
	public static function search($name, $value = null, $attributes = array())
252
	{
253
		return static::input('search', $name, $value, $attributes);
254 255 256
	}

	/**
257
	 * Create a HTML email input element.
258 259 260 261 262
	 *
	 * @param  string  $name
	 * @param  string  $value
	 * @param  array   $attributes
	 * @return string
Taylor Otwell committed
263
	 */		
264
	public static function email($name, $value = null, $attributes = array())
265
	{
266
		return static::input('email', $name, $value, $attributes);
267 268 269
	}

	/**
270
	 * Create a HTML telephone input element.
271 272 273 274 275
	 *
	 * @param  string  $name
	 * @param  string  $value
	 * @param  array   $attributes
	 * @return string
276
	 */
277
	public static function telephone($name, $value = null, $attributes = array())
278
	{
279
		return static::input('tel', $name, $value, $attributes);
280 281 282
	}

	/**
283
	 * Create a HTML URL input element.
284 285 286 287 288
	 *
	 * @param  string  $name
	 * @param  string  $value
	 * @param  array   $attributes
	 * @return string
Taylor Otwell committed
289
	 */		
290
	public static function url($name, $value = null, $attributes = array())
291
	{
292
		return static::input('url', $name, $value, $attributes);
293 294 295
	}

	/**
Taylor Otwell committed
296
	 * Create a HTML number input element.
297 298 299 300 301
	 *
	 * @param  string  $name
	 * @param  string  $value
	 * @param  array   $attributes
	 * @return string
Taylor Otwell committed
302
	 */		
303
	public static function number($name, $value = null, $attributes = array())
304
	{
305
		return static::input('number', $name, $value, $attributes);
306 307 308
	}

	/**
309 310 311 312 313 314
	 * Create a HTML file input element.
	 *
	 * @param  string  $name
	 * @param  array   $attributes
	 * @return string
	 */			
315
	public static function file($name, $attributes = array())
316
	{
317
		return static::input('file', $name, null, $attributes);
318 319 320
	}

	/**
Taylor Otwell committed
321
	 * Create a HTML textarea element.
322
	 *
Taylor Otwell committed
323
	 * @param  string  $name
324
	 * @param  string  $value
325 326 327
	 * @param  array   $attributes
	 * @return string
	 */
328
	public static function textarea($name, $value = '', $attributes = array())
329
	{
Taylor Otwell committed
330 331 332
		$attributes['name'] = $name;

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

334
		if ( ! isset($attributes['rows'])) $attributes['rows'] = 10;
335

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

338
		return '<textarea'.HTML::attributes($attributes).'>'.HTML::entities($value).'</textarea>'.PHP_EOL;
339 340 341
	}

	/**
Taylor Otwell committed
342
	 * Create a HTML select element.
343
	 *
344 345 346 347 348 349 350 351
	 * <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>
	 *
352
	 * @param  string  $name
Taylor Otwell committed
353 354
	 * @param  array   $options
	 * @param  string  $selected
355 356
	 * @param  array   $attributes
	 * @return string
Taylor Otwell committed
357
	 */	
358
	public static function select($name, $options = array(), $selected = null, $attributes = array())
359
	{
360 361 362
		$attributes['id'] = static::id($name, $attributes);
		
		$attributes['name'] = $name;
Taylor Otwell committed
363 364 365 366 367

		$html = array();

		foreach ($options as $value => $display)
		{
368
			$html[] = static::option($value, $display, $selected);
Taylor Otwell committed
369 370
		}

371
		return '<select'.HTML::attributes($attributes).'>'.implode('', $html).'</select>'.PHP_EOL;
372 373 374
	}

	/**
375 376 377 378 379 380 381 382 383
	 * Create a HTML select element option.
	 *
	 * @param  string  $value
	 * @param  string  $display
	 * @return string  $selected
	 * @return string
	 */
	protected static function option($value, $display, $selected)
	{
384
		$selected = ($value == $selected) ? 'selected' : null;
385 386 387 388 389 390 391

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

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

	/**
392 393
	 * Create a HTML checkbox input element.
	 *
394 395 396 397 398 399 400 401
	 * <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>
	 *
402 403 404 405 406 407
	 * @param  string  $name
	 * @param  string  $value
	 * @param  bool    $checked
	 * @param  array   $attributes
	 * @return string
	 */
408
	public static function checkbox($name, $value = 1, $checked = false, $attributes = array())
409
	{
410
		return static::checkable('checkbox', $name, $value, $checked, $attributes);
411 412 413 414 415
	}

	/**
	 * Create a HTML radio button input element.
	 *
416 417 418 419 420 421 422 423
	 * <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>
	 *
424 425 426 427 428 429
	 * @param  string  $name
	 * @param  string  $value
	 * @param  bool    $checked
	 * @param  array   $attributes
	 * @return string
	 */
430
	public static function radio($name, $value = null, $checked = false, $attributes = array())
431
	{
432 433
		if (is_null($value)) $value = $name;

434
		return static::checkable('radio', $name, $value, $checked, $attributes);
435 436 437 438 439 440 441 442 443 444 445 446
	}

	/**
	 * Create a checkable input element.
	 *
	 * @param  string  $type
	 * @param  string  $name
	 * @param  string  $value
	 * @param  bool    $checked
	 * @param  array   $attributes
	 * @return string
	 */
447
	protected static function checkable($type, $name, $value, $checked, $attributes)
448
	{
449 450 451
		if ($checked) $attributes['checked'] = 'checked';

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

453
		return static::input($type, $name, $value, $attributes);
454 455 456
	}

	/**
Taylor Otwell committed
457
	 * Create a HTML submit input element.
458 459 460 461 462
	 *
	 * @param  string  $value
	 * @param  array   $attributes
	 * @return string
	 */
463
	public static function submit($value, $attributes = array())
464
	{
465
		return static::input('submit', null, $value, $attributes);
466 467 468
	}

	/**
Taylor Otwell committed
469
	 * Create a HTML reset input element.
470
	 *
Taylor Otwell committed
471
	 * @param  string  $value
472 473
	 * @param  array   $attributes
	 * @return string
Taylor Otwell committed
474
	 */
475
	public static function reset($value, $attributes = array())
476
	{
477
		return static::input('reset', null, $value, $attributes);
Taylor Otwell committed
478
	}
479

Taylor Otwell committed
480 481 482
	/**
	 * Create a HTML image input element.
	 *
483 484 485 486 487
	 * <code>
	 *		// Create an image input element
	 *		echo Form::image('img/submit.png');
	 * </code>
	 *
Taylor Otwell committed
488 489 490 491
	 * @param  string  $url
	 * @param  array   $attributes
	 * @return string
	 */
492
	public static function image($url, $name = null, $attributes = array())
Taylor Otwell committed
493
	{
494
		$attributes['src'] = URL::to_asset($url);
495

496
		return static::input('image', $name, null, $attributes);
497 498 499
	}

	/**
Taylor Otwell committed
500
	 * Create a HTML button element.
501 502
	 *
	 * @param  string  $name
Taylor Otwell committed
503
	 * @param  string  $value
504 505
	 * @param  array   $attributes
	 * @return string
Taylor Otwell committed
506
	 */
507
	public static function button($value, $attributes = array())
508
	{
509
		return '<button'.HTML::attributes($attributes).'>'.HTML::entities($value).'</button>'.PHP_EOL;
510 511
	}

512 513 514 515 516 517 518
	/**
	 * Determine the ID attribute for a form element.
	 *
	 * @param  string  $name
	 * @param  array   $attributes
	 * @return mixed
	 */
519
	protected static function id($name, $attributes)
520
	{
521 522 523 524
		// 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
		// label names as this makes it convenient to give input elements
		// the same ID as their corresponding labels.
525 526 527 528
		if (array_key_exists('id', $attributes))
		{
			return $attributes['id'];
		}
529

530 531 532 533
		if (in_array($name, static::$labels))
		{
			return $name;
		}
534 535
	}

536
}