简体   繁体   English

确保 Laravel 中的一个输入字段大于另一个

[英]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.

相关问题 laravel比其他领域更大 - laravel Greater than other field Laravel 4.2验证输入必须大于其他输入 - Laravel 4.2 validation input has to be greater than other input 如何在MongoDB中选择一个字段大于其他字段的文档? - How to select documents with one field greater than other in MongoDB? 如果一个输入值大于php中的另一个输入值,则回显错误消息 - Echo error message if one input value is greater than other in php 如何使用 Laravel 中的验证使输入日期字段大于或等于另一个日期字段 - how to make an input date field greater than or equal to another date field using validation in laravel Laravel 验证一个字段大于或等于另一个字段不起作用 - Laravel validation of one field being greater than or equal to another field not working 如何检查一个值是否大于另一个 - How to check if one value is greater than other 已解决:Laravel 6 如果总输入大于 85,则无法获取数组输入(同名输入)与其他输入的验证错误消息 - Solved: Laravel 6 Can't get error message of validation of array input (input with same name) with other inputs if total input are greater than 85 Laravel:验证需要大于另一个的 integer 字段 - Laravel: validate an integer field that needs to be greater than another 确保股息大于除数的功能? - Function to make sure dividend is greater than divisor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM