Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sunilbfcj's avatar

required if array of checkbox is checked

I am creating variants for products. For each variant, if manage stock checkbox is checked then the user must enter the stock but at the time of validation stock quantity asked for each variant.

$validate['variant_manage_stock.*'] = ['sometimes', 'accepted'];
$validate['variant_stock_quantity.*'] = ['required_if:variant_manage_stock.*,1', 'numeric'];
$validate['variant_allow_backorder.*'] = ['required', 'in:no,notify,yes'];
$validate['variant_stock_threshold.*'] = ['required_if:variant_manage_stock.*,1', 'numeric', 'min:1', 'lt:variant_stock_quantity.*'];

I also tried.

$validate['variant_manage_stock.*'] = ['sometimes', 'accepted'];
$validate['variant_stock_quantity.*'] = ['required_with:variant_manage_stock.*,on', 'numeric'];
$validate['variant_allow_backorder.*'] = ['required', 'in:no,notify,yes'];
$validate['variant_stock_threshold.*'] = ['required_with:variant_manage_stock.*,on', 'numeric', 'min:1', 'lt:variant_stock_quantity.*'];

validation errors:- The variant_stock_quantity.0 must be a number.

The variant_stock_quantity.1 must be a number. The variant_stock_quantity.2 must be a number. The variant_stock_quantity.3 must be a number. The variant_stock_quantity.4 must be a number. The variant_stock_quantity.5 must be a number. The variant_stock_threshold.0 must be less than variant_stock_quantity.0. The variant_stock_threshold.1 must be less than variant_stock_quantity.1. The variant_stock_threshold.2 must be less than variant_stock_quantity.2. The variant_stock_threshold.3 must be less than variant_stock_quantity.3. The variant_stock_threshold.4 must be less than variant_stock_quantity.4. The variant_stock_threshold.5 must be less than variant_stock_quantity.5.

0 likes
3 replies
frankincredible's avatar

Please post the JSON of your request params so we can get a better idea of what the structure of your Request looks like.

sunilbfcj's avatar

Thanks for the reply.

	
_token	"3AtIxxNERtxJLigJhKIagpYZpHIJKP0szZFvJgEC"
product_title	null
product_description	null
product_type	"variable"
regular_price	null
sale_price	null
product_url	null
button_text	"Buy now"
product_sku	null
stock_quantity	null
allow_backorder	"no"
stock_threshold	"2"
product_weight	null
product_length	null
product_width	null
product_height	null
shipping_class	"2"
tax_class	"1"
add_new_attribute	null
attribute_name	
0	"size"
1	"color"
attribute_value	
size	
0	"L"
1	"XL"
2	"XXL"
color	
0	"Black"
1	"Blue"
add-attribute_value	null
variant_regular_price	
0	"500"
1	null
2	null
3	null
4	null
5	"600"
variant_sale_price	
0	"300"
1	null
2	null
3	null
4	null
5	"500"
variant_product_sku	
0	null
1	null
2	null
3	null
4	null
5	null
variant_stock_quantity	
0	null
1	null
2	null
3	null
4	null
5	null
variant_allow_backorder	
0	"no"
1	"no"
2	"no"
3	"no"
4	"no"
5	"no"
variant_stock_threshold	
0	null
1	null
2	null
3	null
4	null
5	null
variant_product_weight	
0	null
1	null
2	null
3	null
4	null
5	null
variant_product_length	
0	null
1	null
2	null
3	null
4	null
5	null
variant_product_width	
0	null
1	null
2	null
3	null
4	null
5	null
variant_product_height	
0	null
1	null
2	null
3	null
4	null
5	null
variant_shipping_class	
0	null
1	null
2	null
3	null
4	null
5	null
variant_tax_class	
0	null
1	null
2	null
3	null
4	null
5	null
product_short_description	null
product_status	"public"

my html code

<div class="form-group col-12">
    <label class="checkbox d-block pl-3" style="padding-top: 2px;"><input type="checkbox" name="variant_manage_stock[${index}]" value="1" class="selectable variant_manage_stock"><span class="checkmark"></span>Manage Stock (Enable stock management at product level)</label>
</div>

<div class="form-group col-12 col-md-4 type-stock">
    <label>Stock quantity <i class="fas fa-question-circle" data-toggle="tooltip" data-placement="bottom" title="Stock quntity. If this is a variable product this value will be used to control stock for all variations, unless you define stock at variation level."></i></label>
    <input type="number" name="variant_stock_quantity[${index}]" class="form-control" value="">

    <span class="invalid-feedback" role="alert">
        <strong></strong>
    </span>
</div>

<div class="form-group col-12 col-md-4 type-stock">
    <label>Allow Backorder <i class="fas fa-question-circle" data-toggle="tooltip" data-placement="bottom" title="If managing stock, this controls whether or not backorders are allowed. If enabled, stock quantity can go below 0."></i></label>
    <select name="variant_allow_backorder[${index}]" class="form-control">
        <option value="no">Do not allow</option>
        <option value="notify">Allow, but notify customer</option>
        <option value="yes">Allow</option>
    </select>

    <span class="invalid-feedback" role="alert">
        <strong></strong>
    </span>
</div>

<div class="form-group col-12 col-md-4 type-stock">
    <label>Low stock threshold <i class="fas fa-question-circle" data-toggle="tooltip" data-placement="bottom" title="When product stock reaches this amount you will be notified by email."></i></label>
    <input type="text" name="variant_stock_threshold[${index}]" class="form-control" value="">

    <span class="invalid-feedback" role="alert">
        <strong></strong>
    </span>
</div>

Now I want to validate 'variant_stock_threshold' field it should be required and less than if 'variant_stock_quantity' is not null or till it not fullfill.

sunilbfcj's avatar
sunilbfcj
OP
Best Answer
Level 1

my rule for validation should be:

$validate['variant_manage_stock.*'] = ['sometimes', 'accepted'];
$validate['variant_stock_quantity.*'] = ['nullable', 'numeric', 'min:2'];
$validate['variant_allow_backorder.*'] = ['required', 'in:no,notify,yes'];
$validate['variant_stock_threshold.*'] = ['nullable', 'numeric', 'min:1', 'lt:variant_stock_quantity.*'];

Thanks to all laracasters for their valuable time.

Please or to participate in this conversation.