Home > Blockchain >  why my validation rules in trait are not working when i call it from controller?
why my validation rules in trait are not working when i call it from controller?

Time:06-28

this us my trait. validation rules are not working even i give 1 for the page number getting response not validation error.

trait ValidatePagination
            {
protected function pagination(Request $request)
{ 
    $rules['page'] = 'integer|gt:4';
    $rules['per_page'] = 'integer|gt:0|lte:100';
    $validator = \Validator::make($request->all(), $rules);

   if ($validator->fails()) {
        return response()->json($validator->errors(), Response::HTTP_BAD_REQUEST);
    }
}

}

my controller's method

  public function get(Request $request): JsonResponse
{
   $companyId = $request['user']->cid;
    $perPage = $request->query('per_page', 15);
    
    $this->pagination($request);

    $staffTable = TableBuilder::get($companyId, STAFF);

    $staff = $staffTable->get(['pid', 'name', 'mobile', 'pay_rate', 'is_working', 'pay_start_date', 'pay_end_date']);
    $staff = $staffTable->orderBy('updated_at', 'desc')->simplePaginate($perPage);

    return $this->success('Fetched staff members', $staff);
}

CodePudding user response:

The problem is, you are not using the returned data, you should throw an exception instead, this is kinda tricky to get the correct data into the validation exception. The most correct approach is to use form requests, which you can also reuse across controllers.

The request can be created by using php artisan make:request PaginatedRequest. For further information see the docs.

use Illuminate\Foundation\Http\FormRequest;

class PaginatedRequest extends FormRequest {

    public function rules()
    {
        return [
            'page' => 'integer|gt:4',
            'per_page' => 'integer|gt:0|lte:100',
        ];
    }
}

This will automatically resolve the validator and throw the correct exception. The pattern is also recommended as a good practice in Laravel best practices.

public function get(PaginatedRequest $request): JsonResponse
{
    ....
}

CodePudding user response:

you should return the failed validation inside get function also like so:

public function get(Request $request): JsonResponse
{
   $companyId = $request['user']->cid;
    $perPage = $request->query('per_page', 15);
    
    if ($validatorValue = $this->pagination($request)) {
        return $validatorValue;
    }

    $staffTable = TableBuilder::get($companyId, STAFF);

    $staff = $staffTable->get(['pid', 'name', 'mobile', 'pay_rate', 'is_working', 'pay_start_date', 'pay_end_date']);
    $staff = $staffTable->orderBy('updated_at', 'desc')->simplePaginate($perPage);

    return $this->success('Fetched staff members', $staff);
}

CodePudding user response:

To me, it looks like you are missing returning the validation error from the controller code. An easy fix would be to run the validate instead of the fails code. I would say the most correct approach would be @mrhn solution since you should ideally create a FormRequest.

trait ValidatePagination
{
    protected function pagination(Request $request)
    { 
        $rules['page'] = 'integer|gt:4';
        $rules['per_page'] = 'integer|gt:0|lte:100';
        $validator = \Validator::make($request->all(), $rules);

        $validator->validate();
    }
  • Related