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

class Form {

	/**
Taylor Otwell committed
6 7 8 9
	 * All of the label names that have been created.
	 *
	 * These names are stored so that input elements can automatically be assigned
	 * an ID based on the corresponding label name.
10 11 12
	 *
	 * @var array
	 */
13
	protected static $labels = array();
14 15

	/**
16 17
	 * Open a HTML form.
	 *
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
	 * If PUT or DELETE is specified as the form method, a hidden input field will be generated
	 * containing the request method. PUT and DELETE are not supported by HTML forms, so the
	 * hidden field will allow us to "spoof" PUT and DELETE requests.
	 *
	 * Unless specified, the "accept-charset" attribute will be set to the application encoding.
	 *
	 * <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
37
	 *
38 39 40 41
	 * @param  string   $action
	 * @param  string   $method
	 * @param  array    $attributes
	 * @param  bool     $https
42
	 * @return string
43
	 */
44
	public static function open($action = null, $method = 'POST', $attributes = array(), $https = false)
45
	{
46
		$attributes['method'] =  static::method($method);
47 48
		
		$attributes['action'] = static::action($action, $https);
49 50 51

		if ( ! array_key_exists('accept-charset', $attributes))
		{
52
			$attributes['accept-charset'] = Config::get('application.encoding');
53 54
		}

55
		$append = ($method == 'PUT' or $method == 'DELETE') ? static::hidden(Request::spoofer, $method) : '';
56

57
		return '<form'.HTML::attributes($attributes).'>'.$append.PHP_EOL;
Taylor Otwell committed
58 59 60 61 62 63 64 65 66 67 68
	}

	/**
	 * Determine the appropriate request method to use for a form.
	 *
	 * Since PUT and DELETE requests are spoofed using POST requests, we will substitute
	 * POST for any PUT or DELETE methods. Otherwise, the specified method will be used.
	 *
	 * @param  string  $method
	 * @return string
	 */
69
	protected static function method($method)
Taylor Otwell committed
70 71 72
	{
		return strtoupper(($method == 'PUT' or $method == 'DELETE') ? 'POST' : $method);
	}
73

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

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

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

114
		return static::open($action, $method, $attributes, $https);
115 116 117
	}

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

	/**
Taylor Otwell committed
131 132 133 134
	 * Close a HTML form.
	 *
	 * @return string
	 */
135
	public static function close()
Taylor Otwell committed
136 137 138 139 140
	{
		return '</form>';
	}

	/**
141 142 143 144
	 * Generate a hidden field containing the current CSRF token.
	 *
	 * @return string
	 */
145
	public static function token()
146
	{
Taylor Otwell committed
147
		return static::input('hidden', 'csrf_token', Session::token());
148 149 150 151
	}

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

167
		return '<label for="'.$name.'"'.HTML::attributes($attributes).'>'.HTML::entities($value).'</label>'.PHP_EOL;
168 169 170
	}

	/**
Taylor Otwell committed
171
	 * Create a HTML input element.
172
	 *
Taylor Otwell committed
173 174 175
	 * If an ID attribute is not specified and a label has been generated matching the input
	 * element name, the label name will be used as the element ID.
	 *
176 177 178 179 180 181 182 183
	 * <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>
	 *
184
	 * @param  string  $name
Taylor Otwell committed
185
	 * @param  mixed   $value
186 187 188
	 * @param  array   $attributes
	 * @return string
	 */		
189
	public static function input($type, $name, $value = null, $attributes = array())
190
	{
191 192
		$name = (isset($attributes['name'])) ? $attributes['name'] : $name;

193
		$id = static::id($name, $attributes);
194

195 196 197
		$attributes = array_merge($attributes, compact('type', 'name', 'value', 'id'));

		return '<input'.HTML::attributes($attributes).'>'.PHP_EOL;
198 199 200
	}

	/**
201 202 203 204 205 206 207
	 * Create a HTML text input element.
	 *
	 * @param  string  $name
	 * @param  string  $value
	 * @param  array   $attributes
	 * @return string
	 */
208
	public static function text($name, $value = null, $attributes = array())
