[英]Making sure one input field is greater than the other in Laravel
I have a form that has a start_odo
and an end_odo
for vehicle mileage tracking that user needs to enter.我有一个表单,它有一个
start_odo
和一个end_odo
用于用户需要输入的车辆里程跟踪。 The problem is that currently, they can enter a start_odo
that is higher than the end_odo
and that results in a negative difference.问题是,目前,他们可以进入
start_odo
比越高end_odo
和负差的结果。 Is any way to ensure that the end_odo
will always have to be higher?有什么方法可以确保
end_odo
始终必须更高?
Being fairly new to Laravel, I read about the gt:field
but I am not quite sure how to interpret it in my code as it looks different.作为 Laravel 的新手,我阅读了
gt:field
但我不太确定如何在我的代码中解释它,因为它看起来不同。 Could someone nudge me in the right direction.有人可以将我推向正确的方向。
Controller:控制器:
public function store(Request $request)
{
// $energy = new Maintenance;
$energy = new VehicleLog();
$energy->start_odo = $request->input('start_odo');
$energy->end_odo = $request->input('end_odo');
$energy->km = $request->input('end_odo') - $request->input('start_odo');
$energy->save();
return redirect('/vmaintenance')->with('success', 'data added');
}
my view:我的看法:
<div class="mb-3" style="float:left;" style="margin-left: 200px;">
<label for="recipient-name" style="width: 7em"class="col-form-label">Start ODO</label>
<input type="number"style="width: 7em" name="start_odo" class="form-control" id="recipient-name" min="0" required>
</div>
<div class="mb-3">
<label for="recipient-name" class="col-form-label">End ODO</label>
<input type="number" style="width: 7em" name="end_odo" class="form-control" id="recipient-name" min="0" required>
</div>
The validation code I saw while reading:我在阅读时看到的验证代码:
$request->validate([
'detail' => 'gt:20',
]);
you have to figure out yourself how to write code.你必须自己弄清楚如何编写代码。 there are lots of answers and tutorials out there regarding this.
有很多关于此的答案和教程。 however i am adding this answer to get you started.
但是我添加这个答案是为了让你开始。 in your store method, validate the data before saving to database.
在您的 store 方法中,在保存到数据库之前验证数据。
public function store(Request $request)
{
// validation logic
$request->validate([
'start_odo' => 'required',
'end_odo' => 'required|gt:start_odo',
]);
// if validation passes this will now store values in db
$energy = new VehicleLog();
$energy->start_odo = $request->input('start_odo');
$energy->end_odo = $request->input('end_odo');
$energy->km = $request->input('end_odo') - $request->input('start_odo');
$energy->save();
return redirect('/vmaintenance')->with('success', 'data added');
}
now explore the doc how to show validation errors in the form and other ways to validate data.现在探索文档如何在表单中显示验证错误以及其他验证数据的方法。
As @zahidhasanemon had already indicated.正如@zahidhasanemon 已经指出的那样。 You can achieve it with customs rules.
您可以通过海关规则来实现。 First step would be to define the new validation rule in your AppServiceProvider.
第一步是在 AppServiceProvider 中定义新的验证规则。
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
Validator::extend('greater_than_field', function($attribute, $value, $parameters, $validator) {
$min_field = $parameters[0];
$data = $validator->getData();
$min_value = $data[$min_field];
return $value > $min_value;
});
Validator::replacer('greater_than_field', function($message, $attribute, $rule, $parameters) {
return str_replace(':field', $parameters[0], $message);
});
}
}
Then you can use this new validation rule in your controller like that:然后你可以在你的控制器中使用这个新的验证规则:
$request->validate([
// other validations rules
'end_odo' => 'required|greater_than_field:start_odo',
]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.