laravel pass validation error to custom view - laravel

I created a validation but cant show it on view, its important to return search view and don't redirect back user. help me please, thanks all?
Controller :
public function search(Request $request)
{
$msg = Validator::make($request->all(), [
'search' => 'required'
]);
if ($msg->fails()) {
return view('layouts.search')->withErrors($msg->messages());
} else {
return "Thank you!";
}
}
View :
#if($errors->any())
<ul class="alert alert-danger">
#foreach($errors as $error)
<li> {{$error}} </li>
#endforeach
</ul>
#else

You can use $error->first('name_of_error_field') to show error messages.
You can do it like this:
public function search(Request $request)
{
$validator = Validator::make($request->all(), [
'search' => 'required'
]);
if ($validator->fails()) {
return view('layouts.search')->withErrors($validator); // <----- Send the validator here
} else {
return "Thank you!";
}
}
And in view:
#if($errors->any())
<ul class="alert alert-danger">
#foreach($errors as $error)
<li> {{$error->first('name_of_error_field')}} </li>
#endforeach
</ul>
#endif
See more about Laravel Custom Validators
Hope this helps

Related

laravel validation does not work with Auth [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am trying to add validation in the controller but it does not work with auth:check. This is the Laravel version 5.7.
This is the function store that has a problem with the validation.
public function store(Request $request)
{
$request->validate([
'first_name' => ['required'|'min:2'|'max:50']
]);
if(Auth::check()){
$player = Player::create([
'first_name' => $request->input('fist_name'),
'last_name' => $request->input('last_name'),
]);
if($player){
return redirect()->route('players.show', ['player'=> $player->id])
->with('success' , 'foo');
}
}
return back()->withInput()->with('errors', 'Foo Error');
}
This is the error message:
[![enter image description here][2]][2]
Update
The validation works now but it generated a new problem. When I save a new player it's shows me this error.
I think the problem is this code
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ $player->first_name }} {{ $player->last_name }} <span class="float-right"><a class="" href="{{ route('players.edit', $player->id)}}">
{{ __('Spieler bearbeiten') }}
</a></span></div>
<div class="card-body">
#if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
#endif
<ul>
<li>Vorname: {{ $player->first_name }}</li>
<li>Nachname: {{ $player->last_name }}</li>
<li>Land: {{ $player->country }}</li>
<li>Bild: {{ $player->image }}</li>
<li>Grösse: {{ $player->size }}</li>
<li>Gewicht: {{ $player->weight }}</li>
<li>Alter: {{ $player->date_of_birth }}</li>
</ul>
<br>
<br>
Zurück
</div>
</div>
</div>
</div>
</div>
#endsection
Most of your code is unnecessary, you can protect the route and get rid of most of it:
public function store(Request $request)
{
// Your validation is not correct and you are missing the last_name.
// $request->validate() will return the validated data if
// the validation was successful
$request->validate([
'first_name' => ['required' | 'min:2' | 'max:50']
]);
// After protecting the route we can get rid of the auth check
if (Auth::check()) {
$player = Player::create([
// Since $request->validate returns the validated data, this is unncessary
'first_name' => $request->input('fist_name'),
'last_name' => $request->input('last_name'),
]);
// It will either create the player or throw an exception,
// this conditional seems unnecessary
if ($player) {
return redirect()
// If you setup your routes correctly you can pass the model to the route
// so ['player' => $player->id] is unnecessary
->route('players.show', ['player' => $player->id])
->with('success', 'foo');
}
}
// This return seems pointless since it can never be reached
// after protecting the route
return back()->withInput()->with('errors', 'Foo Error');
}
So it would look something like this:
public function __construct()
{
$this->middleware('auth');
}
/**
* #param Request $request
*
* #return RedirectResponse
*/
public function store(Request $request)
{
$validatedData = $request->validate([
'first_name' => 'required|min:2|max:50',
'last_name' => 'required|min:2|max:50',
]);
$player = Player::create($validatedData);
return redirect()
->route('players.show', $player)
->with('success', 'foo');
}

Laravel 5.8: Validation multiple inputs

