admin管理员组

文章数量:1312753

The following is from the request

[
    'coupons'                 => 'nullable|array|min:1',
    'coupons.*.code'          => [
                'required', 'string', new CheckCouponValidityForOrder(
                    $this->input('coupons.*.code'), // how to current value at this index?
                    $this->input('coupons.*.product_ids')
                ),
            ],
]

How do I get the code for the current index?

The following is from the request

[
    'coupons'                 => 'nullable|array|min:1',
    'coupons.*.code'          => [
                'required', 'string', new CheckCouponValidityForOrder(
                    $this->input('coupons.*.code'), // how to current value at this index?
                    $this->input('coupons.*.product_ids')
                ),
            ],
]

How do I get the code for the current index?

Share Improve this question asked Jan 31 at 16:04 Md. A. ApuMd. A. Apu 1,2502 gold badges19 silver badges40 bronze badges 3
  • Please share the code for CheckCouponValidityForOrder – Emeka Mbah Commented Feb 3 at 23:22
  • it's a custom Rule it takes coupon code & product IDs like [1,5] & does the validation in the wanted way. – Md. A. Apu Commented Feb 4 at 10:35
  • Great, are you doing the validation inside a FormRequest ? – Emeka Mbah Commented Feb 4 at 12:31
Add a comment  | 

2 Answers 2

Reset to default 0

Here is an idea that has not fully tested

Will be slightly different if you are doing this inside a FormRequest

    $data = $request->all() ?? [
        // sample data
        'coupons' => [
            [
                'code' => 'coupon1',
                'product_ids' => [1, 2, 3]
            ],
            [
                'code' => 'coupon2',
                'product_ids' => [4, 5, 6]
            ]
        ]
    ];

    $rules = [
        'coupons'                 => 'nullable|array|min:1',
        'coupons.*.code'          => [
            'required', 'string', new CheckCouponValidityForOrder($data),
        ],
    ];

    $v = Validator::make($data, $rules);
<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use Illuminate\Support\Facades\Request;

class CheckCouponValidityForOrder implements Rule
{
    private $data;

    /**
     * Create a new rule instance.
     *
     * @return void
     */
    public function __construct(array $data = [])
    {
        $this->data = $data ?: Request::all();
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        //  "coupons.0.code" -> "0"
        preg_match('/\d+/', $attribute, $matches);
        $index = $matches[0] ?? null;

        // Ensure index exists
        if ($index === null) {
            return false;
        }

        $productIds = $this->data['coupons'][$index]['product_ids'] ?? [];
        $code = $this->data['coupons'][$index]['code'] ?? $value;

        // Debugging Output
        dd($value, $attribute, $index, $productIds, $code);
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return 'The validation error message.';
    }
}

When you plan to check multiple coupons, you can use the forEach method to apply validation rules to each coupon.

public function rules()
{
    return [
        'coupons' => 'nullable|array|min:1',
        'coupons.*.code' => \Illuminate\Validation\Rule::forEach(function ($value, $attribute, $data) {
            return [
                'required',
                'string',
                new CheckCouponValidityForOrder(
                    $value, // Current 'code' value
                    $data['coupons'][explode('.', $attribute)[1]]['product_ids'] ?? []
                ),
            ];
        }),
    ];
}

本文标签: How to get current value in Laravel form request for array fieldStack Overflow