![](/img/trans.png)
[英]Laravel htmlentities() expects parameter 1 to be string, array given
[英]Laravel 5 Form loop gives htmlentities() expects parameter 1 to be string, array given
我有以下剝離形式:
{{ Form::text('invoicerow[]', null, array( 'class' => 'form-control '.( $errors->has('initial') ? 'errorborder':'' ) ) ) }}
您可以通過JS添加一行。 當我發布這個並驗證它為假時,它會返回到帶有此錯誤的表單頁面。
htmlentities() expects parameter 1 to be string, array given (View: /home/xxx/public_html/resources/views/invoice/create.blade.php)
我怎樣才能避免這個?
使用創建數組的表單會導致 Laravel 出現問題。 解決方法是向表單名稱添加顯式索引(如下所示)。 這應該為您解決問題,並允許您正確驗證每個字段。
{{ Form::text('invoicerow[0]', null, array( 'class' => 'form-control '.( $errors->has('initial') ? 'errorborder':'' ) ) ) }}
{{ Form::text('invoicerow[1]', null, array( 'class' => 'form-control '.( $errors->has('initial') ? 'errorborder':'' ) ) ) }}
{{ Form::text('invoicerow[2]', null, array( 'class' => 'form-control '.( $errors->has('initial') ? 'errorborder':'' ) ) ) }}
...
但是,這確實意味着當添加或刪除新行時,表單中的+
和x
按鈕需要重新計算字段的索引。
例如,如果我刪除invoicerow[1]
,您的 JavaScript 將需要重新計算其他字段的索引,因此invoicerow[2]
被重命名為invoicerow[1]
。
同樣,如果我在最后添加一個新行,新字段的名稱應該是invoicerow[3]
(假設我之前已經添加了 3 個字段),即invoicerow[n+1]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.