What I have
I have a form with 3 inputs and I want to check the following conditions:
All inputs are integers and they are required.
We do a math operation with all the numbers and we get if the operation was successfull or not.
Success: we redirect the user to a success page.
No success: we show a error message to the user with a message explaining him that the numbers aren't valid.
I resolved this with the following lines.
Controller:
function formAction(Request $request) {
$this->validate($request, [
'number1' => 'integer|required',
'number2' => 'integer|required',
'number3' => 'integer|required',
]);
$numbers = $request->all();
$isValid = MyOwnClass::checkMathOperation($numbers);
if($isValid) {
return redirect()->route('success');
} else {
$request->session()->flash('error', 'The numbers are not valid.');
return back();
}
}
View (using Bootstrap):
<form method="POST" action="{{ route('form-action') }}">
#csrf
<div class="form-group">
<label for="number1">number1</label>
<input id="number1" name="number1" class="form-control {{ $errors->has('number1') ? ' is-invalid' : '' }}" />
</div>
<div class="form-group">
<label for="number2">number2</label>
<input id="number2" name="number2" class="form-control {{ $errors->has('number2') ? ' is-invalid' : '' }}" />
</div>
<div class="form-group">
<label for="number3">number3</label>
<input id="number3" name="number3" class="form-control {{ $errors->has('number3') ? ' is-invalid' : '' }}" />
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
What I looking for
When MyOwnClass::checkMathOperation($numbers) is false:
To highlight number1, number2 and number3 inputs.
To show an unique custom error message
To hide the number1, number2 and number3 input error messages.
How can I do that with validators?
Solution
Create a Form Request Validation called, for example, NumbersForm using:
php artisan make:request NumbersForm
The previous command creates a App/Http/Requests/NumbersForm.php file. Make authorize() returns true, put the validation rules into rules() and create a withValidatior() function.
class NumbersForm extends FormRequest
{
public function authorize() {
return true;
}
public function rules() {
return [
'number1' => 'integer|required',
'number2' => 'integer|required',
'number3' => 'integer|required',
];
}
public function withValidator($validator) {
$validator->after(function ($validator) {
$numbers = $this->except('_token'); // Get all inputs except '_token'
$isValid = MyOwnClass::checkMathOperation($numbers);
if(!$isValid) {
$validator->errors()->add('number1', ' ');
$validator->errors()->add('number2', ' ');
$validator->errors()->add('number3', ' ');
$validator->errors()->add('globalError', 'The numbers are not valid.');
}
});
}
}
Note: It's not important the text in the second param of $validator->errors()->add('number1', ' ');, but it can't be empty. If it is an empty string, $errors->has('number1') returns false, and the field won't be hightlighted.
Set the controller like this:
use App\Http\Requests\NumbersForm;
function formAction(NumbersForm $request) {
$this->validated();
return redirect()->route('success');
}
And, finally, if we want to print an unique error message, we must remove the following lines from view:
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
and replace them with:
#if ($errors->has('globalError'))
<div class="alert alert-danger">
{{ $errors->first('globalError') }}
</div>
#else
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
#endif
I haven't tested this but I think it can get you going in the right direction.
1 // Highlight the inputs
You can do this by accessing the error object within your view. This object is an instance of the MessageBag object.
Here is the docs: https://laravel.com/docs/5.7/validation#working-with-error-messages
Example:
// if the error exists for the input the class will be added
<input class=" {{ $error->has('number1') ? 'highlight' : '' }}" name="number1">
<input class=" {{ $error->has('number2') ? 'highlight' : '' }}" name="number2">
<input class=" {{ $error->has('number3') ? 'highlight' : '' }}" name="number3">
2 & 3 // Show a unique custom error message and hide the default messages
See the validator docs: https://laravel.com/docs/5.8/validation#custom-error-messages && https://laravel.com/docs/5.7/validation#working-with-error-messages -- this should solve both of these.
There is a validator callback and I think you can pass your second validation into that. If these numbers aren't valid then you can add your custom error messages and access them the same way as I did above.
function formAction(Request $request) {
$validator = $this->validate($request, [
'number1' => 'integer|required',
'number2' => 'integer|required',
'number3' => 'integer|required',
]);
$validator->after(function ($validator) {
$numbers = $request->all();
$isValid = MyOwnClass::checkMathOperation($numbers);
if(!$isValid) {
$validator->errors()->add('number1', 'Unique message');
$validator->errors()->add('number2', 'Unique message');
$validator->errors()->add('number3', 'Unique message');
}
});
}
Custom Validation Rules:
To add custom messages and validation you can also write a custom validation rule
Example:
class Uppercase implements Rule
{
/**
* Determine if the validation rule passes.
*
* #param string $attribute
* #param mixed $value
* #return bool
*/
public function passes($attribute, $value)
{
return strtoupper($value) === $value;
}
/**
* Get the validation error message.
*
* #return string
*/
public function message()
{
return 'The :attribute must be uppercase.';
}
}
Custom Error Messages:
You could also add custom error messages for rules within a Request:
public function messages()
{
return [
'number1.required' => 'My custom message telling the user he needs to fill in the number1 field.',
'number1.integer' => 'My custom message telling the user he needs to use an integer.',
];
}

Laravel : On 5.5 validation message not showing on view.blade

