php - Laravel is displaying error message before form submit

345

in my last question i was asking for help with single error message displaying after incorrectly filled form instead of error for every incorrect input. They were wery helpfull and I received this piece of code:

          @if ($errors)
              <span class='help-block'>
                  <strong>{{ "There are errors" }}</strong>
              </span>
          @endif

and the second answer was

 @if (!empty($errors))
 <span class='help-block'>
     <strong>{{ "Some input field is not properly filled" }}</strong>
 </span>
 @endif

I thought that the above will be displayed only after form submiting and if Laravel will find any errors. The problem is that it's always displaying error message like in the screenshot under: enter image description here Does anyone knows the solution? Many Thanks.

239

Answer

Solution:

So if I understood it correctly, you want to show the success or error message only after submitting your form.

If that is the case following should work:

You need to add / change following in your Controller function:

$request->session()->flash('success', 'Success!');
return redirect()->route("yourstuff.index");

In your blade.php you return that session with it message like this

  @if (session('success'))
    <div >
      {{ session('success') }}
    </div>
  @endif

  @if (session('error'))
    <div >
      {{ session('error') }}
    </div>
  @endif
841

Answer

Solution:

The problem is solved, i received two good answers but i choosed simplier one. In order to return single error I used the code proposed by user:

devk

    @if ($errors->count() > 0)
 <span class='help-block'>
     <strong>{{ "Some input field is not properly filled" }}</strong>
 </span>
    @endif

And works great.I want to thank to user:

utdev

For this code:

You need to add / change following in your Controller function:

$request->session()->flash('success', 'Success!');
return redirect()->route("yourstuff.index");

In your blade.php you return that session with it message like this

@if (session('success'))
  <div >
    {{ session('success') }}
  </div>
@endif

@if (session('error'))
  <div >
    {{ session('error') }}
  </div>
@endif

I will use the code proposed by you in other part of my project. THANKS !

People are also looking for solutions to the problem: javascript - Save textarea data from HTML page to a TXT file

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.