Laravel 5.5:htmlspecialchars()期望参数1为字符串,给定

时间:2017-12-05 08:22:05

标签: php laravel-5.5

以下视图会引发以下错误:htmlspecialchars() expects parameter 1 to be string, array given。奇怪的是,这个代码在弹出这个错误之前工作了一段时间。

(只有在发布以下表单时才会出现错误。加载视图时没有错误)

@extends($template)

@section('content')

<div class="container" style="margin-top: 50px; margin-bottom: 50px;">

    <div class="row">

        <!-- Blog Entries Column -->
        <div class="col-md-12">

            <h1 class="my-4">Report Incident
            </h1>

            <form action="{{ route('add_post') }}" method="POST" enctype="multipart/form-data">
                {{ csrf_field() }}
                <div class="form-group">
                    <label for="title">Title</label>
                    <input type="text" class="form-control {{ $errors->has('title') ? ' is-invalid' : '' }}" id="title" name="title" placeholder="Title" value="{{ old('title') }}" required>
                    <div class="invalid-feedback">
                        {{ $errors->first('title') }}
                    </div>
                </div>
                <div class="form-group">
                    <label for="date">Date of incident</label>
                    <input type="date" class="form-control {{ $errors->has('date') ? ' is-invalid' : '' }}" id="date" name="date" required value="{{ old('date') }}">
                    <div class="invalid-feedback">
                        {{ $errors->first('date') }}
                    </div>
                </div>
                <div class="form-group">
                    <label for="type">Incident Type</label>
                    <select class="form-control {{ $errors->has('type') ? ' is-invalid' : '' }}" id="type" name="type">
                        @foreach($types as $type => $icon)
                            @if($type == old('type'))
                                <option value="{{ $type }}" selected>{{ $type }}</option>
                            @else
                                <option value="{{ $type }}">{{ $type }}</option>
                            @endif
                        @endforeach
                    </select>
                    <div class="invalid-feedback">
                            {{ $errors->first('type') }}
                    </div>
                </div>
                <div class="form-group">
                    <label for="description">Description</label>
                    <textarea class="form-control {{ $errors->has('description') ? ' is-invalid' : '' }}" id="description" name="description" rows="5" required>{{ old('description') }}</textarea>
                    <div class="invalid-feedback">
                        {{ $errors->first('description') }}
                    </div>
                </div>
                <div class="form-group">
                    <label for="images">Upload images (if any)</label>
                    <input type="file" class="form-control-file {{ $errors->has('images') ? ' is-invalid' : '' }}" id="images" name="images[]" value="{{ old('images') }}" accept=".jpg, .png" multiple>
                    <div class="invalid-feedback">
                        {{ $errors->first('images') }}
                    </div>
                </div>

                <div class="form-group">
                    <label for="province">Select Province</label>
                    <select class="form-control {{ $errors->has('province') ? ' is-invalid' : '' }}" id="province" name="province">
                        @foreach($provinces as $province)
                            @if($province == old('$province'))
                                <option value="{{ $province }}" selected>{{ $province }}</option>
                            @else
                                <option value="{{ $province }}">{{ $province }}</option>
                            @endif
                        @endforeach
                    </select>
                    <div class="invalid-feedback">
                        {{ $errors->first('province') }}
                    </div>
                </div>
                <div class="form-group">
                    <label for="pac-input">Please type in a location</label>
                    <input type="text" class="form-control {{ $errors->has('pac-input') ? ' is-invalid' : '' }}" id="pac-input" name="pac-input" placeholder="Location" value="{{ old('pac-input') }}" required>
                    <div class="invalid-feedback">
                        {{ $errors->first('pac-input') }}
                    </div>
                </div>
                <div class="form-group">
                    <label>Please select an approximate location from the map</label>
                    <div id="map" style="height: 500px; width: 100%;"></div>
                </div>
                <div class="d-inline-block">
                    <label for="lat">Latitude</label>
                    <input type="text" name="lat" id="lat" class="form-control" value="{{ old('lat') }}" readonly>
                </div>
                <div class="d-inline-block">
                    <label for="lng" style="margin-top: 10px;">Longitude</label>
                    <input type="text" name="lng" id="lng" class="form-control" value="{{ old('lng') }}" readonly>
                </div>
                <div class="form-group col-6 col-md-4">


                </div>
                <div class="form-group">
                    <br><br><input type="submit" class="btn btn-lg btn-block btn-success" value="Submit">
                </div>
            </form>

        </div>

    </div>
    <!-- /.row -->

</div>


<script>
    var map;
    function initAutocomplete(){
        console.log(document.getElementById('map'));
        map = new google.maps.Map(document.getElementById('map'), {
            center: {lat: 6.870066, lng: 79.879710},
            zoom: 15
        });
        var marker = new google.maps.Marker({
            position: {
                lat: 16.870066,
                lng: 80.879710
            },
            map: map,
            draggable: true
        });

//            document.getElementById('lat').value = marker.getPosition().lat();
//            document.getElementById('lng').value = marker.getPosition().lng();

        var input = document.getElementById('pac-input');

        var searchBox = new google.maps.places.SearchBox(input);

        google.maps.event.addListener(searchBox, 'places_changed',function(){
            var places = searchBox.getPlaces();
            var bounds = new google.maps.LatLngBounds();
            var i, place;

            for (i=0; place=places[i]; i++) {
                bounds.extend(place.geometry.location);
                marker.setPosition(place.geometry.location);
            }

            map.fitBounds(bounds);
            map.setZoom(15);
        });

        google.maps.event.addListener(marker, 'position_changed', function(){
            var lat = marker.getPosition().lat();
            var lng = marker.getPosition().lng();

            document.getElementById('lat').value = lat;
            document.getElementById('lng').value = lng;
        });
    }
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=API_KEY_WAS_HERE&libraries=places&callback=initAutocomplete"
        async defer></script>

@endsection

我认为这与我的文件上传输入声明有关:

<div class="form-group">
                    <label for="images">Upload images (if any)</label>
                    <input type="file" class="form-control-file {{ $errors->has('images') ? ' is-invalid' : '' }}" id="images" name="images[]" value="{{ old('images') }}" accept=".jpg, .png" multiple>
                    <div class="invalid-feedback">
                        {{ $errors->first('images') }}
                    </div>
</div>

我给出以下输入的名称是images[],因为我想要上传多个图像。当我将名称从images[]更改为images时,错误似乎消失了,但没有上传/保存图像(如果需要,也可以发布处理后端图像上传的代码)。< / p>

1 个答案:

答案 0 :(得分:1)

您如何期望PHP回显数组?它不能。

您的输入正在使用数组命名。所以在请求中的服务器端images是一个数组。因此images的“旧”数据是一个数组,而不是单个值。您正在尝试回显数组{{ old('images') }}

首先,您无法在文件输入上设置值。这将是一个安全问题。

其次,您可能有多个文件而不是一个,那么您尝试将其设置为什么值?除了数组之外,您认为old('images')会是什么?