Laravel: Validate an Integer Field That Needs to Be Greater Than Another

Laravel: validate an integer field that needs to be greater than another

There is no built-in validation that would let you compare field values like that in Laravel, so you'll need to implement a custom validator, that will let you reuse validation where needed. Luckily, Laravel makes writing custom validator really easy.

Start with defining new validator in yor 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);
});
}
}

Now you can use your brand new validation rule in your $rules:

$rules = [
'initial_page' => 'required_with:end_page|integer|min:1|digits_between: 1,5',
'end_page' => 'required_with:initial_page|integer|greater_than_field:initial_page|digits_between:1,5'
];

You'll find more info about creating custom validators here: http://laravel.com/docs/5.1/validation#custom-validation-rules. They are easy to define and can then be used everywhere you validate your data.

Making sure one input field is greater than the other in Laravel

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.

Laravel: Validating a number greater than zero is failing

gt, gte, lt and lte are added in Laravel 5.6 and later versions, I'm guessing that must be the reason for you get the error. (It's working for me though.)

I think you can try like this

$request->validate([
'product_price' => 'required|numeric|min:0|not_in:0',
]);

min:0 make sure the minimum value is 0 and no negative values are allowed. not_in:0 make sure value cannot be 0. So, combination of both of these rules does the job.

You can define meaningful error messages for certain rule. (You can achieve the same result using regular expressions as well.)

Laravel Validation = integer value must 2 if checkbox is checked

this seems to work for me:

'txt' => 'required_if:chk,1|integer|between:2,99

Laravel Validation amount array of fields not work

You are using the max on qty but max suspect a value while you are trying to validate it with the input of another field. I think you need to use lte, see: https://laravel.com/docs/5.8/validation#rule-lte



Related Topics



Leave a reply



Submit