忽略更新函数的唯一验证 Laravel 5
Ignoring Unique Validation on Update Fucntion Laravel 5
我有这个 customer
模型,当您创建它时,它有一个独特的 phone_num
。它在创建中运行良好,但在我的更新函数中运行良好,它是一种基于 id
自动填充值的表单。如果用户仅按预期更新了 address
,unique
验证将再次触发并显示 The phone num has already been taken.
我该如何处理?如果 phone_num
仍然相同,我希望用户能够更新它的信息而忽略 unique
验证,因此如果用户选择更改 phone_num
则允许 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');
}
}
unique
规则为此需要一些额外的参数,看起来像 unique:table,column,except,idColumn
因此,在您的情况下,except
将是 id
,而 idColumn
将是您的 ID 列的名称,可能是 id
。
考虑到这一点,您希望您的验证规则是...
'required|unique:customers,phone_num,'.$id.',id'
在 L5.3+ 中,您可以使用 Rule Facade 流畅地处理它。请注意,
验证规则现在将是数组而不是使用 |定界符。
使用 Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::unique('users')->ignore($user->id),
],
]);
或者您可以在表单请求中做的是(对于 Laravel 5.3+)
public function rules()
{
return [
'email' => 'required|email|unique:users,email,'.$this->user, //here user is users/{user} from resource's route url
];
}
我已经在 Laravel 5.6 完成了并且成功了。
我有这个 customer
模型,当您创建它时,它有一个独特的 phone_num
。它在创建中运行良好,但在我的更新函数中运行良好,它是一种基于 id
自动填充值的表单。如果用户仅按预期更新了 address
,unique
验证将再次触发并显示 The phone num has already been taken.
我该如何处理?如果 phone_num
仍然相同,我希望用户能够更新它的信息而忽略 unique
验证,因此如果用户选择更改 phone_num
则允许 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');
}
}
unique
规则为此需要一些额外的参数,看起来像 unique:table,column,except,idColumn
因此,在您的情况下,except
将是 id
,而 idColumn
将是您的 ID 列的名称,可能是 id
。
考虑到这一点,您希望您的验证规则是...
'required|unique:customers,phone_num,'.$id.',id'
在 L5.3+ 中,您可以使用 Rule Facade 流畅地处理它。请注意, 验证规则现在将是数组而不是使用 |定界符。 使用 Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::unique('users')->ignore($user->id),
],
]);
或者您可以在表单请求中做的是(对于 Laravel 5.3+)
public function rules()
{
return [
'email' => 'required|email|unique:users,email,'.$this->user, //here user is users/{user} from resource's route url
];
}
我已经在 Laravel 5.6 完成了并且成功了。