laravel基于另一列值的唯一验证规则

时间:2015-04-30 05:25:45

标签: validation laravel-4 unique

我需要根据另一个列值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 ??

1 个答案:

答案 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
}