laravel validation custom requested fields - laravel

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

Related

Scope resource controller based on pivot table in Laravel

I have a User and an Organization model, bound together by a many-to-many relationship in a organization_user pivot table.
The User model contains:
public function organizations()
{
return $this->belongsToMany(Organization::class);
}
And likewise the Organization model contains:
public function users()
{
return $this->belongsToMany(User::class);
}
Organizations are a resource that a user can create, view, update, delete, and so forth. Of course, users should only be able to view, update and delete the organizations they're part of. I manage to scope the organizations to the current user in the OrganizationController index method, but I'm not sure how to do the same for the edit, update and delete methods. Such guards are not necessary for the create and store methods, I believe.
The controller looks like this:
class OrganizationController extends Controller
{
public function index(Request $request)
{
$organizations = $request->user()->organizations()->get();
return view('organizations.index', compact('organizations'));
}
public function create()
{
return view('organizations.create');
}
public function store(Request $request)
{
$request->validate([
'name' => ['required', 'string', 'max:255'],
]);
$request->user()->organizations()->create([
'name' => $request->name,
]);
return redirect()->route('organizations.index');
}
public function edit(Request $request, Organization $organization)
{
// How to make make sure the user is part of the organization?
return view('organizations.edit', [
'organization' => $organization,
]);
}
public function update(Request $request, Organization $organization)
{
// How to make make sure the user is part of the organization?
$request->validate([
'name' => ['required', 'string', 'max:255'],
]);
$organization->update([
'name' => $request->name,
]);
return redirect()->route('organizations.index');
}
}

Laravel form request validation on store and update use same validation

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;
}

Laravel 6 Backpack 4.0: How to get the current page ID in FormRequest class or can I get by without using FormRequest classes?

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

laravel request validation rules pass parameter

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 [
//
];
}

Laravel: Undefined variable: request

I don´t can find the problem. Where ist it? "Undefined variable: request"
public function login()
{
// Validate the form data
$this->validate($request, [
'email' => 'required|email',
'passwort' => 'required|min:6'
]);
// Attempt to log the user in
if (Auth::guard('admin')->attempt(['email' => $request->email, 'passwort' => $request->passwort], $remember)) {
// if successful, then redirect to their intended location
return redirect()->intended(roue('admin.dashboard'));
}
// if unsuccessful, then redirect back to the login with the form data
return redirect()->back()->withInput($request->only('email', 'remember'));
}
You need to inject Request object:
public function login(Request $request)
Also, add this line to the top of your class:
use Illuminate\Http\Request;
Alternatively, you can just use request() instead of $request in your code.
You have not defined the 'request' variable but you are using it in your code.
Edited Code
public function login(Request $request)
{
// Validate the form data
$this->validate($request, [
'email' => 'required|email',
'passwort' => 'required|min:6'
]);
// Attempt to log the user in
if (Auth::guard('admin')->attempt(['email' => $request->email, 'passwort' => $request->passwort], $remember)) {
// if successful, then redirect to their intended location
return redirect()->intended(roue('admin.dashboard'));
}
// if unsuccessful, then redirect back to the login with the form data
return redirect()->back()->withInput($request->only('email', 'remember'));
}
Also, add this line to the top of your class:
use Illuminate\Http\Request;

Resources