Validation not showing any message on view page. I just can't figure out.
My controller code
public function save(Request $request) {
try {
$file = $request->file('flag_image');
$this->validate($request, Country::rules()); // checking validation
/*Image Upload code*/
If(Input::hasFile('flag_image')){
$file = Input::file('flag_image');
$destinationPath = public_path(). '/images/admin/country/';
$filename = $file->getClientOriginalName();
$image = time().$filename;
$file->move($destinationPath, $image);
$imgpath = 'images/admin/country/'.$image;
}
if($file !="") {
$request->merge(['flag_image' => $imgpath]);
}
/*Image Upload code end*/
$country = Country::saveOrUpdate($request);
if($file !="") {
$country->flag_image = $imgpath;
$country->save();
}
if($country !== false) {
return redirect()->route('lists-country')->with('success', trans('Country data added successfully.!!'));
} else {
return back()->with('error', "Unable to save country data.!!")->withInput();
}
} catch (\Exception $ex) {
return back()->with('error', "Unable to save country data.!!")->withInput();
}
}
Here is my model code for validation check :
public static function rules() {
return [
'title' => 'required|string|max:255',
'short_name' => 'required',
'status' => 'required|string|in:' . implode(",", Country::STATUSES)
];
}
On view page didn' get any validation message : On my view
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div> #endif
Also added in a app/exceptions/handler.php file code
protected $dontReport = [
\Illuminate\Auth\AuthenticationException::class,
\Illuminate\Auth\Access\AuthorizationException::class,
\Symfony\Component\HttpKernel\Exception\HttpException::class,
\Illuminate\Database\Eloquent\ModelNotFoundException::class,
\Illuminate\Session\TokenMismatchException::class,
\Illuminate\Validation\ValidationException::class,
];
Not working at all.what am i missing ?
Try using -
#if (session()->has('error'))
<div class="alert alert-danger">
<ul>
<li>{{ session('error') }}</li>
</ul>
</div>
#endif
In this way you can access only the key, that has been set by with methods
Change this line:
$this->validate($request, Country::rules());
To:
$request->validate(Country::rules());

Laravel validation not working after submit

This did not show the errors after submit
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Cars;
use App\Images;
use DB;
$this->validate($request,[
'name'=>'required|min:3',
'specifications'=>'required|min:10'
]);
Inserting values into the database.I want to validate on submitting the form
public function store(Request $request)
{
//
$this->validate($request->all(),[
'name'=>'required|min:3',
'specifications'=>'required|min:10'
]);
$cars = New Cars;
$cars->name = $request->name;
$cars->specifications = $request->specifications;
$cars->price = $request->price;
$cars->model_id = $request->model_id;
$cars->year = $request->year;
$cars->milage = $request->milage;
if($cars->save()) {
$id = DB::getPdo()->lastInsertId();
}
return redirect('home');
}
I displayed errors like this, but not working for me
#if(count($errors) > 0)
<div class="alert alert-danger">
#foreach($errors->all() as $error)
<p>{{ $error }}</p>
#endforeach
</div>
#endif
Add this piece of code on your controller method.
return back()->withErrors()->withInput()
you can also access old input value with old(field_name)
Use ->all() here to provide all input value in to validate
$this->validate($request->all(),[
Or you can try like this
public function store(Request $request)
{
$validatedData = $request->validate([
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
// The blog post is valid...
}
And display error as
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
first, edit your method like this :
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Cars;
use App\Images;
use DB;
use Validator;
and inside this method please edit your redirect route and make sure your redirect url, redirect you in the page that had the error message:
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'name'=>'required|min:3',
'specifications'=>'required|min:10'
]);
if ($validator->fails()) {
return redirect(route('make your route here '))
->withErrors($validator)
->withInput();
}
$cars = New Cars;
$cars->name = $request->name;
$cars->specifications = $request->specifications;
$cars->price = $request->price;
$cars->model_id = $request->model_id;
$cars->year = $request->year;
$cars->milage = $request->milage;
if($cars->save()) {
$id = DB::getPdo()->lastInsertId();
}
return redirect('home');
}
and for displaying error messages inside your blade template make this :
#if(!empty($errors))
#if($errors->any())
<div class="alert alert-danger">
<ul>
#foreach($errors->all() as $error)
<li>{!! $error !!}</li>
#endforeach
</ul>
</div>
#endif
#endif

Laravel 5.2.26 get form validation data array

I submit the form and have some validation mean email,require,unique email,
when validation have error message then laravel 5.2 return validation return array.
I guess you want to retain the submitted data on error.
Considering a sample
public function postJobs(Request $request) {
$input = $request->all();
$messages = [
'job_title.required' => trans('job.title_required'),
];
$validator = Validator::make($request->all(), [
'job_title' => 'required'
], $messages);
if ($validator->fails()) { // redirect if validation fails, note the ->withErrors($validator)
return redirect()
->route('your.route')
->withErrors($validator)
->withInput();
}
// Do other stuff if no error
}
And, in the view you can handle errors like this:
<div class="<?php if (count($errors) > 0) { echo 'alert alert-danger'; } ?>" >
<ul>
#if (count($errors) > 0)
#foreach ($errors->all() as $error)
<li>{!! $error !!}</li>
#endforeach
#endif
</ul>
</div>
And if you want the input data, you need to redirect with ->withInput(); which can be fetch in view like:
Update
<input name= "job_title" value="{{ Request::old('job_title') }}" />
But, the best thing is to use laravel Form package so they all are handled automatically.
If you just need form data, you can use Request object:
public function store(Request $request)
{
$name = $request->input('firstName');
}
https://laravel.com/docs/5.1/requests#accessing-the-request
Alternatively, you could use $request->get('firstName');
Use old() to get previous value from input. Example:
<input name="firstName" value="{{old('firstName')}}">
See documentation here https://laravel.com/docs/5.1/requests#old-input

Resources