当输入数据中存在某些验证错误时,我的表单不会填充。 Laravel Collective

时间:2016-09-23 10:17:29

标签: forms laravel validation laravelcollective

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

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

UserProfile.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 -->

subjects.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

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

SubjectRequest.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);
    }

问题: 当输入数据中存在某些验证错误时,我的表单不会填充。 获得验证错误后,我的输入字段为空。

2 个答案:

答案 0 :(得分: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" />

答案 1 :(得分:0)

使用表单模型绑定

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

{{ Form::model($user, [....]) }}
   {{ Form::text('firstname', null, [...]) }}
{{ Form::close() }}