2017-10-10 182 views
2

我爲圖像表單創建了驗證規則。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

請接受其中任何一個解決您的問題,關閉了這個問題,並給予好評任何/所有的答案已經幫助解決您的問題的答案 –

回答

-2

我找到了解決方案。

我將其更名爲圖片轉換爲文件

這條路線是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; 
    } 
} 
2

你可以使用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,以便

0

創建擴展請求類另一個類,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

0

更好的辦法是在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; 
    } 
} 

但是我已經和影像最近使用過的和它的工作般的魅力對我來說。試一試!

0

這種情況下最簡單的方法是以其他方式。默認情況下有更新的規則,如果它的存儲添加需要像這樣:

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