忽略獨特驗證我有當您創建具有unqiue phone_num
這個customer
模型。它在創建時可以正常工作,但是在我的更新函數中,這是一種基於id
的自動填充值形式。如果用戶只按預期更新了address
,則unique
驗證將再次觸發The phone num has already been taken.
如何處理此問題?我希望用戶能夠更新它忽略了unique
驗證,如果phone_num
仍從而使得如果用戶選擇改變phone_num
上更新溫控功能Laravel 5
我的代碼unique
驗證相同的信息:
public function update($id)
{
$rules = array(
'title' => 'required',
'firstname' => 'required',
'lastname' => 'required',
'gender' => 'required',
'birthdate' => 'required',
'age_bracket' => 'required',
'addr1' => 'required',
'addr2' => 'required',
'addr3' => 'required',
'addr4' => 'required',
'town' => 'required',
'country' => 'required',
'postcode' => 'required',
'phone_num' => 'required|unique:customers',
'phone_type' => 'required',
'work_status' => 'required',
'home_status' => 'required',
'marital_status' => 'required',
);
$validator = Validator::make(Input::all(), $rules);
// Check if all fields is filled
if ($validator->fails())
{
return Redirect::to('customer/'.$id.'/edit')->withErrors($validator);
}
else
{
$customer = Customer::find($id);
$customer->title = Input::get('title');
$customer->gender = Input::get('gender');
$customer->firstname = Input::get('firstname');
$customer->lastname = Input::get('lastname');
$customer->postcode = Input::get('postcode');
$customer->addr1 = Input::get('addr1');
$customer->addr2 = Input::get('addr2');
$customer->addr3 = Input::get('addr3');
$customer->addr4 = Input::get('addr4');
$customer->addr4 = Input::get('addr4');
$customer->town = Input::get('town');
$customer->country = Input::get('country');
$customer->phone_num = Input::get('phone_num');
$customer->phone_type = Input::get('phone_type');
$customer->birthdate = Input::get('birthdate');
$customer->work_status = Input::get('work_status');
$customer->home_status = Input::get('home_status');
$customer->marital_status = Input::get('marital_status');
$customer->agebracket = Input::get('age_bracket');
if($customer->save())
{
Session::flash('alert-success', 'Form Submitted Successfully.');
}
else
{
Session::flash('alert-danger', 'Error on submitting form.');
}
return Redirect::to('customer/'.$id.'/edit');
}
}
哇!完美的作品!非常感謝! – jackhammer013