【问题标题】:Laravel validation ErrorException - htmlentities() expects parameter 1 to be string, array givenLaravel 验证 ErrorException - htmlentities() 期望参数 1 是字符串,给定数组
【发布时间】:2015-04-30 08:57:02
【问题描述】:

所以我是 laravel 5 的新手,刚刚开始编写自己的应用程序。

当我只是为 invoices.create 控制器调用路由时,视图生成就好了。

当我从 invoices.store 控制器重定向到同一视图时,我收到以下错误消息。

我的控制器,看起来像这样。

我猜它与传递给控制器​​的参数冲突,查看消息。但我似乎找不到问题所在以及如何解决它。

编辑: 这是风景……

@extends('app')

@section('content')

<div class="row">
    <div class="col-md-10 col-md-offset-1">
        <div class="panel panel-default">
            <div class="panel-heading">New Invoice</div>
            <div class="panel-body">
                @if ($errors->has())
                <div class="alert alert-danger">
                    @foreach ($errors->all() as $error)
                        {{ $error }}<br>        
                    @endforeach
                </div>
                @endif

                {!! Form::model(new App\Invoice, array('route' => array('invoices.store'))) !!}
                    <div class="form-group">
                        {!! Form::label('customer_id', 'Choose customer:') !!}
                        {!! Form::select('customer_id', array(null => ' - Please select customer - ') + $customers, $selected_customer, array('class'=>'form-control')) !!}
                    </div>
                    <div class="row" id="company_row">
                        <div class="col col-sm-6">
                            <div class="form-group">
                                {!! Form::label('number', 'Number:') !!}
                                {!! Form::text('number', $invoice_number, array('class'=>'form-control')) !!}
                            </div>
                        </div>
                        <div class="col col-sm-6">
                            <div class="form-group">
                                {!! Form::label('date', 'Date:') !!}
                                {!! Form::text('date', null, array('class'=>'form-control')) !!}
                            </div>
                        </div>
                    </div>
                    <div class="panel panel-default">
                        <div class="panel-heading">Invoice items</div>
                        <div class="panel-body">
                            <div class="table-responsive">
                                <table class="table table-bordered" id="table-invoice-items">
                                    <thead>
                                        <tr>
                                            <th>Amount</th>
                                            <th>Description</th>
                                            <th>Price</th>
                                            <th>Total</th>
                                            <th>VAT</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                            <td class="cell-width-5">{!! Form::text('items[amount][]', null, array('class'=>'form-control')) !!}</td>
                                            <td>{!! Form::text('items[description][]', null, array('class'=>'form-control')) !!}</td>
                                            <td class="cell-width-6">{!! Form::text('items[price][]', null, array('class'=>'form-control')) !!}</td>
                                            <td class="cell-width-5"></td>
                                            <td class="cell-width-12">{!! Form::text('items[vat][]', null, array('class'=>'form-control')) !!}</td>
                                            <td class="cell-center"><span class="glyphicon glyphicon-trash pointer" aria-hidden="true"></span></td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                            <button type="button" class="btn btn-primary" id="add-invoice-item">New Item</button>
                        </div>
                    </div>
                    <div class="form-group">
                        {!! Form::label('note', 'Note:') !!}
                        {!! Form::textarea('note', null, array('class'=>'form-control')) !!}
                    </div>
                    <div class="form-group">
                        {!! Form::submit('Create Invoice', ['class'=>'btn primary']) !!}
                    </div>
                {!! Form::close() !!}
            </div>
        </div>
    </div>
</div>

<script>
$(document).ready(function(){
    $("input#date").datepicker({format: 'dd-mm-yyyy'});

    $("button#add-invoice-item").click(function(){
        var table_row = $("table#table-invoice-items > tbody > tr:first").clone();
        table_row.find('input:text').val('');
        $("table#table-invoice-items > tbody > tr:last").after(table_row);
    });
});
</script>

@endsection

好的,所以我设法缩小了问题范围。它与数组作为输入字段的名称有关,当我删除此表行时..错误消失了。 但是我该如何解决呢?

<tr>
    <td class="cell-width-5">{!! Form::text('items[amount][]', null, array('class'=>'form-control')) !!}</td>
    <td>{!! Form::text('items[description][]', null, array('class'=>'form-control')) !!}</td>
    <td class="cell-width-6">{!! Form::text('items[price][]', null, array('class'=>'form-control')) !!}</td>
    <td class="cell-width-5"></td>
    <td class="cell-width-12">{!! Form::text('items[vat][]', null, array('class'=>'form-control')) !!}</td>
    <td class="cell-center"><span class="glyphicon glyphicon-trash pointer" aria-hidden="true"></span></td>
</tr>

感谢任何帮助!

【问题讨论】:

  • 这看起来问题可能出在视图中。请通过编辑您的问题发布该代码。并且不要将其作为图像发布,而只是(正确格式化)文本。这样可以保证它在许多设备上的可搜索性和可读性。
  • 按照您的要求编辑了帖子
  • invoice_number 是否可能是一个数组而不仅仅是一个数字?在控制器中尝试dd($invoice_number)。
  • 您似乎没有在store() 方法中实际设置invoice_number,但您在create() 方法中设置了这可能是它在那里工作的原因。
  • @JoelHinz,我不太明白...有一个重定向要创建,所以创建控制器被再次调用?

标签: php laravel-5


【解决方案1】:

编辑:这是 Laravel 表单构建器的已知问题:https://github.com/laravel/framework/issues/2243

它会阻止您使用数组作为 Form::text() 方法调用的名称参数。我认为这与表单如何从 HTML 中的给定模型分配值有关。

我认为您将不得不为此部分编写基本的 HTML 表单内容。

【讨论】:

  • 请查看我的帖子底部,我找到了问题所在。我只是不知道如何解决它。
  • 抱歉,忽略了你正在调试的笔记。我对这个问题做了更多的挖掘,发现了一些东西。查看我的编辑。
  • 感谢您查找此内容。然而,这是一个可以追溯到 2013 年的问题,来自该链接。我想看看如何在 laravel 表单构建器中完成多行以及如何验证它们,但我似乎找不到任何东西。也许我只是在搜索中使用了错误的词,idk。
  • 我链接到的讨论中的最后一条评论来自 Taylor Otwell,创建者或 Laravel。我相信这个问题仍然没有解决,他们没有计划在未来实施修复。事实上,Laravel 5 默认不包含表单构建器。
  • 它实际上不是输入文件,使用数组时会出错。
【解决方案2】:

嗯,我已经设法重现了这个问题。我有一个文件数组字段:

<input type="file" name="files[]" />

而且我也在尝试像这样获取旧值字段:

<input type="file" name="files[]" value="{{ old('files') }}" />

我发现由于某种原因,这会导致 htmlentities() 出现错误。如果有人有时间检查这个案例,请写下你的结果。

【讨论】:

    【解决方案3】:

    不要在表单字段名称中使用[],而是分配一个整数[1]并递增。

    例如,这个:

    {!! Form::text('items[amount][]', null, array('class'=>'form-control')) !!}
    

    应该是:

    {!! Form::text('items[amount][1]', null, array('class'=>'form-control')) !!}
    

    进一步阅读: https://mattstauffer.co/blog/form-array-validation-in-laravel-5-2#a-quick-introduction-to-html-form-arrays

    【讨论】:

      猜你喜欢
      • 2017-09-23
      • 1970-01-01
      • 2016-10-26
      • 2016-04-30
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      • 2017-05-17
      • 2021-02-06
      相关资源
      最近更新 更多