So I have this form with these fields

{{ Form::open(array('url' => 'user', 'id' => 'user_create_form')) }}

    <div class="form-input-element">
        <label for="facebook_id">ID Facebook</label>
        {{ Form::text('facebook_id', Input::old('facebook_id'), array('placeholder' => 'ID Facebook')) }}
    </div>

    <div class="form-input-element">
        <label for="twitter_id">ID Twitter</label>
        {{ Form::text('twitter_id', Input::old('twitter_id'), array('placeholder' => 'ID Twitter')) }}
    </div>

    <div class="form-input-element">
        <label for="instagram_id">ID Instagram</label>
        {{ Form::text('instagram_id', Input::old('instagram_id'), array('placeholder' => 'ID Instagram')) }}
    </div>

{{ Form::close() }}

I'd like to tell Laravel that at least one of these fields is required. How do I do that using the Validator?

$rules = array(
    'facebook_id'                   => 'required',
    'twitter_id'                    => 'required',
    'instagram_id'                  => 'required',
);
$validator = Validator::make(Input::all(), $rules);

推荐答案

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(
    'facebook_id' => 'required_without_all:twitter_id,instagram_id',
    'twitter_id' => 'required_without_all:facebook_id,instagram_id',
    'instagram_id' => 'required_without_all:facebook_id,twitter_id',
);
$validator = Validator::make(Input::all(), $rules);

Laravel相关问答推荐

子目录中具有Vue实例的Laravel不起作用

Laravel 联合和分页

将 ID 传递给资源控制器进行编辑

嵌套数组请求的 Laravel 验证,其中如果整数之一为零,则另一个应大于零

如何从@foreach 循环中捕获所有数据并将其传递给 值? - Laravel

Laravel 连接表

向 Laravel 模型查询添加计算字段

给定的角色或权限应该使用 guard `` 而不是 `web`. -Laravel

Laravel 5.4 存储:下载文件.文件不存在,但显然存在

Laravel 查询构建器 - 如何按别名分组,或进行原始 groupBy

Laravel 将参数从路由传递到过滤器

laravel 4 - 如何限制(采取和跳过)Eloquent的 ORM?

如何使用外部图像生成 html div 的 screenshot截图?

laravel:Eloquent的例子将数据插入数据库

如何使用命令行手动运行 laravel/lumen 作业(job)

Laravel 更新语法 - 使用数组更新记录

在 Laravel 中将 Eloquent 导出到 Excel 时如何包含列标题?

如何在 Eloquent Orm 中实现自引用(parent_id)模型

Laravel 集合计数结果

如何访问 Laravel 集合中的第 n 个项目?