2017-10-10 6 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

は、この質問を閉じて、あなたの問題を解決するために役立っている任意の/すべての答えをupvoteするためにあなたの問題を解決した答えのいずれかを受け入れてください –

答えて

-2

解決策が見つかりました。

イメージに変更しました。

経路は更新ステッドにhomestead.app/images/1あります。ストアでアプリ/画像よう$画像プロパティはストアの$ this - >画像= 1更新との$ this - >画像= NULLあろう。 「イメージ」=> ':

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

挿入時にあなたがIDを持っていけないので、また、あなたがそう

ので、あなたはその更新するかどうかをチェックしたり、挿入することができ、そのIDに基づいているyuo更新に似condtionを使用することができます
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

はい、これは(を含むバックアップソリューションです私は、同じ検証クラスを次のようなものを使用して再利用したいと考えていました: ルール::時には( '必須'、function($ query){image | max:4000 | dimensions:min_width = 200、min_height = 200 ' return $ query-> where( 'image_id'、1); }); –

+0

このクラスからルールを変更できる通常のクラスを拡張することができます – madalinivascu

0

もっと良い方法は、Lでnullableを使用することである、Requestクラスを拡張する他のクラスを作成します。 aravel 5.5検証

参考Docs

検証の下のフィールドがnullの場合もあります。 にnull値が含まれている可能性のある文字列や整数などのプリミティブを検証する場合には、これは特に便利です。

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