验证错误显示在同一页上的不同div上

时间:2018-08-01 12:44:56

标签: php laravel

我有一个页面,该页面的表单带有选择菜单,如果表单中存在某些验证错误,则使用“ @include('includes.errors')”显示验证错误。但是在同一页面中,我有一个按钮,当用户单击该按钮时,它会显示一个模式,用户可以在其中介绍主题,并发送一条消息以发送电子邮件。在此模式下,我也有“ @include('includes.errors')”。

问题:因此问题在于,如果模态中的表单中存在验证错误,因为用户未填写主题或信息,则错误会同时出现在模态中具有选择菜单的表单上方的页面。另外,如果表单中的某些验证错误带有选择菜单,并且用户打开了验证方式,验证错误也会出现在验证方式中。

您知道如何解决此问题吗?

带有选择菜单的表单:

@include('includes.errors')

<div class="card">
    <div class="card_body">
        <form method="post"
              action="{{route('conferences.storeQuantities', ['id' => $confernece->id, 'slug' => $conference->slug])}}">

            <ul class="list-group list-group-flush">
                {{ csrf_field() }}

                @foreach($registration_types as $rtype)
                    <li>
                        <span>{{$rtype->name}}</span>
                        <select id="rtype_{{ $rtype->id }}" data-price="{{ $rtype->price }}"
                                name="rtypes[{{ $rtype->name }}]">
                            <option value="">0</option>
                            @for ($i = $rtype->min_participants; $i <= $rtype-> max_participants; $i++)
                                <option value="{{ $i }}">{{ $i }}</option>
                            @endfor
                        </select>
                        <span>X {{$rtype->presentPrice()}}€</span>
                    </li>
                @endforeach 
            </ul>
        </form>
    </div>
</div>

模式:

<div class="modal fade bd-example-modal-lg" id="contactOrganizer" tabindex="-1" 
role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-md">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Contact Organizer</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <div class="container">
                    <div class="row">
                        <div class="col">
                            @include('includes.errors')
                            @if($flash = session('email_sent'))
                                <div class="alert alert-success" role="alert">
                                    <strong><i class="fa fa-check-circle" aria-hidden="true"></i></strong>
                                    {{ $flash }}
                                </div>
                            @endif
                            <form method="post"
                                  action="{{route('users.contactOrganizer', ['conference_id' => $conference->id])}}"
                                  enctype="multipart/form-data">
                                {{csrf_field()}}
                                <div class="form-group">
                                    <label for="subject">Subject</label>
                                    <input type="text" class="form-control" value="{{ old('subject') }}"
                                           name="subject"
                                           id="subject">
                                </div>

                                <div class="form-group">
                                    <label for="message">Message</label>
                                    <textarea class="form-control" id="message" value="{{ old('message') }}"
                                              name="message" rows="3"></textarea>
                                </div>
                                <input type="submit" class="btn btn-primary btn" value="Send"/>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

jQuery,以显示联系人管理器表单中是否存在验证错误的模式:

$(window).on('load', function () {
    @if ($errors->any())
    $('#contactOrganizer').trigger('click');

    @endif

    @if(session('email_sent'))
    $('#contactOrganizer').trigger('click');
    @endif
});

errors.blade.php文件:

@if ($errors->any())
    <div class="alert alert-danger mt-3">
        <ul>
            @foreach ($errors->all() as $error)
                <li class="text-danger"><strong><i class="fa fa-times" aria-hidden="true"></i></strong> {{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

1 个答案:

答案 0 :(得分:0)

您可以使用命名的消息袋来实现所需的目的,

在控制器中,您必须这样命名错误:

function storeQuantities(){ //assuming this is your controller function 
    $validator = Validator::make($request->all(), [
            // your validation rules here :)
        ]);

        if ($validator->fails())
        {
            return redirect()->back()->withErrors($validator, 'conferneceErrors');
        }
}

对其他功能使用相同的东西:

function contactOrganizer(){ //assuming this is your controller function 
    $validator = Validator::make($request->all(), [
            // your validation rules here :)
        ]);

        if ($validator->fails())
        {
            return redirect()->back()->withErrors($validator, 'contactErrors');
        }
}

在视图中,您可以这样做:

@include('includes.errors', ['errors' => $errors->conferneceErrors]) 

对于模式:

@include('includes.errors', ['errors' => $errors->contactErrors])