Try to pass variable to the macro, in this case an array of errors maybe.
Form::macro('test', function($var) {
return '<div class="error">' . $var . '</div>';
});
{{ HTML::test($errors) }}
Would it be possible to set up a macro or Helper class that always gets passed the $errors variable?
I've set up a Markup helper class with a facade to avoid repetitively typing in Bootstrap markup for common things. One of its methods is an inputGroup method that generates a Bootstrap input group with an input and label with a consistent name.
The inputGroup also inserts any error messages for the field, but I've only been able to get this working by passing the $errors variable into the method each time its called.
I've been trying without success to pass Session or $errors to the Markup helper in its __construct() function so that it can do this automatically without being passed the $errors variable. Is this possible?
$errors = Session::get('errors', new Illuminate\Support\MessageBag);
I tried that, but the methods on the resulting MessageBag object always return empty arrays. Session::get('errors') seems to always be empty in the view as well?
I can use Session::get('error'), but that seems to only be numerically indexed and doesn't contain which fields the errors apply to.
This is my macro for both validation and user-defined errors.
HTML::macro('errors', function($auto = true, $type = 'danger')
{
$errors = Session::get('errors', new Illuminate\Support\MessageBag);
/*
window.setTimeout(function () {
$("div[data-alert-auto]").fadeTo(500, 0).slideUp(500, function () {
$(this).remove();
});
}, 2000);
*/
$autoCloseAttr = ($auto)?' data-alert-auto':'';
if($errors->has()) {
$alert = '<div class="alert alert-'.$type.'"'.$autoCloseAttr.'><a class="close" data-dismiss="alert" href="#" aria-hidden="true">×</a>';
foreach ($errors->all('<p>:message</p>') as $message) {
$alert .= $message;
}
$alert .= '</div>';
return $alert;
}
});
You beauty!
Had me foxed for hours, then I came across your post.
Thanks for sharing.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community