Laravel 5.5条件表单请求验证规则更新

Laravel 5.5条件表单请求验证规则更新

问题描述:

我为图像表单创建了验证规则。Laravel 5.5条件表单请求验证规则更新

它在存储方法上工作正常,但我不希望在更新时需要图像字段,因为我可能只会更新标题。

class ImageRequest extends Request 
{ 
    /** 
    * Rules array 
    */ 
    protected $rules = [ 
     'title' => 'required|string|between:3,60', 
     'alt' => 'sometimes|string|between:3,60', 
     'image' => 'required|image|max:4000|dimensions:min_width=200,min_height=200', 
    ]; 

    /** 
    * Determine if the user is authorized to make this request. 
    * 
    * @return bool 
    */ 
    public function authorize() 
    { 
     return true; 
    } 

    /** 
    * Get the validation rules that apply to the request. 
    * 
    * @return array 
    */ 
    public function rules() 
    { 
     return $this->rules; 
    } 
} 

对于独特验证,我们可以添加自定义查询条件:

'email' => Rule::unique('users')->ignore($user->id, 'user_id') 

'email' => Rule::unique('users')->where(function ($query) { 
    return $query->where('account_id', 1); 
}) 

它是一个干净的方式来实现类似的需要的东西吗?

适用要求只适用于新图像。

+0

请接受其中任何一个解决您的问题,关闭了这个问题,并给予好评任何/所有的答案已经帮助解决您的问题的答案 –

我找到了解决方案。

我将其更名为图片转换为文件

这条路线是homestead.app/images/1更新宅基地。上商店应用程序/图像所以$图像属性将是$ 这 - >图像= 1更新$这 - >图像上= 商店

class ImageRequest extends Request 
{ 
    /** 
    * Rules array 
    */ 
    protected $rules = [ 
     'title'=> 'required|string|between:3,60', 
     'alt' => 'sometimes|string|between:3,60', 
     'file' => [ 
      'image', 
      'max:4000', 
      'dimensions:min_width=200,min_height=200', 
     ], 
    ]; 

    /** 
    * Determine if the user is authorized to make this request. 
    * 
    * @return bool 
    */ 
    public function authorize() 
    { 
     return true; 
    } 

    /** 
    * Get the validation rules that apply to the request. 
    * 
    * @return array 
    */ 
    public function rules() 
    { 
     $this->rules['file'][] = is_null($this->image) ? 'required' : 'sometimes'; 

     return $this->rules; 
    } 
} 

你可以使用switch语句规则

public function rules() 
    { 

     switch ($this->method()) { 
      case 'GET': 
      case 'DELETE': { 
       return []; 
      } 
      case 'POST': { 

         return [ 
          'first_name'=>'required', 
          'last_name'=>'required', 
         'email'=>'required|email|unique:users,email,'.$this->id, 
          'password'=>'', 
          'dob'=>'required', 
          'phone_one'=>'required', 
          'phone_two'=>'required', 
          //'user_role'=>'required', 
         // 'profile_image'=>'required' 
         ]; 
      } 
      case 'PUT': 
      case 'PATCH': { 
       return [ 

       ]; 
      } 
      default:break; 
     } 

您也可以在更新使用condtion像宥有ID,以便根据您可以检查其是否更新或插入,因为在插入你没有ID,以便

创建扩展请求类另一个类,DI是到您的更新控制器动作

class UpdateImageRequest extends Request 
{ 
    /** 
    * Rules array 
    */ 
    protected $rules = [ 
     'title' => 'required|string|between:3,60', 
     'alt' => 'sometimes|string|between:3,60' 
    ]; 

    /** 
    * Determine if the user is authorized to make this request. 
    * 
    * @return bool 
    */ 
    public function authorize() 
    { 
     return true; 
    } 

    /** 
    * Get the validation rules that apply to the request. 
    * 
    * @return array 
    */ 
    public function rules() 
    { 
     return $this->rules; 
    } 
} 
+0

是的,这是备份解决方案(包括:“图像” =>'有时候| image | max:4000 | dimensions:min_width = 200,min_height = 200')但我希望重复使用类似的验证类: Rule :: sometimes('required',function($ query){ return $ query-> where('image_id',1); }); –

+0

你可以从这个类延伸普通的类,你只是改变规则 – madalinivascu

更好的办法是在L使用nullable篷帐5.5验证

Docs

下验证的字段可以是空的。当验证可以包含空值的字符串和整数等基元时,这是特别有用的。 。

class ImageRequest extends Request 
{ 
    /** 
    * Rules array 
    */ 
    protected $rules = [ 
     'title' => 'required|string|between:3,60', 
     'alt' => 'nullable|string|between:3,60', 
     'image' => 'nullable|image|max:4000|dimensions:min_width=200,min_height=200', 
    ]; 

    /** 
    * Determine if the user is authorized to make this request. 
    * 
    * @return bool 
    */ 
    public function authorize() 
    { 
     return true; 
    } 

    /** 
    * Get the validation rules that apply to the request. 
    * 
    * @return array 
    */ 
    public function rules() 
    { 
     return $this->rules; 
    } 
} 

但是我已经和影像最近使用过的和它的工作般的魅力对我来说。试一试!

这种情况下最简单的方法是以其他方式。默认情况下有更新的规则,如果它的存储添加需要像这样:

class ImageRequest extends Request 
{ 
    /** 
    * Rules array 
    */ 
    protected $rules = [ 
     'title' => 'required|string|between:3,60', 
     'alt' => 'sometimes|string|between:3,60', 
     'image' => 'image|max:4000|dimensions:min_width=200,min_height=200', 
    ]; 

    /** 
    * Determine if the user is authorized to make this request. 
    * 
    * @return bool 
    */ 
    public function authorize() 
    { 
     return true; 
    } 

    /** 
    * Get the validation rules that apply to the request. 
    * 
    * @return array 
    */ 
    public function rules() 
    { 
     $rules = $this->rules; 

     if ($this->isMethod('POST')) { 
      $rules['image'] = 'required|' . $rules['image'] 
     } 

     return $rules; 
    } 
}