Reputation: 141
I try to validate input, but when input incorrect not show error and redirect to another url.My code is something like this:
$rules = [
'point' => 'required|numeric|min:0|max:10',
'id_tieuchi' => 'required|numeric|min:1'
];
and
foreach ($data as $value)
{
$validator = Validator::make( $value, $rules);
}
I also using syntax into foreach but not working because controller not understand what's user
in base url ../evaluate/$user/edit
:
if ($validator->fails())
return redirect('evaluate/{user}/edit')->withInput()->with('message',$validator->messages()->all());
Upvotes: 1
Views: 282
Reputation: 9873
Laravel comes with Requests that will handle this logic for you. You can leverage them like so:
In App\Http\Requests, create a FooRequest.php and add this code to it:
<?php
namespace App\Http\Requests;
class FooRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'point' => 'required|numeric|min:0|max:10',
'id_tieuchi' => 'required|numeric|min:1'
];
}
}
And in your FooController.php, be sure to add this to the top of the file:
use App\Http\Requests\FooRequest;
Then in your controller method, use the FooRequest as such:
public function bar(FooRequest $request)
{
// Do stuff here...
}
The FooRequest will validate whatever input you send to the bar method. If validation errors exist, it will automatically redirect back to whatever page you came from with the errors in the validator. If validation passes, it will enter the bar
method with the items in the request in $request.
For more information, read about validation in Laravel.
Upvotes: 2