【问题标题】:Laravel "At Least One" Field Required ValidationLaravel“至少一个”字段需要验证
【发布时间】:2014-06-17 13:22:49
【问题描述】:

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

{{ 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,这些字段中至少有一个是必需的。我如何使用验证器做到这一点?

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

【问题讨论】:

    标签: php laravel laravel-4


    【解决方案1】:

    试试看required_without_all:foo,bar,...,看起来应该可以帮到你。引用他们的文档:

    只有在所有其他指定字段都不存在时,才必须存在正在验证的字段。


    示例:

    $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 太棒了!
    • 太棒了,拯救了我的一天。
    • 这可行,但问题是消息的重复。如果你想避免这种情况,你可以尝试stackoverflow.com/a/65806163/959260的方式
    • 正在寻找这个! Laravel 太棒了!谢谢!
    【解决方案2】:
    **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();
    

    【讨论】:

      【解决方案3】:

      使用我已经完成的代码

      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";
          }
      }
      

      然后在控制器中使用这个

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

      【讨论】:

        猜你喜欢
        • 2011-07-21
        • 2016-06-29
        • 1970-01-01
        • 2020-08-20
        • 2023-03-10
        • 2017-06-02
        • 2017-07-05
        • 2013-01-05
        相关资源
        最近更新 更多