1

希望你们都做得很好。我正在使用 Laravel 5.3 和一个包 LaravelCollective 进行表单模型绑定。subjects array如果输入数据没有错误,我想验证然后存储在数据库中。

以下片段显示了表单的部分视图、其他视图、验证规则和控制器代码。

用户配置文件.blade.php

                    <!-- Panel that Adds the Subject - START -->
                    <div class="col-md-12">
                        <div class="panel panel-default" id="add_Subject_panel">
                            <div class="panel-heading">Add Subject(s):</div>
                            <div class="panel-body">
                                {!! Form::open( array('method'=>'post', 'url'=>route('user.store.subject', 1)) ) !!}
                                    @include('user.partials.subjects', ['buttonText'=>'Add Subject(s)'])
                                {!! Form::close() !!}
                            </div>
                        </div>
                    </div>
                    <!-- Panel that Adds the Subject - END -->

主题.blade.php

@if (count($errors) > 0)
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

    <div class="subject-list">
        <div class="input-group subject-input">
            <input type="text" name="name[]" class="form-control" value="" placeholder="Subject" />
            <span class="input-group-btn">
                <span class="btn btn-default">Primary subject</span>
            </span>
        </div>
    </div>

    <div class="text-right">
        <br />
        <button type="button" class="btn btn-success btn-sm btn-add-subject"><span class="glyphicon glyphicon-plus"></span> Add Subject</button>
    </div>

    <div class="form-group">
        <div class="col-md-6 col-md-offset-4 text-right">
            <button type="submit" class="btn btn-primary">
                {{ $buttonText }}&nbsp; <i class="fa fa-btn fa-subject"></i> 
            </button>
        </div>
    </div>


@push('scripts')

{{-- <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script> --}}
<script src="{{ asset('scripts/jquery-2.2.4.min.js') }}" type="text/javascript" charset="utf-8" async defer></script>

<script>

        $(function()
        {

            $(document.body).on('click', '.btn-remove-subject', function(){
                $(this).closest('.subject-input').remove();
            });

            $('.btn-add-subject').click(function()
            {
                var index = $('.subject-input').length + 1;

                $('.subject-list').append(''+
                    '<div class="input-group subject-input" style="margin-top:20px; margin-bottom:20px;">'+
                        '<input type="text" name="name['+index+']" class="form-control" value="" placeholder="Subject" />'+
                        '<span class="input-group-btn">'+
                            '<button class="btn btn-danger btn-remove-subject" type="button"><span class="glyphicon glyphicon-remove"></span></button>'+
                        '</span>'+
                    '</div>'
                ); 
            });
        });


</script>

@endpush

我想使用自定义创建的表单请求验证作为数组传递给控制器​​的所有主题。以下是该表单请求的代码

主题请求.php

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class SubjectRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        // dd("Rules Area");
        foreach($this->request->get('name') as $key => $val)
        {

            $rules['name.'.$key] = 'required|min:5|max:50';
        }
        // dd($rules);
        return $rules;
    }

    public function messages()
    {
        // dd('Message Area. . .');
        $messages = [];
        foreach($this->request->get('name') as $key => $val)
        {
            $messages['name.'.$key.'.required'] = ' Subject Name '.$key.'" is required.';
            $messages['name.'.$key.'.min'] = ' Subject Name '.$key.'" must be atleast :min characters long.';
            $messages['name.'.$key.'.max'] = ' Subject Name '.$key.'" must be less than :max characters.';
        }
        // dd($messages);
        return $messages;
    }
}

以下是我用来将输入的数组数据存储到数据库的控制器方法的代码。

    public function storeSubjects(SubjectRequest $request)
    {
        $data = $request->all();

        // save logic
        dd($data);
    }

问题: 当输入数据中存在一些验证错误时,我的表单没有填充。收到验证错误后,我的输入字段为空白。

4

2 回答 2

0

问题

在 subject.blade.php 中,您的代码看起来像

 <input type="text" name="name[]" class="form-control" value="" placeholder="Subject" />

您的字段未填充,因为您将value属性留空。

解决方案:

传递旧值来解决您的问题。

    <input type="text" name="name[]" class="form-control" value="{{old('name[]')}}" placeholder="Subject" />
于 2016-09-28T06:22:26.673 回答
0

改用表单模型绑定

https://laravelcollective.com/docs/5.3/html#form-model-binding

{{ Form::model($user, [....]) }}
   {{ Form::text('firstname', null, [...]) }}
{{ Form::close() }}
于 2017-06-21T08:53:39.877 回答