The form request validators are used for w3coded route code validating HTML form data that are sent to w3coded route code server via POST method. It is better that you do w3coded route code not use them for validating route parameters. w3coded route code route parameters are mostly used for retrieving w3coded route code data from data base so in order to ensure that w3coded route code your token route parameter is correct change w3coded route code this line of your code, from,FormRequest has a w3coded route code method validationData() that defines what data w3coded route code to use for validation. So just override that one w3coded route code with route parameters in your form request w3coded route code class:, w3coded route code 1 w3coded route code I agree that this w3coded route code is the only one solution for this problem, if w3coded route code You want to stick to validation inside request w3coded route code class. Small improvement would be to replace all w3coded route code route parameters automatically: w3coded route code pastebin.com/pjwrPme6. w3coded route code – Giedrius Kiršys w3coded route code Jun 1 '16 at 4:53 w3coded route code ,Is it possible to validate the w3coded route code parameters in a separate "Form request", Or I w3coded route code have to do all in a controller?
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;
}
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
Laravel < 5.5:
public function all()
{
return array_merge(parent::all(), $this->route()->parameters());
}
Laravel 5.5 or above:
public function all($keys = null)
{
// Add route parameters to validation data
return array_merge(parent::all(), $this->route()->parameters());
}
The form request validators are used for validating HTML form data that are sent to server via POST method. It is better that you do not use them for validating route parameters. route parameters are mostly used for retrieving data from data base so in order to ensure that your token route parameter is correct change this line of your code, from
$token = Token::where('token', '=', $request->token)->first();
to
$token = Token::where('token', '=', $request->input(token))->firstOrFail();
if you insist on validating your "token" parameter, by form request validators you gonna slow down your application, because you perform two queries to your db, one in here
$token = Token::where('token', '=', $request->token)->first();
and one in here
return [
'token' => ['required','exists:Tokens,token,executed_at,null']
];
Trait
<?php
namespace App\Http\Requests;
/**
* Class RouteParameterValidation
* @package App\Http\Requests
*/
trait RouteParameterValidation{
/**
* @var bool
*/
private $captured_route_vars = false;
/**
* @return mixed
*/
public function all(){
return $this->capture_route_vars(parent::all());
}
/**
* @param $inputs
*
* @return mixed
*/
private function capture_route_vars($inputs){
if($this->captured_route_vars){
return $inputs;
}
$inputs += $this->route()->parameters();
$inputs = self::numbers($inputs);
$this->replace($inputs);
$this->captured_route_vars = true;
return $inputs;
}
/**
* @param $inputs
*
* @return mixed
*/
private static function numbers($inputs){
foreach($inputs as $k => $input){
if(is_numeric($input) and !is_infinite($inputs[$k] * 1)){
$inputs[$k] *= 1;
}
}
return $inputs;
}
}
Usage
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class MyCustomRequest extends FormRequest{
use RouteParameterValidation;
/**
* 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 [
//
'any_route_param' => 'required'//any rule(s) or custom rule(s)
];
}
}
Last Update : 2023-09-22 UTC 12:57:54 PM
Last Update : 2023-09-22 UTC 12:57:41 PM
Last Update : 2023-09-22 UTC 12:57:34 PM
Last Update : 2023-09-22 UTC 12:57:15 PM
Last Update : 2023-09-22 UTC 12:57:04 PM
Last Update : 2023-09-22 UTC 12:56:34 PM
Last Update : 2023-09-22 UTC 12:56:16 PM