Laravel “At Least One” Field Required Validation
所以我有这些字段的表格
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | {{ Form::open(array('url' => 'user', 'id' => 'user_create_form')) }} <label for="facebook_id">ID Facebook</label> {{ Form::text('facebook_id', Input::old('facebook_id'), array('placeholder' => 'ID Facebook')) }} <label for="twitter_id">ID Twitter</label> {{ Form::text('twitter_id', Input::old('twitter_id'), array('placeholder' => 'ID Twitter')) }} <label for="instagram_id">ID Instagram</label> {{ Form::text('instagram_id', Input::old('instagram_id'), array('placeholder' => 'ID Instagram')) }} {{ Form::close() }} |
我想告诉Laravel,至少需要这些字段之一。如何使用验证器做到这一点?
1 2 3 4 5 6 | $rules = array( 'facebook_id' => 'required', 'twitter_id' => 'required', 'instagram_id' => 'required', ); $validator = Validator::make(Input::all(), $rules); |
尝试签出
The field under validation must be present only when the all of the other specified fields are not present.
例子:
1 2 3 4 5 6 | $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); |
1 2 3 4 5 6 7 8 9 10 11 12 13 | **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(); |
使用此代码,我已经做到了
1 | php artisan make:rule RequiredWithout |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | use Illuminate\\Contracts\\Validation\ ule; 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"; } } |
,然后在控制器中使用此
1 2 | 'country_id' => ['nullable', 'exists:countries,id', new ValidCountryID(), new RequiredWithout('country_code')], 'country_code' => ['nullable', 'exists:countries,IsoCountryCode', new ValidCountryID(), new RequiredWithout('country_id')], |