Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么可空验证在laravel 5.4中不起作用_Php_Laravel_Laravel 5 - Fatal编程技术网

Php 为什么可空验证在laravel 5.4中不起作用

Php 为什么可空验证在laravel 5.4中不起作用,php,laravel,laravel-5,Php,Laravel,Laravel 5,我已经能够找到问题所在,问题出现在我的迁移表中,我没有将网站字段设置为null(),因此当我尝试向其中插入空字符串时,它就成了一个问题。 我的视图表单中有一个名为“网站”的字段,我将此字段设置为可选字段。这意味着任何用户都可以决定输入他们的网站,如果他们有一个,或者让它空着 <div class="form-group{{ $errors->has('website') ? ' has-error' : '' }}"> <label for="websi

我已经能够找到问题所在,问题出现在我的迁移表中,我没有将网站字段
设置为null()
,因此当我尝试向其中插入空字符串时,它就成了一个问题。

我的视图表单中有一个名为“网站”的字段,我将此字段设置为可选字段。这意味着任何用户都可以决定输入他们的网站,如果他们有一个,或者让它空着

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

如果希望字段是可选的,那么不必在验证中输入“required”

因此,在您的情况下,您可以尝试:url | max:100


这仅在您将任何字段放入其中时验证该字段。

如果您希望字段是可选的,则不必在验证中输入“必需”

因此,在您的情况下,您可以尝试:url | max:100


仅当您在字段中添加任何内容时,才验证该字段。

尝试此操作;网站“=>”有时| url |字符串|最大值:100|nullable@lea_Kelmedi,谢谢您的回复,但是我现在尝试了,我得到了这个网站格式无效的错误消息。你可以尝试“url | max:100”实际上你正在验证为url,所以null不起作用。只是测试了这个和它的作用:“url”=>“有时| null | url | string | max:100”试试这个;网站“=>”有时| url |字符串|最大值:100|nullable@lea_Kelmedi,谢谢您的回复,但是我现在尝试一下,我得到了这个网站格式无效的错误消息。你可以尝试“url | max:100”实际上你正在验证为url,所以nullable不起作用只是测试了这个和它的作用:“url”=>“有时| nullable | url | string | max:100”他从来没有在他的任何验证中使用过它?这只是对我认为string的解释在这个验证中不需要nullable。他从来没有在他的任何验证中加入required?这只是一个解释,我认为string和nullable在这个验证中是不需要的。
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'],