Laravel 5.5 Validation errors are not displaying in case form validation fails












0















I am trying to display form errors in case form validation fails. Everything works fine and form is validated correctly but it does not display form errors in view. Every time an empty array is returned as errors.






namespace AppHttpControllers;

use IlluminateHttpRequest;
use IlluminateSupportFacadesAuth;

class HomeController extends Controller
{
public function storeProjectDetails(Request $request)
{
$messages = [
'title.required' => 'Please enter trip title',
'title.max' => 'Only 254 characters are allowed as trip title',
'startDate.required' => 'Please enter trip start date',
'startDate.date' => 'Only date formats are allowed as start date',
'endDate.required' => 'Please enter trip end date',
'endDate.date' => 'Only date formats are allowed as end date',
];

$this->validate($request,[
'title' => 'required|string|max:254',
'startDate' => 'required|date',
'endDate' => 'required|date',
]);

}
}


View:



  print_r($errors->all());









share|improve this question























  • What if you try dd($errors) instead of print_r($errors->all()); ?

    – cbaconnier
    Nov 20 '18 at 8:20
















0















I am trying to display form errors in case form validation fails. Everything works fine and form is validated correctly but it does not display form errors in view. Every time an empty array is returned as errors.






namespace AppHttpControllers;

use IlluminateHttpRequest;
use IlluminateSupportFacadesAuth;

class HomeController extends Controller
{
public function storeProjectDetails(Request $request)
{
$messages = [
'title.required' => 'Please enter trip title',
'title.max' => 'Only 254 characters are allowed as trip title',
'startDate.required' => 'Please enter trip start date',
'startDate.date' => 'Only date formats are allowed as start date',
'endDate.required' => 'Please enter trip end date',
'endDate.date' => 'Only date formats are allowed as end date',
];

$this->validate($request,[
'title' => 'required|string|max:254',
'startDate' => 'required|date',
'endDate' => 'required|date',
]);

}
}


View:



  print_r($errors->all());









share|improve this question























  • What if you try dd($errors) instead of print_r($errors->all()); ?

    – cbaconnier
    Nov 20 '18 at 8:20














0












0








0








I am trying to display form errors in case form validation fails. Everything works fine and form is validated correctly but it does not display form errors in view. Every time an empty array is returned as errors.






namespace AppHttpControllers;

use IlluminateHttpRequest;
use IlluminateSupportFacadesAuth;

class HomeController extends Controller
{
public function storeProjectDetails(Request $request)
{
$messages = [
'title.required' => 'Please enter trip title',
'title.max' => 'Only 254 characters are allowed as trip title',
'startDate.required' => 'Please enter trip start date',
'startDate.date' => 'Only date formats are allowed as start date',
'endDate.required' => 'Please enter trip end date',
'endDate.date' => 'Only date formats are allowed as end date',
];

$this->validate($request,[
'title' => 'required|string|max:254',
'startDate' => 'required|date',
'endDate' => 'required|date',
]);

}
}


View:



  print_r($errors->all());









share|improve this question














I am trying to display form errors in case form validation fails. Everything works fine and form is validated correctly but it does not display form errors in view. Every time an empty array is returned as errors.






namespace AppHttpControllers;

use IlluminateHttpRequest;
use IlluminateSupportFacadesAuth;

class HomeController extends Controller
{
public function storeProjectDetails(Request $request)
{
$messages = [
'title.required' => 'Please enter trip title',
'title.max' => 'Only 254 characters are allowed as trip title',
'startDate.required' => 'Please enter trip start date',
'startDate.date' => 'Only date formats are allowed as start date',
'endDate.required' => 'Please enter trip end date',
'endDate.date' => 'Only date formats are allowed as end date',
];

$this->validate($request,[
'title' => 'required|string|max:254',
'startDate' => 'required|date',
'endDate' => 'required|date',
]);

}
}


View:



  print_r($errors->all());






laravel laravel-5.5






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 7:56









aishazafaraishazafar

3222521




3222521













  • What if you try dd($errors) instead of print_r($errors->all()); ?

    – cbaconnier
    Nov 20 '18 at 8:20



















  • What if you try dd($errors) instead of print_r($errors->all()); ?

    – cbaconnier
    Nov 20 '18 at 8:20

















What if you try dd($errors) instead of print_r($errors->all()); ?

– cbaconnier
Nov 20 '18 at 8:20





What if you try dd($errors) instead of print_r($errors->all()); ?

– cbaconnier
Nov 20 '18 at 8:20












3 Answers
3






active

oldest

votes


















0














used this



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





share|improve this answer































    0














    First you need to check for errors using an if condition then you need to print the errors using a loop as given below



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


    In your case, you are missing to return the errors array in your controller. Try the validation check given below.



    $this->validate($request,[
    'title' => 'required|string|max:254',
    'startDate' => 'required|date',
    'endDate' => 'required|date',
    ], $messages);





    share|improve this answer
























    • If we don't return messages with validate function it displays default messages for required and other rules. So this is not the issue. There is something else due to that errors are not being displayed.

      – aishazafar
      Nov 20 '18 at 8:11











    • did you try displaying errors as shown in my answer?

      – Bonish Koirala
      Nov 20 '18 at 8:13











    • yes. That's only a way to display errors. Which can be done with print_r too.

      – aishazafar
      Nov 20 '18 at 8:16













    • How did you know that its validating correctly yet you didn't able to see the error?

      – Jesus Erwin Suarez
      Nov 20 '18 at 8:23











    • @JesusErwinSuarez after your question I modified the code and checked again thinking there may be my mistake. Even only with required rule it does not display any error.

      – aishazafar
      Nov 20 '18 at 8:48



















    0














    You are building the messages array in a wrong format, it should look like this:



    $messages = [
    'required' => 'Please enter :attribute',
    'date' => 'Only date formats are allowed as :attribute',
    ...
    ];





    share|improve this answer
























    • There is no issue in messages. messages can be set like this too.

      – aishazafar
      Nov 22 '18 at 6:27











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388506%2flaravel-5-5-validation-errors-are-not-displaying-in-case-form-validation-fails%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    used this



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





    share|improve this answer




























      0














      used this



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





      share|improve this answer


























        0












        0








        0







        used this



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





        share|improve this answer













        used this



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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 8:00









        Usman JdnUsman Jdn

        426115




        426115

























            0














            First you need to check for errors using an if condition then you need to print the errors using a loop as given below



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


            In your case, you are missing to return the errors array in your controller. Try the validation check given below.



            $this->validate($request,[
            'title' => 'required|string|max:254',
            'startDate' => 'required|date',
            'endDate' => 'required|date',
            ], $messages);





            share|improve this answer
























            • If we don't return messages with validate function it displays default messages for required and other rules. So this is not the issue. There is something else due to that errors are not being displayed.

              – aishazafar
              Nov 20 '18 at 8:11











            • did you try displaying errors as shown in my answer?

              – Bonish Koirala
              Nov 20 '18 at 8:13











            • yes. That's only a way to display errors. Which can be done with print_r too.

              – aishazafar
              Nov 20 '18 at 8:16













            • How did you know that its validating correctly yet you didn't able to see the error?

              – Jesus Erwin Suarez
              Nov 20 '18 at 8:23











            • @JesusErwinSuarez after your question I modified the code and checked again thinking there may be my mistake. Even only with required rule it does not display any error.

              – aishazafar
              Nov 20 '18 at 8:48
















            0














            First you need to check for errors using an if condition then you need to print the errors using a loop as given below



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


            In your case, you are missing to return the errors array in your controller. Try the validation check given below.



            $this->validate($request,[
            'title' => 'required|string|max:254',
            'startDate' => 'required|date',
            'endDate' => 'required|date',
            ], $messages);





            share|improve this answer
























            • If we don't return messages with validate function it displays default messages for required and other rules. So this is not the issue. There is something else due to that errors are not being displayed.

              – aishazafar
              Nov 20 '18 at 8:11











            • did you try displaying errors as shown in my answer?

              – Bonish Koirala
              Nov 20 '18 at 8:13











            • yes. That's only a way to display errors. Which can be done with print_r too.

              – aishazafar
              Nov 20 '18 at 8:16













            • How did you know that its validating correctly yet you didn't able to see the error?

              – Jesus Erwin Suarez
              Nov 20 '18 at 8:23











            • @JesusErwinSuarez after your question I modified the code and checked again thinking there may be my mistake. Even only with required rule it does not display any error.

              – aishazafar
              Nov 20 '18 at 8:48














            0












            0








            0







            First you need to check for errors using an if condition then you need to print the errors using a loop as given below



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


            In your case, you are missing to return the errors array in your controller. Try the validation check given below.



            $this->validate($request,[
            'title' => 'required|string|max:254',
            'startDate' => 'required|date',
            'endDate' => 'required|date',
            ], $messages);





            share|improve this answer













            First you need to check for errors using an if condition then you need to print the errors using a loop as given below



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


            In your case, you are missing to return the errors array in your controller. Try the validation check given below.



            $this->validate($request,[
            'title' => 'required|string|max:254',
            'startDate' => 'required|date',
            'endDate' => 'required|date',
            ], $messages);






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 20 '18 at 8:02









            Bonish KoiralaBonish Koirala

            4588




            4588













            • If we don't return messages with validate function it displays default messages for required and other rules. So this is not the issue. There is something else due to that errors are not being displayed.

              – aishazafar
              Nov 20 '18 at 8:11











            • did you try displaying errors as shown in my answer?

              – Bonish Koirala
              Nov 20 '18 at 8:13











            • yes. That's only a way to display errors. Which can be done with print_r too.

              – aishazafar
              Nov 20 '18 at 8:16













            • How did you know that its validating correctly yet you didn't able to see the error?

              – Jesus Erwin Suarez
              Nov 20 '18 at 8:23











            • @JesusErwinSuarez after your question I modified the code and checked again thinking there may be my mistake. Even only with required rule it does not display any error.

              – aishazafar
              Nov 20 '18 at 8:48



















            • If we don't return messages with validate function it displays default messages for required and other rules. So this is not the issue. There is something else due to that errors are not being displayed.

              – aishazafar
              Nov 20 '18 at 8:11











            • did you try displaying errors as shown in my answer?

              – Bonish Koirala
              Nov 20 '18 at 8:13











            • yes. That's only a way to display errors. Which can be done with print_r too.

              – aishazafar
              Nov 20 '18 at 8:16













            • How did you know that its validating correctly yet you didn't able to see the error?

              – Jesus Erwin Suarez
              Nov 20 '18 at 8:23











            • @JesusErwinSuarez after your question I modified the code and checked again thinking there may be my mistake. Even only with required rule it does not display any error.

              – aishazafar
              Nov 20 '18 at 8:48

















            If we don't return messages with validate function it displays default messages for required and other rules. So this is not the issue. There is something else due to that errors are not being displayed.

            – aishazafar
            Nov 20 '18 at 8:11





            If we don't return messages with validate function it displays default messages for required and other rules. So this is not the issue. There is something else due to that errors are not being displayed.

            – aishazafar
            Nov 20 '18 at 8:11













            did you try displaying errors as shown in my answer?

            – Bonish Koirala
            Nov 20 '18 at 8:13





            did you try displaying errors as shown in my answer?

            – Bonish Koirala
            Nov 20 '18 at 8:13













            yes. That's only a way to display errors. Which can be done with print_r too.

            – aishazafar
            Nov 20 '18 at 8:16







            yes. That's only a way to display errors. Which can be done with print_r too.

            – aishazafar
            Nov 20 '18 at 8:16















            How did you know that its validating correctly yet you didn't able to see the error?

            – Jesus Erwin Suarez
            Nov 20 '18 at 8:23





            How did you know that its validating correctly yet you didn't able to see the error?

            – Jesus Erwin Suarez
            Nov 20 '18 at 8:23













            @JesusErwinSuarez after your question I modified the code and checked again thinking there may be my mistake. Even only with required rule it does not display any error.

            – aishazafar
            Nov 20 '18 at 8:48





            @JesusErwinSuarez after your question I modified the code and checked again thinking there may be my mistake. Even only with required rule it does not display any error.

            – aishazafar
            Nov 20 '18 at 8:48











            0














            You are building the messages array in a wrong format, it should look like this:



            $messages = [
            'required' => 'Please enter :attribute',
            'date' => 'Only date formats are allowed as :attribute',
            ...
            ];





            share|improve this answer
























            • There is no issue in messages. messages can be set like this too.

              – aishazafar
              Nov 22 '18 at 6:27
















            0














            You are building the messages array in a wrong format, it should look like this:



            $messages = [
            'required' => 'Please enter :attribute',
            'date' => 'Only date formats are allowed as :attribute',
            ...
            ];





            share|improve this answer
























            • There is no issue in messages. messages can be set like this too.

              – aishazafar
              Nov 22 '18 at 6:27














            0












            0








            0







            You are building the messages array in a wrong format, it should look like this:



            $messages = [
            'required' => 'Please enter :attribute',
            'date' => 'Only date formats are allowed as :attribute',
            ...
            ];





            share|improve this answer













            You are building the messages array in a wrong format, it should look like this:



            $messages = [
            'required' => 'Please enter :attribute',
            'date' => 'Only date formats are allowed as :attribute',
            ...
            ];






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 21 '18 at 21:22









            Arthur SamarcosArthur Samarcos

            2,4001420




            2,4001420













            • There is no issue in messages. messages can be set like this too.

              – aishazafar
              Nov 22 '18 at 6:27



















            • There is no issue in messages. messages can be set like this too.

              – aishazafar
              Nov 22 '18 at 6:27

















            There is no issue in messages. messages can be set like this too.

            – aishazafar
            Nov 22 '18 at 6:27





            There is no issue in messages. messages can be set like this too.

            – aishazafar
            Nov 22 '18 at 6:27


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388506%2flaravel-5-5-validation-errors-are-not-displaying-in-case-form-validation-fails%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?