Support the ongoing development of Laravel.io →
Session Forms Validation
Last updated 1 year ago.
0

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) }}
Last updated 1 year ago.
0

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?

Last updated 1 year ago.
0
$errors = Session::get('errors', new Illuminate\Support\MessageBag);
Last updated 1 year ago.
0

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.

Last updated 1 year ago.
0

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;
    }
});
Last updated 1 year ago.
0

You beauty!

Had me foxed for hours, then I came across your post.

Thanks for sharing.

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Jakobud jakobud Joined 31 Jan 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.