简体   繁体   中英

How to use required_if Laravel Validation in php

I want to give validation if weight is not empty then shipping_cost required and vise verse.

array('weight' => 'numeric|required_if:shipping_cost,value',
'shipping_cost' => 'numeric|required_if:weight,value')

What will be the value for 'weight, value ' and 'shipping_cost, value ' ?

I am not sure you can use the required_if without a specific value.

You can use a condition for that.

$rules = [
    'weight' => ['numeric'],
    'shipping_cost' => ['numeric']
]

// Shipping costs requires weight, and weight requires shipping cost
if ( ! empty(Input::get('weight')) or ! empty(Input::get('shipping_cost')))
{
    $weight['shipping_cost'][] = 'required';
    $rules['weight'][] = 'required';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM