php Laravel“至少一个”字段需要验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23401365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:42:14  来源:igfitidea点击:

Laravel "At Least One" Field Required Validation

phplaravellaravel-4

提问by William Wino

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?

我想告诉 Laravel 至少需要这些字段之一。我如何使用验证器做到这一点?

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

回答by Sam

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

尝试检查一下required_without_all:foo,bar,...,看起来应该可以为您完成。引用他们的文档:

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

回答by William Wino

**For automatic validation**

$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',
);
$message = array(
    'facebook_id' => 'The facebook field is required when none of twitter / instagram are present.',
    'twitter_id' => 'The twitter field is required when none of facebook / instagram are present.',
    'instagram_id' => 'The instagram field is required when none of twitter / facebook are present.');

$validator = Validator::make(Input::all(), $rules, $message)->validate();

回答by Koko Monsef

use this code i already made it

使用此代码我已经做到了

php artisan make:rule RequiredWithout

use Illuminate\Contracts\Validation\Rule;

class RequiredWithout implements Rule
{
    private $without = [];
    private $current_attribute = "";
    private $no_need = "";

    public function __construct($without)
    {
        if(is_string($without))
        {
            $this->without = [$without];
        }
        elseif(is_array($without))
        {
            $this->without = $without;
        }
        else
           throw new \Exception('$without must be array or string');
    }

    public function passes($attribute, $value)
    {
        $this->current_attribute = $attribute;
        $requests = request()->all();
        foreach ($this->without as $WO_i)
        {            
            if(array_key_exists($WO_i, $requests))
            {
                $this->no_need = $WO_i;
                return false;
            }
        }
        return true;
    }

    public function message()
    {
        return "the $this->current_attribute Field and $this->no_need Shouldn't be Exist in this request together";
    }
}

and then in the controller use this

然后在控制器中使用这个

'country_id' => ['nullable', 'exists:countries,id', new ValidCountryID(), new RequiredWithout('country_code')],
'country_code' => ['nullable', 'exists:countries,IsoCountryCode', new ValidCountryID(), new RequiredWithout('country_id')],