Laravel:在FormRequest和自定义规则中传递参数

时间:2018-11-28 10:59:25

标签: laravel validation oop

我正在尝试将我在控制器中初始化的$this->repo传递给我正在使用EmployeeWorkingHours调用的自定义规则类EmployeeRequest中可用,以便可以获取数据从数据库进行验证。

下面是我的代码示例:

控制器:

<?php

namespace Modules\ShopManager\Http\Controllers;

use App\Http\Controllers\Controller;
use Auth;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Modules\ShopManager\Entities\Repository\Contract\ShopManagerRepository;
use Validator;
use Modules\ShopManager\Http\Requests\EmployeeRequest;

class EmployeesController extends Controller
{
    public function __construct(ShopManagerRepository $repo)
    {
        $this->repo = $repo;

        $this->middleware(function ($request, $next) {

            $this->repo->setManagerId(Auth::id()); //new $repo(Auth::id());
            $this->shopId = $this->repo->getShopId();

            if (!is_null($this->shopId)) {
                $this->storagePath = 'images/shopmanager/shop/' . $this->shopId . '/employees';
            } else {
                abort(404);
            }

            return $next($request);
        });
    }

    public function store(EmployeeRequest $request)
    {           

        $data = $request->all();
        ...
        ...
    }
}

EmployeeRequest:

<?php

namespace Modules\ShopManager\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Modules\ShopManager\Rules\EmployeeWorkingHours;

class EmployeeRequest extends FormRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'first_name' => 'required',
            'last_name' => 'required',
            'email' => 'required|email',
            'from_hour' => ['required','numeric', new EmployeeWorkingHours],
            'to_hour' => ['required','numeric', new EmployeeWorkingHours],
            'status' => 'required|numeric',
            'profile_image' => 'image',
        ];
    }

    public function messages()
    {
        return [
            'profile_image.image' => 'Uploaded file is not an image.',
        ];
    }
    public function attributes()
    {
        return [
            'first_name' => 'First Name',
            'last_name' => 'Last Name',
            'email' => 'Email'
        ];
    }
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }
}

EmployeeWorkingHours:规则

<?php

namespace Modules\ShopManager\Rules;

use Illuminate\Contracts\Validation\Rule;

class EmployeeWorkingHours implements Rule
{
    private static $from;
    private static $to;
    /**
     * Create a new rule instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        switch ($attribute) {
            case 'from_hour':
                self::$from = $value;
                break;
            case 'to_hour':
                self::$to = $value;
                return $this->_validate();
                break;
        }        
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return 'Please select valid Time Interval From hour & To hour.';
    }

    private function _validate(){
        //dd(self::$from, self::$to);
        return false;
    }
}

1 个答案:

答案 0 :(得分:0)

  

解决方案

我只是通过尝试一下才意识到。在将存储库类与单例绑定在一起时,我可以在EmployeeWorkingHours规则类

中的任何位置解析相同的类
private function _validate(){

    // by adding this line 
    $repo = resolve(ShopManagerRepository::class);        

    dd($repo->getShopHoursList());        
    return false;
}