简体   繁体   English

如何在PHP中使用required_if Laravel验证

[英]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. 如果重量不为空,我想进行验证,然后需要shipping_cost和虎钳。

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. 我不确定您是否可以在没有特定值的情况下使用required_if。

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';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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