0

hi i have project on laravel 5.6 and this is my role for validation

'voucher_debt' => 'required|array|min:1',
'voucher_debt.*' => 'nullable|numeric|min:0.001',
'voucher_credit' => 'required|array|min:1',
'voucher_credit.*' => 'nullable|numeric|min:0.001',

my problem thats i need it to check if the

array_sum($voucher_credit) - array_sum($voucher_debt) == 0

i tried many thing nothing works out with me is that possible on laravel

1 Answer 1

1

You may do this in your request:

public function rules()
{
   $rules = [
          'voucher_debt' => ['required', 'array', 'min:1'],
          'voucher_debt.*' => ['nullable', 'numeric', 'min:0.001'],
          'voucher_credit' => ['required', 'array', 'min:1'],
          'voucher_credit.*' => ['nullable', 'numeric' ,'min:0.001'],
   ];

   if (array_sum($this->get('voucher_debt')) - array_sum($this->get('voucher_debt')) == 0) {
       throw new ConflictHttpException('YOUR_MESSAGE');
   }
}

Or you can make your custom validation rule and add to voucher_debt key

https://laravel.com/docs/5.6/validation#custom-validation-rules

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

2 Comments

i gat this error Symfony \ Component \ HttpKernel \ Exception \ ConflictHttpException YOUR_MESSAGE
yeah, thats right, or you need ValidationException? if your validation failed laravel throw validation exception, but in your case i think need conflict exception

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.