簡體   English   中英

為什么可空驗證在laravel 5.4中不起作用

[英]why is nullable validation not working in laravel 5.4

我能夠找出問題所在,而問題出在我的遷移表中,但我沒有將網站字段設置為nullable()因此當我嘗試向其中插入空字符串時,這便成為問題。

我的視圖表單中有一個名為“網站”的字段,並且我將此字段設為可選。 這意味着任何用戶都可以決定是否輸入自己的網站,也可以將其留空。

 <div class="form-group{{ $errors->has('website') ? ' has-error' : '' }}">
        <label for="website" class="col-md-4 control-label">Website</label>

              <div class="col-md-6">
                  <input id="website" type="text" class="form-control" name="website" value="{{ old('website') }}" placeholder="ex. www.mytruck.com" autofocus>

                      @if ($errors->has('website'))
                          <span class="help-block">
                              <strong>{{ $errors->first('website') }}</strong>
                           </span>
                        @endif
               </div>
 </div>

從控制器我有此驗證代碼

protected function validator(array $data)
{
    return Validator::make($data, [
         'website' => 'url|string|max:100|nullable',
     ]);
}


  protected function create(array $data)
   {
    return User::create([
           'website' => $data['website'],

如果希望字段為可選,則不必在驗證中輸入“ required”。

因此,您可以嘗試:url | max:100

僅當您在其中輸入任何內容時,才驗證該字段。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM