4

I am trying to add validation for the min and max values. Both these values are coming from input fields.

I want to validate that max value (max_price) should always be greater than min value (min_price).

I am working on the Laravel 5.7

$validator = validator($request->all(),[
  'min_price' => 'required|min:1"',
  'max_price' => 'required|numeric|min:min_price',
]);
3

2 Answers 2

9

You can use the code below

$validator=validator($request->all(),[
    'min_price'=>'required|min:1',
    'max_price' => 'required|gt:min_price'
]);

References

Sign up to request clarification or add additional context in comments.

1 Comment

Thnx for the solution and refrence. It worked fyn. There is just 1 issue. If we keep min_price field empty then it crashes.
5

You can use the rule gt (Greather than) that expect the number of another field as first argument:

'min_price'=>'required|numeric|min:30',
'max_price'=>'required|numeric|gt:min_price'

3 Comments

Can you explain what you've changed and why? Keep in mind that others should be able to learn from your answer
because gte match value greater than or equal and you say that you get value greater than that i have changed from gte to gt now you can get get value greater than
Please add all such explanation to the answer itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.