Laravel at least one field is required

大城市里の小女人 提交于 2019-12-12 12:47:42

问题


I have two field billable_option and billable_option_yes. I want to validate at least one field is mandatory. I have tried:

$this->validate($request,[
    'billable_option'=>'required'|'billable_option_yes'=>'required',
]); 

I want to check at least one field is required in Laravel-5.3.


回答1:


you can try with the required_without.

$rules = array(
   'billable_option' => 'required_without:billable_option_yes',
   'billable_option_yes' => 'required_without:billable_option'
);



回答2:


Try checking out required_without_all:foo,bar,..., it looks like that should do it for you. To quote their documentation:

The field under validation must be present only when the all of the other specified fields are not present.

Example:

$rules = array(
   'billable_option' => 'required_without_all:billable_option_yes',
   'billable_option_yes' => 'required_without_all:billable_option'
);
$validator = Validator::make(Input::all(), $rules);

Hope this helps you!



来源:https://stackoverflow.com/questions/46909982/laravel-at-least-one-field-is-required

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!