Stop Writing Your Form Errors by Hand!

Submitted by crhayes - 11 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>
			%s
			%s
		</div>', 
		$errors->has($name) ? 'has-error' : '', 
		$label, 
		$callback($name), 
		$errors->first($name, '<span class="help-block">:message</span>')
	);
});

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