【问题标题】:Laravel Validation : Validate only if fields are presentLaravel 验证:仅在字段存在时验证
【发布时间】:2019-11-20 10:29:20
【问题描述】:

因此,如果满足某些条件,我将隐藏一些字段。现在我的验证有问题。这是我所拥有的:

blade.php

<div class="{{ $room['show_checkin_out'] ? '' : 'hide-fields' }}">
    <div class="form-group check-in-dtls">
        <label for="before_checkin">@lang('before_checkin')</label>
        <input type="text" class="form-control" id="before_checkin" name="before_checkin" placeholder="@lang('before_checkin')" value="{{ old('before_checkin', '') }}">
        @if ($errors->has('before_checkin'))
            <div class="form-group">
                <p class="text-danger">{{ $errors->first('before_checkin') }}</p>
            </div>
        @endif
    </div>
</div>

验证文件

'before_checkin' => ['sometimes', 'required', 'max:255'],

css

.hide-fields{
    display: none;
}

我希望只有在显示且我的验证效果不佳时才需要这些字段。对于这个,最好的方法是什么?

【问题讨论】:

  • 如果存在hide-fields 类,您可以通过从输入字段中删除name 属性来使用jquery 来解决问题

标签: validation laravel-5


【解决方案1】:

我可以建议 3 种方法。

  1. 如果$room['show_checkin_out'] 是虚假的,则根本不要添加此验证规则。

种类:

$rules = [...];
if ($room['show_checkin_out']) {
    $rules['before_checkin'] = ['sometimes', 'required', 'max:255'];
}
  1. 使用“必需的”规则 (https://laravel.com/docs/5.8/validation#rule-required-with)。 只需在必要时将 show_checkin_out 作为隐藏参数传递,并仅在存在 show_checkin_out 时才需要 before_checkin。

添加到您的表单中:

@if($room['show_checkin_out'])
    <input type="hidden" name="show_checkin_out" value="1"/>
@endif

这样修改验证规则:

'before_checkin' => ['required_with:show_checkin_out', 'sometimes', 'max:255'],
  1. 可能是最好的一个。既然你已经有了sometimes 规则,那么当show_checkin_out 为假时,基本上不输出before_checkin 字段:
@if($room['show_checkin_out'])
    <div class="form-group check-in-dtls">
        <label for="before_checkin">@lang('before_checkin')</label>
        <input type="text" class="form-control" id="before_checkin" name="before_checkin" placeholder="@lang('before_checkin')" value="{{ old('before_checkin', '') }}">
        @if ($errors->has('before_checkin'))
            <div class="form-group">
                <p class="text-danger">{{ $errors->first('before_checkin') }}</p>
            </div>
        @endif
    </div>
@endif

【讨论】:

    【解决方案2】:

    您可以这样做,而不是设置display:none 属性。

    @if(isset($room['show_checkin_out']))
    <div class="">
      <div class="form-group check-in-dtls">
        <label for="before_checkin">@lang('before_checkin')</label>
          <input type="text" class="form-control" id="before_checkin" name="before_checkin" placeholder="@lang('before_checkin')" value="{{ old('before_checkin', '') }}">
         @if ($errors->has('before_checkin'))
            <div class="form-group">
                <p class="text-danger">{{ $errors->first('before_checkin') }}</p>
             </div>
         @endif
       </div>
     </div>
    @endif
    

    如果它设置为$room['show_checkin_out'],它将创建该部分并进行验证,否则它不会创建该部分并且您的验证将按照您的意愿进行。

    【讨论】:

      【解决方案3】:

      所以最后,我用这种方法来完成验证工作。

      刀片

      .... name="{{ $room['show_checkin_out']==1 ? 'before_checkin' : '' }}"
      

      验证

      $ret = [
             // other validation
             ];
      
      if (\Request::has('before_checkin')) {
          $ret['before_checkin'] = ['required', 'sometimes', 'max:255'];
      }
      

      这要简单得多。

      【讨论】:

        猜你喜欢
        • 2016-01-11
        • 2014-01-30
        • 2020-04-26
        • 1970-01-01
        • 2018-10-06
        • 2014-03-17
        • 2014-10-07
        • 2021-04-13
        • 1970-01-01
        相关资源
        最近更新 更多