2017-05-24 54 views
1

我試圖驗證更新方法laravel 5.4以下是我的驗證代碼:自定義驗證用於更新表

$data = $request->only('id', 'name', 'number', 'logo', 'email', 'address', 'city', 'state', 'country', 'type', 'tier', 'is_client'); 
$company = Companies::find($request->id); 
Validator::make($data, [ 
    'name' => 'required|max:255', 
    'email' => 'required', Rule::unique('companies')->ignore($company->id), 
    'address' => 'max:255', 
    'city' => 'max:255', 
    'state' => 'max:255', 
    'country' => 'max:255', 
]); 

但這驗證休息,假設在更新方法我通過名稱空,不扔錯誤代碼422 Error

它顯示這個錯誤,我無法趕上span標記錯誤並顯示

更新

至於建議中的答案我嘗試添加了驗證,並試圖檢查我收到以下錯誤:

Trying to get property of non-object

以下是我更新的代碼:

$data = $request->only(['id', 'name', 'number', 'logo', 'email', 'address', 'city', 'state', 'country', 'type', 'tier', 'is_client']); 
$company = Companies::find($request->id); 
Validator::make($data, [ 
    'name' => 'required|max:255', 
    'email' => 'required', Rule::unique('companies')->ignore($company->id), 
    'address' => 'max:255', 
    'city' => 'max:255', 
    'state' => 'max:255', 
    'country' => 'max:255', 
])->validate(); 

即使我不讓我的$data = request->only()數組我得到相同的錯誤。

Validation error

回答

1

您只創建Validator,不驗證數據。必須調用validate函數。

Validator::make($data, [ 
    ... 
])->validate(); 

UPDATE

有關錯誤Trying to get property of non-object。通知你逝去的電子郵件驗證規則的字符串,它應該是陣列

'email' => ['required', Rule::unique('companies')->ignore($company->id)],

如果錯誤依然存在。如果變量包含數據,請檢查變量$company

+0

'試圖獲得非對象的屬性' –

+0

@NitishKumar'CompanyController'中的第46行是什麼? –

+0

''email'=>'required',Rule :: unique('companies') - > ignore($ company-> id),' –

0

->only()需要的參數數組:

$request->only(['id', 'name', 'number', 'logo', 'email', 'address', 'city', 'state', 'country', 'type', 'tier', 'is_client']); 

你傳遞了​​幾個參數,來代替。

+0

'試圖獲取非對象的屬性' –