当输入数据中存在某些验证错误时,我的表单没有填充.Laravel集体 [英] My form is not populating when there is some validation error in the input data. Laravel Collective

查看:56
本文介绍了当输入数据中存在某些验证错误时,我的表单没有填充.Laravel集体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望你们都做得很好.我正在使用Laravel 5.3和一个LaravelCollective软件包进行表单模型绑定.我有要验证的主题数组,然后如果输入数据中没有错误,则将其存储在数据库中.

Hope yo are all doing great. I am using Laravel 5.3 and a package LaravelCollective for form-model binding. I have subjects array that I want to validate and then store in database if there is no error in the input data.

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

The following snippets show the partial view of form, other views, rules for validations and controller code.

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

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

I want to validate all the subjects passed as an array to the controller using a custom created form request. the following is the code for that form request

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;
    }
}

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

the following is the code for controller method I am using to store inputted array data to database.

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

        // save logic
        dd($data);
    }

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

Problem: My form is not populating when there is some validation error in the input data. After getting validation errors, my input fields are blank.

推荐答案

改为使用表单模型绑定

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

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

这篇关于当输入数据中存在某些验证错误时,我的表单没有填充.Laravel集体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