gpt4 book ai didi

Laravel 验证 如果勾选复选框,则需要输入文本?

转载 作者:行者123 更新时间:2023-12-02 10:10:59 25 4
gpt4 key购买 nike

我一直在阅读 Laravel 验证文档。我不清楚如何结合两个规则。

例如:

<input type="checkbox" name="has_login" value="1">

<input type="text" name="pin" value="">

如果勾选has_login复选框,则需要输入文本pin值。

如果未勾选has_login,则不需要输入文本pin

Laravel 验证:

public function rules()
{
return [
'has_login' => 'accepted',
'pin' => 'required',
];
}

最佳答案

使用 required_with 或 required_if

required_with:foo,bar

The field under validation must be present and not empty only if any of the other specified fields are present.

return [
'has_login' => 'sometimes',
'pin' => 'required_with:has_login,on',
];

--

required_if:anotherfield,value

The field under validation must be present and not empty if the anotherfield field is equal to any value.

return [
'has_login' => 'sometimes',
'pin' => 'required_if:has_login,on',
];

--

https://laravel.com/docs/5.2/validation

--

此外,如果未选中 has_login 复选框,则不会作为表单提交的一部分发送

关于Laravel 验证 如果勾选复选框,则需要输入文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38525613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com