简体   繁体   中英

Laravel 5 Form loop gives htmlentities() expects parameter 1 to be string, array given

在此处输入图片说明

I have the following stripped form:

{{ Form::text('invoicerow[]', null, array( 'class' => 'form-control '.( $errors->has('initial') ? 'errorborder':'' )  ) ) }}

You can add a row by JS. When i post this and validates it false it's return to the form page with this error.

htmlentities() expects parameter 1 to be string, array given (View: /home/xxx/public_html/resources/views/invoice/create.blade.php)

How can i obviated this?

Using forms that create arrays cause problems with Laravel. The workaround for this is to add explicit indexes to your form names (as below). This should resolve the issue for you, and allow you to correctly validate each field.

{{ 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':'' )  ) ) }}
...

However, it does mean that the + and x buttons you have in the form need to recalculate the indexes for the fields when a new row has been added, or deleted.

Eg if I delete invoicerow[1] , your JavaScript will need to recalculate the index for the other fields, so invoicerow[2] is renamed to invoicerow[1] .

Similarly, if I add a new row to the end, the name for the new field should be invoicerow[3] (assuming I've already added 3 fields before), ie invoicerow[n+1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM