Stop Writing Your Form Errors by Hand!

Submitted by crhayes - 9 years ago

This trick is handy for wrapping all of your form elements with the same HTML/classes, which is especially useful for handling error states when using frameworks like Bootstrap or Foundation.

$errors = Session::get('errors', new Illuminate\Support\MessageBag);

Form::macro('bootwrapped', function($name, $label, $callback) use ($errors)
	return sprintf(
		'<div class="form-group %s">
			<label class="control-label">%s</label>
		$errors->has($name) ? 'has-error' : '', 
		$errors->first($name, '<span class="help-block">:message</span>')

Form::bootwrapped('email', 'Email', function($name)
	return Form::text($name, null, ['class' => 'form-control']);