I want to validate the route parameters in the "form request" but don't know how to do it.
Below is the code sample, I am trying with:
Route
// controller Server Route::group(['prefix' => 'server'], function(){ Route::get('checkToken/{token}',['as'=>'checkKey','uses'=> 'ServerController@checkToken']); });
Controller
namespace App\Http\Controllers; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use App\Http\Requests; class ServerController extends Controller { public function checkToken( \App\Http\Requests\CheckTokenServerRequest $request) // OT: - why I have to set full path to work?? { $token = Token::where('token', '=', $request->token)->first(); $dt = new DateTime; $token->executed_at = $dt->format('m-d-y H:i:s'); $token->save(); return response()->json(json_decode($token->json),200); } }
CheckTokenServerRequest
namespace App\Http\Requests; use App\Http\Requests\Request; class CheckTokenServerRequest extends Request { //autorization /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'token' => ['required','exists:Tokens,token,executed_at,null'] ]; } }
But when I try to validate a simple url http://myurl/server/checkToken/222, I am getting the response: no " token " parameter set
.
Is it possible to validate the parameters in a separate "Form request", Or I have to do all in a controller?
ps. Sorry for my bad English.
Laravel routes are located in the app/Http/routes.A parameter provided in the route is usually annotated with curly braces. For instance, to pass in a name parameter to a route, it would look like this. By convention, the Controller function accepts parameters based on the parameters provided.
By default, Laravel 'confirmed' validator adds the error message to the original field and not to the field which usually contains the confirmed value.
Validation Error Response Format When your application throws a Illuminate\Validation\ValidationException exception and the incoming HTTP request is expecting a JSON response, Laravel will automatically format the error messages for you and return a 422 Unprocessable Entity HTTP response.
For Laravel < 5.5:
The way for this is overriding all()
method for CheckTokenServerRequest
like so:
public function all() { $data = parent::all(); $data['token'] = $this->route('token'); return $data; }
EDIT
For Laravel >= 5.5:
Above solution works in Laravel < 5.5. If you want to use it in Laravel 5.5 or above, you should use:
public function all($keys = null) { $data = parent::all($keys); $data['token'] = $this->route('token'); return $data; }
instead.
Override the all()
function on the Request object to automatically apply validation rules to the URL parameters
class SetEmailRequest { public function rules() { return [ 'email' => 'required|email|max:40', 'id' => 'required|integer', // << url parameter ]; } public function all() { $data = parent::all(); $data['id'] = $this->route('id'); return $data; } public function authorize() { return true; } }
Access the data normally from the controller like this, after injecting the request:
$setEmailRequest->email // request data $setEmailRequest->id, // url data
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With