Laravel 两个值必须不相等验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32631242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:21:47  来源:igfitidea点击:

Laravel Two Values Must Be NOT Equal Validation

phplaravellaravel-4

提问by Jigs Virani

In Laravel 4 I'm trying to find not confirmation validation for input values. For example with confirmationwe can check matching password and repassword and now I want to check contrary of two values. for example Amust be not equal Ain input values and after check in rulesI must be return error. In this sample how to check that?

在 Laravel 4 中,我试图找到对输入值的不确认验证。例如,confirmation我们可以检查匹配的密码和重新密码,现在我想检查两个值的相反数。例如,输入值A必须不相等A,并且在签入后rules我必须返回错误。在这个示例中如何检查?

<input id="starttime_range"
       name="starttime_range"
       type="text"
       class="form-control"
/>

<input id="finish_range"
       name="finish_range"
       type="text"
       class="form-control"
/>

starttime_rangemust be not equal with finish_range

starttime_range必须不等于 finish_range

My code :

我的代码:

public function postInsert()
{
    $rules = array(
        'starttime_range' => 'required|integer',
        'endtime_range' => 'required|integer|not:starttime_range',
    );
    $validator = Validator::make(Input::all(), $rules);

    if ($validator->fails()) {
        return Redirect::back()
            ->withErrors($validator)
            ->withInput();
    } else {

    }
}

回答by Jigs Virani

If you want campare two diffrent value, than you have to use different:

如果你想要两个不同的值,那么你必须使用不同的:

 $rules = array(
    'starttime_range' => 'required|integer',
    'finish_range' => 'required|integer|different:starttime_range',
);