我需要根据另一个列值temp_name
对名为temp_mode
的元素进行唯一验证。当temp_name
值为temp_mode
时,email
必须是唯一的。我像这样做了规则
'temp_name' => 'unique:templates,temp_name,NULL,temp_id,temp_mode,email'
其中templates
是数据库名称。这没用。对于This template name is already present
的所有值,它始终显示temp_mode
。我知道会有其他方法来实现这一目标。任何人都可以给我一个解决这个问题的想法。
修改
从下面的回答我已经尝试过这段代码
$rules = array(
'temp_txt' => 'required',
);
$validator = Validator::make(Input::all(),$rules);
$validator->sometimes('temp_name', 'unique:templates', function($input){
return $input->temp_mode == 'email';
});
但是这显示了错误
BadMethodCallException Method [sometimes] does not exist
我应该在项目中安装任何其他功能才能使用sometimes
??
答案 0 :(得分:0)
您可以conditionally add validation rules使用sometimes
:
$validator = Validator::make(...);
$validator->sometimes('temp_name', 'unique:templates', function($input){
return $input->temp_mode == 'email';
});
if($validator->passes()){
// yay
}