代码之家  ›  专栏  ›  技术社区  ›  Amit Shah

Laravel:在FormRequest和Custom规则中传递参数

  •  0
  • Amit Shah  · 技术社区  · 6 年前

    我想通过考试 $this->repo 我在controller中启动的,以便在自定义规则类中可用 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();
            ...
            ...
        }
    }
    

    员工要求:

    <?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 回复  |  直到 6 年前
        1
  •  0
  •   Amit Shah    6 年前

    解决方案

    我是通过尝试才意识到这一点的。当我将我的repository类与singleton绑定时,我可以在我的 EmployeeWorkingHours 规则类

    private function _validate(){
    
        // by adding this line 
        $repo = resolve(ShopManagerRepository::class);        
    
        dd($repo->getShopHoursList());        
        return false;
    }