0

there I'm facing an issue with the validation, Input arrays should include at least one value for example when I submit a form I get the following inputs

 array:26 [
  "_token" => "GMPKHos9nnC6zw6UGN4nFKVEGd5PoEpDe9lEPETw"
  "Customer" => "1"
  "Text_From_Date" => "07 Jun 2021"
  "From_Date" => "2021-06-07"
  "Text_Due_Date" => "16 Jun 2021"
  "Due_Date" => "2021-06-16"
  "Invoice_Id" => "INV-1"
  "Reference" => null
  "Branding" => null
  "Add_Currency" => null
  "Tax_Type" => "ex_tax"
  "Item" => array:5 [
    0 => null
    1 => null
    2 => null
    3 => null
    4 => null
  ]
  "Product_Description" => array:5 [
    0 => null
    1 => null
    2 => null
    3 => null
    4 => null
  ]
  "Quantity" => array:5 [
    0 => null
    1 => null
    2 => null
    3 => null
    4 => null
  ]
  "Unit_Price" => array:5 [
    0 => null
    1 => null
    2 => null
    3 => null
    4 => null
  ]
  "Discount" => array:5 [
    0 => null
    1 => null
    2 => null
    3 => null
    4 => null
  ]
  "Discount_Amount" => array:5 [
    0 => null
    1 => null
    2 => null
    3 => null
    4 => null
  ]
  "Account" => array:5 [
    0 => null
    1 => null
    2 => null
    3 => null
    4 => null
  ]
  "Tax_Rate" => array:5 [
    0 => "0"
    1 => "0"
    2 => "0"
    3 => "0"
    4 => "0"
  ]
  "Tax_Rate_Amount" => array:5 [
    0 => null
    1 => null
    2 => null
    3 => null
    4 => null
  ]
  "Region" => array:5 [
    0 => null
    1 => null
    2 => null
    3 => null
    4 => null
  ]
  "Amount" => array:5 [
    0 => "0"
    1 => "0"
    2 => "0"
    3 => "0"
    4 => "0"
  ]
  "Sub_Total" => "0"
  "Total_Discount" => "0"
  "Grand_Total" => "0"
  "Form_Type" => "1"
]

here are the validation rules in the controller

 public function save(Request $request)
    {
        $rules =  [
            'Customer' => 'required',
            'Text_From_Date' => 'required',
            'From_Date' => 'required|date',
            'Text_Due_Date' => 'required',
            'Due_Date' => 'required|date',
            'Reference' => 'bail|nullable|string|min:3',
            'Add_Currency' => 'bail|nullable',
            'Tax_Type' => 'required',
            'Product_Description.*' => 'exclude_if:Quantity.*,null|exclude_if:Unit_Price.*,null|required|max:1000',
            'Item.*' => 'exclude_if:Quantity.*,null|exclude_if:Unit_Price.*,null|bail|nullable',
            'Quantity.*' => 'exclude_if:Product_Description.*,null|required|numeric',
            'Unit_Price.*' => 'exclude_if:Product_Description.*,null|required|numeric',
            'Account.*' => 'exclude_if:Product_Description.*,null|required|numeric',
            'Amount.*' => 'exclude_if:Product_Description.*,null|required|numeric',
            'Sub_Total' => 'required|numeric',
            'Total_Discount' => 'required|numeric',

        ];
        $validator = Validator::make($request->all(), $rules);
        if ($validator->fails()) {
            return response()->json($validator->errors(), 422);
        } else {
            dd($validator->validated());
            return response()->json('saved', 200);
        }
    }

arrays Item , Product_Description, Unit_Price , Quantity , Discount , Discount_Amount , Discount_Amount , Account , Tax_Rate , Tax_Rate_Amount , Region , Amount Should have at least one notNull Value.

Please Help to resolve the issue Thanks in Advance!

3
  • you can use: "Product_Description.0" => "required", see: stackoverflow.com/a/57200746/10573560 Commented Jun 7, 2021 at 15:23
  • @OMR I don't think that's gonna work, he's passing the array with as key pair value, so it will says the element exists, I faced something like that before, I end up creating a custom validator, that would be really useful since that situation is repeating multiple times. Commented Jun 7, 2021 at 15:27
  • @OMR I don't want to fill only the first array element any of these can be filled elements having null will be excluded. Commented Jun 7, 2021 at 16:27

2 Answers 2

1

You can use custom validation.You can create custom rule to keep code clean.This is just an snippet

 'Quantity' =>['required',  function ($attribute, $value, $fail) {

            if (is_array($value)&&!array_filter($value)) {
                $fail('The '.$attribute.' is invalid.');
            }
        },],

For example ,Here data contain all null values in array so throwing error

Array
(
    [data] => Array
        (
            [0] => 
            [1] => 
        )

)

Valdiation Error

Illuminate\Support\MessageBag Object
(
    [messages:protected] => Array
        (
            [data] => Array
                (
                    [0] => The data is invalid.
                )

        )

    [format:protected] => :message
)
Sign up to request clarification or add additional context in comments.

Comments

1

Use custom validation rules, and make that rule:

<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class NotEmptyArrayRule implements Rule
{

    private string $key;
    private string $value;

    public function passes($attribute, $value): bool
    {
        // remove null items from collection.
        $arr = collect($value)->filter(); 

        return $arr->isNotEmpty();
    }

    public function message(): string
    {
        return 'Input array is empty.';
    }
}

Then use rule:

'Item' => [
  'exclude_if:Quantity.*,null',
  'exclude_if:Unit_Price.*,null',
  'bail',
  'nullable',
  new NotEmptyArrayRule(),
]

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.