Home > Software design >  Making sure one input field is greater than the other in Laravel
Making sure one input field is greater than the other in Laravel

Time:11-05

I have a form that has a start_odo and an end_odo for vehicle mileage tracking that user needs to enter. 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. Is any way to ensure that the end_odo will always have to be higher?

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. 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', 
]); 

CodePudding user response:

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.

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.

CodePudding user response:

As @zahidhasanemon had already indicated. You can achieve it with customs rules. First step would be to define the new validation rule in your 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', 
]); 
  • Related