0

I have created rule validation as follow :

$rules = [
  'items.*.qty' => ['required', 'integer', 'min:1'],
  'items.*.ordered_qty' => ['required','numeric','min:1']
]
$this->validate($request, $rules);

here i need to validate that each ordered_qty should be greater than qty I have tried the below way but it doesn't work for me

$rules = [
  'items.*.qty' => ['required', 'integer', 'min:1','max:items.*.ordered_qty'],
  'items.*.ordered_qty' => ['required','numeric','min:1']
]
$this->validate($request, $rules);

i tried custom validation also but i could not able to getting the value of other field

$rules = [
  'items.*.qty' => ['required', 'integer', 'min:1',function ($attribute, $value, $fail) {
            if ($value > ordered_qty) {
                $fail(':attribute qty is invalid!');
            }
        }],
  'items.*.ordered_qty' => ['required','numeric','min:1']
]

It validate wrong. How can i do the comparision on array of objects among their attributes

6
  • write the custom validation rules for this ? Commented May 31, 2019 at 10:13
  • yes i tried that also but how can i getting the value of other field inside the custom validation function Commented May 31, 2019 at 10:22
  • Possible of duplication stackoverflow.com/questions/32036882/… Commented May 31, 2019 at 10:23
  • @Sethu the mention question is releated to single filed validation but my question is array of fields Commented May 31, 2019 at 10:28
  • try like this 'required|soft_delete:pim_attribute_fields,'.$attribute_id, Commented May 31, 2019 at 10:47

1 Answer 1

1

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

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

Comments

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.