Laravel验证所需的 [英] Laravel Validation of Required With

查看:81
本文介绍了Laravel验证所需的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的控制器中为我的请求设置验证,并且试图弄清楚我如何才能使地址,城市,州,邮编都相互依赖,只要有一个值那么所有的值都必须有一个值,address2是唯一一个独立的值,但仅取决于该地址.由于某种原因,这是行不通的.例如,如果提交了城市,则验证通过.所以我不太明白我在做什么错.

I'm attempting to set up validation for my request in my controller and I'm trying to figure out how I can have address, city, state, zip all depend on each other as far as if there's a value for one then there must be a value for all of them, address2 is the only one that is stand-alone but depends ONLY on the address. For some reason, this doesn't work. For example, if the city is submitted then validation passes. So I don't quite understand what I"m doing wrong.

'address' => 'required_with_all:city,state,zip|string|nullable',
'address2' => 'required_with:address|string|nullable',
'city' => 'required_with_all:address,state,zip|string|nullable',
'state' => 'required_with_all:address,city,zip|string|size:2|nullable',
'zip' => 'required_with_all:address,city,state|integer|digits:5|nullable'

推荐答案

requiredwith required_with_all 不适用于您解释它们的方式. required_with_all 表示如果以下所有字段具有值,则此字段为必填字段.例如, required_with_all:address,state,zip 表示如果地址,州和邮政编码都具有值,则城市是必需的".

required_with and required_with_all don't work in the way you're interpreting them. required_with_all means this field is required if all of the following fields have values. For example, required_with_all:address,state,zip means "city is required if address and state and zip all have values".

您正在尝试实现:

  • 如果 address 有一个值,则需要 city state zip
  • 如果 address2 有一个值,则需要 address
  • 如果 city 具有值,则需要地址 zip
  • 如果具有值,则需要地址城市邮政编码
  • 如果 zip 具有值,则需要地址城市
  • If address has a value, city, state and zip are required
  • If address2 has a value, address is required
  • If city has a value, address, state and zip are required
  • If state has a value, address, city and zip are required
  • If zip has a value, address, city and state are required

您要查找的规则 required_with ,但是逻辑不同.您可以通过在一个字段上锚定来有效地使用 required_with 规则,例如:锚定到 address ,并且您的英语规则可以是如果 address 具有值,然后需要城市,州和邮政编码,或者,如果 city state zip 具有值,则 address 是必需的",可以构造为:

The rule you're looking for then is required_with but the logic is different. You can effectively use the required_with rule by anchoring on one field, e.g: anchor against address and your rule in English can be "if address has a value, then city, state and zip are required, or if city, state or zip have a value then address is required" which can be constructed as:

$this->validate($request, [
    'address' => 'required_with:city,state,zip|string|nullable',
    'city' => 'required_with:address|string|nullable',
    'state' => 'required_with:address|string|size:2|nullable',
    'zip' => 'required_with:address|integer|digits:5|nullable'
]);

对于 address2 ,您的规则是如果 address2 具有值,则需要 address "(它将级联成导致 state city zip 也是必需的).再次使用 required_with 构造,我们在 address 上设置 required_with :

And for address2 your rule is "address is required if address2 has a value" (which will cascade into causing state, city and zip to be required too). This is again constructed using required_with, we set required_with on address:

$this->validate($request, [
    'address' => 'required_with:city,state,zip,address2|string|nullable',
    'address2' => 'string|nullable',
    'city' => 'required_with:address|string|nullable',
    'state' => 'required_with:address|string|size:2|nullable',
    'zip' => 'required_with:address|integer|digits:5|nullable'
]);

此答案的先前版本中有一个错误,现已修复.

there was an error in a previous version of this answer, it is now fixed.

这篇关于Laravel验证所需的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