Laravel 5.5 Validation errors are not displaying in case form validation fails
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
add a comment |
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
What if you trydd($errors)
instead ofprint_r($errors->all());
?
– cbaconnier
Nov 20 '18 at 8:20
add a comment |
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
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
laravel laravel-5.5
asked Nov 20 '18 at 7:56
aishazafaraishazafar
3222521
3222521
What if you trydd($errors)
instead ofprint_r($errors->all());
?
– cbaconnier
Nov 20 '18 at 8:20
add a comment |
What if you trydd($errors)
instead ofprint_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
add a comment |
3 Answers
3
active
oldest
votes
used this
@if($errors->has())
@foreach ($errors->all() as $error)
<div>{{ $error }}</div>
@endforeach
@endif
add a comment |
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);
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
|
show 1 more comment
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',
...
];
There is no issue in messages. messages can be set like this too.
– aishazafar
Nov 22 '18 at 6:27
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
used this
@if($errors->has())
@foreach ($errors->all() as $error)
<div>{{ $error }}</div>
@endforeach
@endif
add a comment |
used this
@if($errors->has())
@foreach ($errors->all() as $error)
<div>{{ $error }}</div>
@endforeach
@endif
add a comment |
used this
@if($errors->has())
@foreach ($errors->all() as $error)
<div>{{ $error }}</div>
@endforeach
@endif
used this
@if($errors->has())
@foreach ($errors->all() as $error)
<div>{{ $error }}</div>
@endforeach
@endif
answered Nov 20 '18 at 8:00
Usman JdnUsman Jdn
426115
426115
add a comment |
add a comment |
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);
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
|
show 1 more comment
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);
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
|
show 1 more comment
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);
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);
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
|
show 1 more comment
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
|
show 1 more comment
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',
...
];
There is no issue in messages. messages can be set like this too.
– aishazafar
Nov 22 '18 at 6:27
add a comment |
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',
...
];
There is no issue in messages. messages can be set like this too.
– aishazafar
Nov 22 '18 at 6:27
add a comment |
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',
...
];
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',
...
];
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
What if you try
dd($errors)
instead ofprint_r($errors->all());
?– cbaconnier
Nov 20 '18 at 8:20