Field validation in Lumen 5.5

Asked

Viewed 262 times

0

In Lumen version 5.2 the following validation in the Controller is working:

$this->validate($request, [
        'nome' => 'required',
        'email' => 'required|email',
        'cpf' => 'required',
    ]);

Iofc the following View:

@if (count($errors) > 0) {
    <div>
        <ul>
            @foreach($errors->all() as $error){
                    echo("<li>". $error . "</li>");
                 }
            @endforeach     
        </ul>
    </div>
@endif

It turns out that in version 5.5 of Lumen is not working.

The following error is displayed:1/1) Errorexception Undefined variable: errors in cadasPeople.php (line 8) at Application->Laravel Lumen Concerns{closure}(8, 'Undefined variable: errors', 'C: wamp64 www blog Resources views cadasPessoas.Ph p', 8, array('__path' => 'C: wamp64 www blog Resources views/cadastroPessoas.php ', '__data' => array('__env' => Object(Factory), 'app' => Object(Application)), 'obLevel' => 1, '__env' => Object(Factory), 'app' => Object(Application))) in cadastre('C: wamp64 www blog Resources views cadastroPe ssoas.php

  • Is there an error? Enabled DEBUG?

  • The following error appears:1/1) Errorexception Undefined variable: errors in cadasPeople.php (line 8) at Application->Laravel Lumen Concerns{closure}(8, 'Undefined variable: errors', 'C: wamp64 www blog Resources views cadasPessoas.php', 8, array('__path' => 'C: wamp64 www blog Resources views/cadastroPessoas.php', '__data' => array('__env' => Object(Factory), 'app' => Object(Application)), 'obLevel' => 1, '__env' => Object(Factory), 'app' => Object(Application)) in cadastre('C: wamp64 www blog Resources views cadastre

  • @Guillhermenascimento

  • @Virgilionovic doesn’t see how it can be duplicated, because the error is not with the validation, but rather a syntax error and another reason is that Lumen and Laravel despite sharing the same ecosystem, they have the 'application' layer a little different, aiming that the intention of the Lumen is to be much more light that Laravel, so its settings may vary, such as the configuration of middleware, I believe the author forgot to add Illuminate\View\Middleware\ShareErrorsFromSession (required to make the variable available globally $errors).

  • @Fernandoi.Kobayashi reply edited, see https://answall.com/a/246481/3635, read the part I wrote "Important note about $errors in Laravel/Lumen views"

  • @Guilhermenascimento in the part Laravel and Lumen there are enough controversies regarding performance and functionalities, I will withdraw the vote of duplicate, because, who asked the question has now put the error message of variable not defined.

  • @Virgilionovic I understand, but I just wanted to say that regardless of whether or not I informed the error message, there was no evidence of duplicity, the only indication before the AP informed the error message was that there were possible syntax errors regarding the use of "Blade" (which may or may not cause the failure, summarizing it would be more necessary to vote as "typo"). Thank you ;)

  • 1

    @Guilhermenascimento to me came at first that there was a problem at the time of calling the validation and the associated answer of duplicity had the explanation that apparently he needed. Thank you !

  • @Guilhermenascimento , in relation to note en.stackoverflow.com/a/246481/3635, was like this in version 5.2 of Lumen. I thank you for the help you are giving me,

Show 4 more comments

1 answer

1

Not working is very ambiguous, probably it is occurring 500 Internal server error (if DEBUG is turned off) or this displaying an Exception.

It seems to me it’s a series of typo:

Has a { not required, in "Views" this is not used:

@if (count($errors) > 0) { <--- ISTO
...
    @foreach($errors->all() as $error){ <--- ISTO

and }:

    echo("<li>". $error . "</li>");
 } <--- ISTO

and maybe echo also does not work (nor does it make sense, even if it works, to use echo, after all the rest did not need)

It must be so:

@if (count($errors) > 0)
    <div>
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach     
        </ul>
    </div>
@endif

Always make the examples according to the documentation: https://laravel.com/docs/5.5/validation#Quick-Displaying-the-validation-errors

Note: I don’t know if $errors works with count(), but in the example is with ->any(), so if the script still fails, switch to

@if ($errors->any())
    <div>
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach     
        </ul>
    </div>
@endif

Important note about $errors in Laravel/Lumen views

Why $errors is available it is necessary to add to middleware the Illuminate\View\Middleware\ShareErrorsFromSession, then go to bootstrap/app.php and add this:

$app->middleware([
   Illuminate\View\Middleware\ShareErrorsFromSession::class
]);

So the $errors will be "global", if not added Lumen will trigger the error:

Errorexception Undefined variable: errors

  • Now the following error appears: Runtimeexception Session store not set on request. in Request.php (line 414) at Request->Session() in Shareerrorsfromsession.php (line 42) at Shareerrorsfromsession->Handle(Object(Request), Object(Closure)) in Pipeline.php (line 149) at Pipeline->Illuminate Pipeline closure{}(Object(Request)) at call_user_func(Object(Closure), Object(Request)) in Pipeline.php (line 32) at Pipeline->Laravel Lumen Routing{closure}(Object(Request)) in Pipeline.php (line 102)

  • @Fernandoi.Kobayashi will tetar here as soon as possible I warn you

  • In version 5.2, I added: Illuminate Session Middleware Startsession::class, now the error appears: Bindingresolutionexception Unresolvable dependency resolving [Parameter #0 [ <required> $app ]] in class Illuminate Support Manager in Container.php (line 933) at Container->unresolvablePrimitive(Object(Reflectionparameter)) in Container.php (line 871) at Container->resolvePrimitive(Object(Reflectionparameter)) in Container.php (line 812) @Guilhermenascimento

Browser other questions tagged

You are not signed in. Login or sign up in order to post.