Type error: Argument 1 passed to App\Http\Controllers\Controller::validate() must be an instance of Illuminate\Http\Request, array given, called in app\Http\Controllers\RegistrationController.php on line 23
It doesn't work here
Registration Controller
but at the same time works fine in another controller
AuthController
The reason you're getting this error is because you're passing your validation rules to the request() helper function and not as the 2nd param to $this->validate()
You can still use the request() helper function but you just need to do:
$this->validate(request(), [
'name' => 'required',
'email' => 'required|email',
'password' => 'password|confirmed', //<-- Is the password rule something you've created?!?
]);
Hope this helps!
function store()should be function store(Request $request) if you want to use the request. However #CBroe is right: please learn to ask your questions better.
Related
I'm working on a Laravel 5.8 project and trying to show custom validation messages for a validation which uses the requiredIf validation rule.
Here is how I have it set up:
$validation = Validator::make(
$request->all(),
[
...
'sum' => [
Rule::requiredIf(function() use ($request){
$model = Model::find($request->id);
return $model->is_special; //returns a boolean value
}),
'numeric'
],
...
],
[
...
'sum.required_if' => 'This cannot be blank',
'sum.numeric' => 'Must use a number here',
...
]
);
Now the validation is working correctly and the custom message for the numeric validation shows as should, but the message I get for the requiredIf() method is Laravel's default error message.
I also tried using 'sum.requiredIf' => '...' but that didn't work either and can't seem to find any documentation or example for this scenario.
I was tinkering with this for a while and noticed that for this to work I needed to define
'sum.required' => 'This cannot be blank'
and not 'sum.required_if' => 'This cannot be blank',.
Not sure if this is expected behavior or just a workaround but my deduction is that with the callback Rule::requiredIf(function() use ($request){...}) the parameters :other and :value are not passed so it falls back onto required messaging and I guess this makes sense since required_if and required would not be used on the same :attribute.
Hope this helps anyone who comes across this problem.
First, create a rule name isSpecial or whatever
php artisan make:rule isSpecial
Go to App\Rules\isSpecial.php
private $id;
public function __construct($id) // pass id or what you need
{
//
$this->id=$id;
}
public function passes($attribute, $value) // customize your rules here
{
//
return Model::find($request->id)->is_special;
}
public function message() // here is answer for your question
{
return 'The validation error message.'; // your message
}
in your controller
use App\Rules\isSpecial;
\Validator::make($request->all(), [
'sum' => new isSpecial() ,
])->validate();
another idea :
Specifying Custom Messages In Language Files
In most cases, you will probably specify your custom messages in a language file instead of passing them directly to the Validator. To do so, add your messages to custom array in the resources/lang/xx/validation.php language file.
'custom' => [
'email' => [
'required' => 'We need to know your e-mail address!',
],
],
Simple notice:
- I suggest using HTTP Requests instead use validation in your controller and function direct
Looks like as of Laravel 8, using required_if works as expected, and alternatively will not fall back on required as mentioned previously:
'sum.required_if' => 'This cannot be blank',
I wanted to validate inputs from a GET request without using the
this->validate($request... or \Validator::make($request...
and prefer to do it like
$input = $request->validate([... rules ...]);
however since get requests doesn't have $request parameters how can I achieve it?
public function sampleGet($param1, $param2) {
// How can I pass the $param1 and $param to to validate?
$input = $request->validate([
'param1' => 'required',
'param2' => 'required
]);
}
You can do so and it will have same behavior as validate
validator($request->route()->parameters(), [
'param1' => 'required',
'param2' => 'required'
....
])->validate();
If you want all the route parameters you can get them as an array:
$request->route()->parameters()
Since you already have those parameters being passed to your method you can just build an array with them:
compact('param1', 'param2');
// or
['param1' => $param1, 'param2' => $param2];
You are not going to be using the validate method on the Request though, you will have to manually create a validator. Unless you want to merge this array into the request or create a new request with these as inputs.
There is nothing special about the validate method on a Controller or on a Request. They are all making a validator and validating the data the same way you would yourself.
When manually creating a validator you still have a validate method that will throw an exception, which would be the equivalent to what is happening on Request and the Controller with their validate methods.
Laravel 7.x Docs - Validation - Manualy Creating Validators - Automatic Redirection
You can do like that.
public function getData(Request $request)
{
try {
$input['route1'] = $request->route('route1');
$input['route2'] = $request->route('route2');
$valid = Validator::make($input, [
'route1' => 'required',
'route2' => 'required'
]);
} catch (\Throwable $th) {
echo "<pre>";print_r($th->__toString());die;
}
}
Or you can follow the below link for more info.
https://laravel.com/docs/7.x/validation#manually-creating-validators
How are you? Hope you are doing great
I need one help for Laravel Validator, i have created one custom validation rule like below
$validation = Validator::make($request->all(), [
'user_id' => 'required',
'role' => ['required', new RoleExist($request->user_id)],
]);
See i have passed one argument to rule's constructor new RoleExist($request->user_id) but laravel giving me 500 error if i do not pass user_id in the request
The error is
Argument 1 passed to App\Rules\RoleExist::__construct() must be of the type integer, null given
I know user_id is not passed in the request so laravel giving above error, but here my custom rule should be execute after 'user_id' => 'required',
Custom Rule Code
private $userId;
public function __construct(Int $userId)
{
$this->userId= $userId;
}
public function passes($attribute, $value)
{
return empty(\App\User::where('user_id', $this->userId)->where('status', '1')->first());
}
Is there any way to do the same
Thank you in advance
I currently have a controller, with its path declared like this:
Route::resource('/route_testing', 'RouteTestingController')->middleware('checkPermission:my_permission_index');
he accesses the store() method
and I have my test done like this:
public function testCreateExcessCarriers(){
$formData = [
'user_id' => 1233,
'agency_id' => 444,
'uuid' => \Uuid::generate(),
'email' => 'faker#faker.com',
'name' => 'faker name'
];
//$this->withoutExceptionHandling();
$this->post(route('route_testing.store'), $formData)
->assertStatus(200);
}
The problem I have is that in my controller the store () method is commented, I have commented to see if the error in the test jumps.
That the store() method in the controller is commented, that is to say as it would not exist but in the test if it gives OK, I understand it is because if you made the insert which I understand it cannot be true since it is commented. And if I try it from the front, it effectively jumps Status Code: 500 Internal Server Error. Shouldn't it be the same in the test?
I'm trying to validate post request. I think when the validation fails, it gives me this error message.
app/Http/Controllers/InternationalShippingController.php
public function store(Request $request){
//echo '<pre>';
$post = $request->post();
$order_ids = session('international_order_ids');
//var_dump($order_ids);
//var_dump($post);
$validator = Validator::make(
$post,[
'documents.*' => 'mimes:jpg,jpeg,png,pdf|max:5000|nullable',
'company_name' => 'nullable',
'shipping_address1' => 'nullable',
'message' => 'size:1000',
],[
'image_file.*.mimes' => __('Only jpeg,png and pdf files are allowed'),
'image_file.*.max' => __('Sorry! Maximum allowed size for an document is 5MB'),
]
);
if($validator->fails()){
return redirect('internationalshippings/create2')
->withErrors($validator)
->withInput();
}
}
web.php
Route::post('internationalshippings/create2','InternationalShippingController#create2');
Route::resource('internationalshippings','InternationalShippingController');
I haven't made show() method in the controller.
Does this error mean when the validation fails, it tries to redirect to internationalshippings/show method?
When the validation fails,I'd like this to redirect back to internationalshippings/create2. How can I achieve this?
Thank you
you are using the resource controller,
in resources this url internationalshippings/SomeThing means the show method i mean this url calls the show method in resource
First way
so you can use this in your fail return:
return redirect()->route('your_route_name') OR return back()
Second way
and the second way is in your web.php, when you are defining the resource route, type in this way:
Route::resource('internationalshippings','InternationalShippingController',['except'=>['show']]);
EDIT:
in your code situation the best way is change Return, because the url that you want to redirect to it, is POST