209
	{
210
		return static::input('text', $name, $value, $attributes);
211 212 213
	}

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

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

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

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

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

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

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

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

	/**
Taylor Otwell committed
316
	 * Create a HTML textarea element.
317
	 *
Taylor Otwell committed
318
	 * @param  string  $name
319
	 * @param  string  $value
320 321 322
	 * @param  array   $attributes
	 * @return string
	 */
323
	public static function textarea($name, $value = '', $attributes = array())
324
	{
325
		$attributes = array_merge($attributes, array('id' => static::id($name, $attributes), 'name' => $name));
326

327
		if ( ! isset($attributes['rows'])) $attributes['rows'] = 10;
328

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

331
		return '<textarea'.HTML::attributes($attributes).'>'.HTML::entities($value).'</textarea>'.PHP_EOL;
332 333 334
	}

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

		$html = array();

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

364
		return '<select'.HTML::attributes($attributes).'>'.implode('', $html).'</select>'.PHP_EOL;
365 366 367
	}

	/**
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
	 * Create a HTML select element option.
	 *
	 * @param  string  $value
	 * @param  string  $display
	 * @return string  $selected
	 * @return string
	 */
	protected static function option($value, $display, $selected)
	{
		$selected = ($value === $selected) ? 'selected' : null;

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

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

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

	/**
	 * Create a HTML radio button input element.
	 *
409 410 411 412 413 414 415 416
	 * <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>
	 *
417 418 419 420 421 422
	 * @param  string  $name
	 * @param  string  $value
	 * @param  bool    $checked
	 * @param  array   $attributes
	 * @return string
	 */
423
	public static function radio($name, $value = null, $checked = false, $attributes = array())
424
	{
425 426
		if (is_null($value)) $value = $name;

427
		return static::checkable('radio', $name, $value, $checked, $attributes);
428 429 430 431 432 433 434 435 436 437 438 439
	}

	/**
	 * Create a checkable input element.
	 *
	 * @param  string  $type
	 * @param  string  $name
	 * @param  string  $value
	 * @param  bool    $checked
	 * @param  array   $attributes
	 * @return string
	 */
440
	protected static function checkable($type, $name, $value, $checked, $attributes)
441
	{
442 443 444
		if ($checked) $attributes['checked'] = 'checked';

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

446
		return static::input($type, $name, $value, $attributes);
447 448 449
	}

	/**
Taylor Otwell committed
450
	 * Create a HTML submit input element.
451 452 453 454 455
	 *
	 * @param  string  $value
	 * @param  array   $attributes
	 * @return string
	 */
456
	public static function submit($value, $attributes = array())
457
	{
458
		return static::input('submit', null, $value, $attributes);
459 460 461
	}

	/**
Taylor Otwell committed
462
	 * Create a HTML reset input element.
463
	 *
Taylor Otwell committed
464
	 * @param  string  $value
465 466
	 * @param  array   $attributes
	 * @return string
Taylor Otwell committed
467
	 */
468
	public static function reset($value, $attributes = array())
469
	{
470
		return static::input('reset', null, $value, $attributes);
Taylor Otwell committed
471
	}
472

Taylor Otwell committed
473 474 475
	/**
	 * Create a HTML image input element.
	 *
476 477 478 479 480 481 482
	 * The URL::to_asset method will be called on the given URL.
	 *
	 * <code>
	 *		// Create an image input element
	 *		echo Form::image('img/submit.png');
	 * </code>
	 *
Taylor Otwell committed
483 484 485 486
	 * @param  string  $url
	 * @param  array   $attributes
	 * @return string
	 */
487
	public static function image($url, $name = null, $attributes = array())
Taylor Otwell committed
488
	{
489
		$attributes['src'] = URL::to_asset($url);
490

491
		return static::input('image', $name, null, $attributes);
492 493 494
	}

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

507 508 509
	/**
	 * Determine the ID attribute for a form element.
	 *
510 511 512
	 * An explicitly specified ID in the attributes takes first precedence, then
	 * the label names will be checked for a label matching the element name.
	 *
513 514 515 516
	 * @param  string  $name
	 * @param  array   $attributes
	 * @return mixed
	 */
517
	protected static function id($name, $attributes)
518
	{
519 520 521 522
		if (array_key_exists('id', $attributes))
		{
			return $attributes['id'];
		}
523

524 525 526 527
		if (in_array($name, static::$labels))
		{
			return $name;
		}
528 529
	}

530
}