Laravel“至少一个"字段必填验证

2022-01-08 00:00:00 php laravel laravel-4

所以我有这个包含这些字段的表单

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() }}

我想告诉 Laravel,这些字段中至少有一个是必需的.我如何使用验证器做到这一点?

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);

推荐答案

尝试检查 required_without_all:foo,bar,...,看起来应该为你做.引用他们的文档:

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.

<小时>

示例:

$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);

相关文章