I'm going straight to the point here, I am wondering if it is possible to pass a parameter on a validation rule in Laravel.
Here's my code:
I need to pass the $product->id to the ProductUpdateRequest class.
I've read some articles and to no avail can't pass a parameter into it. my other solution was to not use the validation rule class and do the validation directly on the controller by using $request->validate[()]. Since I can access the $product->id on the controller I can easily do the validation. but out of curiosity is there a way for me to pass the $product->id on the validation class?
CONTROLLER
public function update(ProductUpdateRequest $request, Product $product)
{
$request['detail'] = $request->description;
unset($request['description']);
$product->update($request->all());
return response([
'data' => new ProductResource($product)
], Response::HTTP_CREATED);
}
VALIDATION RULE
public function rules()
{
return [
'name' => 'required|max:255|unique:products,name'.$product->id,
'description' => 'required',
'price' => 'required|numeric|max:500',
'stock' => 'required|max:6',
'discount' => 'required:max:2'
];
}
Any suggestions/answers/help would be highly appreciated.
You can get the resolved binding from request
$product = $this->route('product');
Inside your rules method you can get the product instance with the above method.
public function rules()
{
$product = $this->route('product');
return [
'name' => 'required|max:255|unique:products,name'.$product->id,
'description' => 'required',
'price' => 'required|numeric|max:500',
'stock' => 'required|max:6',
'discount' => 'required:max:2'
];
}
It works when you make a function with this Product $product (when you used the Resource route in most cases)
public function update(ProductUpdateRequest $request, Product $product)
{
// code goes here
}
but if you make it like the below it won't work ()
public function update(ProductUpdateRequest $request, $id)
{
// code goes here
}
This is how I would validate unique product name on update. I pass the product ID as a route parameter, the use the unique validation rule to validate that it the product name does't exist in the Database except for this product (id).
class ProductController extends Controller {
public function update(Request $request, $id) {
$this->validate($request, [
'name' => 'required|max:255|unique:products,name'.$id,
]);
// ...
}
}
For custom request in validation rule you can put in your
View :
<input type="hidden" value="product_id">
In Validation Request :
public function rules()
{
$product_id = $this->request->get('product_id');
return [
//
];
}
Related
I create laravel form validation request and have unique rules on that validation.
I want use it on store and update method without create new form request validation again.
but the problem is when on store the id doesnt exist and the validate is passed
and when on update i failed the pass the validating because the id is exist on storage
i want to ignore the id on unique rules but use same form validate request
what is best practice to check on form validate request class if this action from store or update method to ignore unique id ?
Ok.. i can do it like #porloscerros Ψ suggest
public function rules()
{
$rules = [
'name' => 'required|string|unique:products|max:255',
];
if (in_array($this->method(), ['PUT', 'PATCH'])) {
$product = $this->route()->parameter('product');
$rules['name'] = [
'required',
'string',
'max:255',
Rule::unique('loan_products')->ignore($product),
];
}
return $rules;
}
Try this, it worked for me.
Laravel unique: third param can exclude the id for example, of the record, like this:
public function rules()
{
return [
'name' => 'required|string|max:255|unique:products,'.$this->id,
];
}
Why are you checking the id when store or update in FormRequest? You don't need this. The id comes to your controller's method like as parameter. Or laravel will create the model using DI in the your controller's method public function update(User $user) and then you can use $user like an instance of User model. You may check the id in web.php or api.php:
https://laravel.com/docs/7.x/routing#parameters-regular-expression-constraints
And I suggest you not to use one FormRequest for two methods. This is bad practice
im using this
$validated = $request->validated();
use this method:
public function createAccount(RegisterRequest $request)
{
$attr = $request->validated();
instead of something like this:
public function createAccount(Request $request)
{
$attr = $request->validate([
'name' => 'required|string|max:255',
'email' => 'required|string|email|unique:users,email',
'password' => 'required|string|min:6|confirmed'
]);
use php artisan make:request RegisterRequest
public function rules()
{
return [
'name' => 'required|string|max:255',
'email' => 'required|string|email|unique:users,email',
'password' => 'required|string|min:6|confirmed'
];
}
public function rules()
{
if (request()->isMethod('post')) {
$rules = [
'image' => 'required|image|mimes:jpeg,jpg,png|max:2000',
'name' => 'required|unique:categories'
];
} elseif (request()->isMethod('PUT')) {
$rules = [
'name' => 'required|unique:categories,name'
];
}
return $rules;
}
In my UpdateUserRequest class I have a validation rule that requires using the page ID to exclude the current record from validation. Question is, how can I get the current page ID?
public function rules()
{
return [
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users_admin,email,'. $page_id,
];
}
I know how to do it without the FormRequest class basically by just using the update(Request $request, $id) method in the controller.
I have tried doing this basic way which is by writing a update(Request $request, $id) method in the controller and performing the validations in there. The validation works as expected but then there's another problem of the page wasn't redirecting properly in the Backpack admin after saving.
I actually prefer this basic approach (using store() and update() methods in the controller) than having to have separate FormRequest classes for create and update validations.
Thank you.
We can get the id with the below simple way , i have tried it and it works for me.
public function rules()
{
$page_id = $this->get('id') ?? request()->route('id');
return [
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users_admin,email,'. $page_id,
];
}
Referenced from the below mentioned url
https://github.com/Laravel-Backpack/PermissionManager/blob/master/src/app/Http/Requests/UserUpdateCrudRequest.php
It's better to use the unique method of the Illuminate\Validation\Rule class:
public function rules(): array
{
return [
'name' => [
'required',
Rule::unique('post')->ignore(request()->route('id'))
],
];
}
Detailed description in laravel documentation:
https://laravel.com/docs/9.x/validation#rule-unique
i new in laravel i trying to make custom validation rule to validate name of brand i make this rule and worked normally :-
public function update(Request $request, $id)
{
//prepare data for validation
request()->validate([
'name' => [
'required',
'min:2', // validate english name is exist before
**function ($attribute, $value, $fail) {
$englishname=Brand::where(['name'=>$value,'deleted'=>1 ])->first();
if(false !=$englishname) {
$fail('Opps '.$attribute.' Is Exist Before.');
}**
},
],
],[],[
"name"=>"Brand Name",
]);
validate name success and no problem but my real problem is how link id of brand in case of edit data to make this function
**$englishname=Brand::where(['name'=>$value,'deleted'=>1,'id'=>$id ])->first()**
how to right function in validator ?
$this->validate($request,[
'name'=>['required',Rule::unique('brands')->where('deleted_at',1)],
]);
Note : please import Rule at top.
How to update existing current name ?
Code Below :
public function update(Request $request, $id)
{
$this->validate($request,[
'name'=>['required',Rule::unique('brands')->ignore($id)],
]);
}
If I understand correctly, you try to make brand name unique, so you can try this:
request()->validate([
'name' => 'required|unique:<table name>,name'
]);
You can write custom validate and notification
public function update(Request $request, $id)
{
$this->validate($request,[
'name'=>['required',Rule::unique('brands')->ignore($id)],
],
[
'required'=>'This field is required',
]);
}
I am using Laravel 5.2 and I have managed to set correctly my update request for my users but when I do exactly the same for another table (clusters), I cannot get the id in the clusterrequest validation.
Here is what I have for my clusters:
routes:
Route::post('clusterFormUpdate/{id}', ['uses'=>'ClusterController#postFormUpdate','middleware' => ['permission:user-edit']]);
Controller:
public function postFormUpdate(ClusterUpdateRequest $request, $id)
{
$inputs = $request->all();
ClusterUpdateRequest:
...
public function rules()
{
$id = $this->id;
dd($id);
return [
'name' => 'required|max:255|unique:clusters,name,' . $id . ',id',
'countries' => 'required',
];
}
When I run this, I get null.
If I try the same for my user table, I get the id that is printed correctly so I was wondering where I can look for this problem?
Thanks.
To get the id in the Request you can use the route() method:
public function rules()
{
$id = $this->route('n'); //or whatever it is named in the route
return [
'name' => 'required|max:255|unique:clusters,name,' . $id . ',id',
'countries' => 'required',
];
}
I have used front end is Angular and backend is Laravel,
Create user call store function, it triggers to validate the user request using "UserRequest".
All validation working perfectly.
Now I will update the user record based on the field value change, not all fields.
The UserController update function triggers while updates the record, but not all fields I passed to update. one specific field.
Example:
The requests pass "Age=40" to the only update. but UserRequest validates all the fields and throw the error required fields.
How to I use reuse the request also achieve the output.
// UserController
public function store(UserRequest $request)
{
//TODO...
}
and
// UserRequest
public function rules()
{
return [
'name' => 'required',
'email' => 'required|email',
'age' => 'required|integer|min:15',
];
}
and
// UserController
public function Update(UserRequest $request, User $user)
{
//TODO...
$user->update($request->all());
}
Add sometimes to your rules
public function rules()
{
return [
'name' => 'sometimes|required',
'email' => 'sometimes|required|email',
'age' => 'sometimes|required|integer|min:15',
];
}
This will only validate if the data is present see more https://laravel.com/docs/5.4/validation#conditionally-adding-rules